You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CeDistributedInformationImplTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.google.common.collect.ImmutableSet;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import java.util.stream.Collectors;
  28. import java.util.stream.Stream;
  29. import org.junit.Test;
  30. import org.sonar.ce.taskprocessor.CeWorker;
  31. import org.sonar.ce.taskprocessor.CeWorkerFactory;
  32. import org.sonar.process.cluster.hz.HazelcastMember;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.data.MapEntry.entry;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.when;
  37. import static org.sonar.process.cluster.hz.HazelcastObjects.WORKER_UUIDS;
  38. public class CeDistributedInformationImplTest {
  39. private String clientUUID1 = "1";
  40. private String clientUUID2 = "2";
  41. private String clientUUID3 = "3";
  42. private Map workerMap = ImmutableMap.of(
  43. clientUUID1, ImmutableSet.of("1", "2"),
  44. clientUUID2, ImmutableSet.of("3"),
  45. clientUUID3, ImmutableSet.of("4", "5", "6"));
  46. private HazelcastMember hzClientWrapper = mock(HazelcastMember.class);
  47. @Test
  48. public void getWorkerUUIDs_returns_union_of_workers_uuids_of_local_and_cluster_worker_uuids() {
  49. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  50. when(hzClientWrapper.getMemberUuids()).thenReturn(ImmutableSet.of(clientUUID1, clientUUID2, clientUUID3));
  51. when(hzClientWrapper.getReplicatedMap(WORKER_UUIDS)).thenReturn(workerMap);
  52. CeDistributedInformation ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, mock(CeWorkerFactory.class));
  53. assertThat(ceDistributedInformation.getWorkerUUIDs()).containsExactly("1", "2", "3", "4", "5", "6");
  54. }
  55. @Test
  56. public void getWorkerUUIDs_must_filter_absent_client() {
  57. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  58. when(hzClientWrapper.getMemberUuids()).thenReturn(ImmutableSet.of(clientUUID1, clientUUID2));
  59. when(hzClientWrapper.getReplicatedMap(WORKER_UUIDS)).thenReturn(workerMap);
  60. CeDistributedInformation ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, mock(CeWorkerFactory.class));
  61. assertThat(ceDistributedInformation.getWorkerUUIDs()).containsExactly("1", "2", "3");
  62. }
  63. @Test
  64. public void broadcastWorkerUUIDs_adds_local_workerUUIDs_to_shared_map_under_key_of_localendpoint_uuid() {
  65. Set<String> connectedClients = new HashSet<>();
  66. Map modifiableWorkerMap = new HashMap<>();
  67. connectedClients.add(clientUUID1);
  68. connectedClients.add(clientUUID2);
  69. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  70. when(hzClientWrapper.getMemberUuids()).thenReturn(connectedClients);
  71. when(hzClientWrapper.getReplicatedMap(WORKER_UUIDS)).thenReturn(modifiableWorkerMap);
  72. CeWorkerFactory ceWorkerFactory = mock(CeWorkerFactory.class);
  73. Set<CeWorker> ceWorkers = Stream.of("a10", "a11").map(uuid -> {
  74. CeWorker res = mock(CeWorker.class);
  75. when(res.getUUID()).thenReturn(uuid);
  76. return res;
  77. }).collect(Collectors.toSet());
  78. when(ceWorkerFactory.getWorkers()).thenReturn(ceWorkers);
  79. CeDistributedInformationImpl ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, ceWorkerFactory);
  80. try {
  81. ceDistributedInformation.broadcastWorkerUUIDs();
  82. assertThat(modifiableWorkerMap).containsExactly(
  83. entry(clientUUID1, ImmutableSet.of("a10", "a11")));
  84. } finally {
  85. ceDistributedInformation.stop();
  86. }
  87. }
  88. @Test
  89. public void stop_must_remove_local_workerUUIDs() {
  90. Set<String> connectedClients = new HashSet<>();
  91. connectedClients.add(clientUUID1);
  92. connectedClients.add(clientUUID2);
  93. connectedClients.add(clientUUID3);
  94. Map modifiableWorkerMap = new HashMap(workerMap);
  95. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  96. when(hzClientWrapper.getMemberUuids()).thenReturn(connectedClients);
  97. when(hzClientWrapper.getReplicatedMap(WORKER_UUIDS)).thenReturn(modifiableWorkerMap);
  98. CeDistributedInformationImpl ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, mock(CeWorkerFactory.class));
  99. ceDistributedInformation.stop();
  100. assertThat(modifiableWorkerMap).containsExactly(
  101. entry(clientUUID2, ImmutableSet.of("3")),
  102. entry(clientUUID3, ImmutableSet.of("4", "5", "6")));
  103. }
  104. }