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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.UUID;
  28. import java.util.stream.Collectors;
  29. import java.util.stream.Stream;
  30. import org.junit.Test;
  31. import org.sonar.ce.taskprocessor.CeWorker;
  32. import org.sonar.ce.taskprocessor.CeWorkerFactory;
  33. import org.sonar.process.cluster.hz.HazelcastMember;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.assertj.core.data.MapEntry.entry;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.when;
  38. import static org.sonar.process.cluster.hz.HazelcastObjects.WORKER_UUIDS;
  39. public class CeDistributedInformationImplTest {
  40. private final UUID clientUUID1 = UUID.randomUUID();
  41. private final UUID clientUUID2 = UUID.randomUUID();
  42. private final UUID clientUUID3 = UUID.randomUUID();
  43. private final String w1 = UUID.randomUUID().toString();
  44. private final String w2 = UUID.randomUUID().toString();
  45. private final String w3 = UUID.randomUUID().toString();
  46. private final String w4 = UUID.randomUUID().toString();
  47. private final String w5 = UUID.randomUUID().toString();
  48. private final String w6 = UUID.randomUUID().toString();
  49. private final Map<UUID, Set<String>> workerMap = ImmutableMap.of(
  50. clientUUID1, ImmutableSet.of(w1, w2),
  51. clientUUID2, ImmutableSet.of(w3),
  52. clientUUID3, ImmutableSet.of(w4, w5, w6));
  53. private final HazelcastMember hzClientWrapper = mock(HazelcastMember.class);
  54. @Test
  55. public void getWorkerUUIDs_returns_union_of_workers_uuids_of_local_and_cluster_worker_uuids() {
  56. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  57. when(hzClientWrapper.getMemberUuids()).thenReturn(ImmutableSet.of(clientUUID1, clientUUID2, clientUUID3));
  58. when(hzClientWrapper.<UUID, Set<String>>getReplicatedMap(WORKER_UUIDS)).thenReturn(workerMap);
  59. CeDistributedInformation ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, mock(CeWorkerFactory.class));
  60. assertThat(ceDistributedInformation.getWorkerUUIDs()).containsExactlyInAnyOrder(w1, w2, w3, w4, w5, w6);
  61. }
  62. @Test
  63. public void getWorkerUUIDs_must_filter_absent_client() {
  64. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  65. when(hzClientWrapper.getMemberUuids()).thenReturn(ImmutableSet.of(clientUUID1, clientUUID2));
  66. when(hzClientWrapper.<UUID, Set<String>>getReplicatedMap(WORKER_UUIDS)).thenReturn(workerMap);
  67. CeDistributedInformation ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, mock(CeWorkerFactory.class));
  68. assertThat(ceDistributedInformation.getWorkerUUIDs()).containsExactlyInAnyOrder(w1, w2, w3);
  69. }
  70. @Test
  71. public void broadcastWorkerUUIDs_adds_local_workerUUIDs_to_shared_map_under_key_of_localendpoint_uuid() {
  72. Set<UUID> connectedClients = new HashSet<>();
  73. Map<UUID, Set<String>> modifiableWorkerMap = new HashMap<>();
  74. connectedClients.add(clientUUID1);
  75. connectedClients.add(clientUUID2);
  76. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  77. when(hzClientWrapper.getMemberUuids()).thenReturn(connectedClients);
  78. when(hzClientWrapper.<UUID, Set<String>>getReplicatedMap(WORKER_UUIDS)).thenReturn(modifiableWorkerMap);
  79. CeWorkerFactory ceWorkerFactory = mock(CeWorkerFactory.class);
  80. Set<CeWorker> ceWorkers = Stream.of("a10", "a11").map(uuid -> {
  81. CeWorker res = mock(CeWorker.class);
  82. when(res.getUUID()).thenReturn(uuid);
  83. return res;
  84. }).collect(Collectors.toSet());
  85. when(ceWorkerFactory.getWorkers()).thenReturn(ceWorkers);
  86. CeDistributedInformationImpl ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, ceWorkerFactory);
  87. try {
  88. ceDistributedInformation.broadcastWorkerUUIDs();
  89. assertThat(modifiableWorkerMap).containsExactly(
  90. entry(clientUUID1, ImmutableSet.of("a10", "a11")));
  91. } finally {
  92. ceDistributedInformation.stop();
  93. }
  94. }
  95. @Test
  96. public void stop_must_remove_local_workerUUIDs() {
  97. Set<UUID> connectedClients = new HashSet<>();
  98. connectedClients.add(clientUUID1);
  99. connectedClients.add(clientUUID2);
  100. connectedClients.add(clientUUID3);
  101. Map<UUID, Set<String>> modifiableWorkerMap = new HashMap<>(workerMap);
  102. when(hzClientWrapper.getUuid()).thenReturn(clientUUID1);
  103. when(hzClientWrapper.getMemberUuids()).thenReturn(connectedClients);
  104. when(hzClientWrapper.<UUID, Set<String>>getReplicatedMap(WORKER_UUIDS)).thenReturn(modifiableWorkerMap);
  105. CeDistributedInformationImpl ceDistributedInformation = new CeDistributedInformationImpl(hzClientWrapper, mock(CeWorkerFactory.class));
  106. ceDistributedInformation.stop();
  107. assertThat(modifiableWorkerMap).containsExactlyInAnyOrderEntriesOf(
  108. ImmutableMap.of(clientUUID2, ImmutableSet.of(w3), clientUUID3, ImmutableSet.of(w4, w5, w6)));
  109. }
  110. }