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.

StandaloneCeDistributedInformationTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.ImmutableSet;
  22. import java.util.Arrays;
  23. import java.util.Random;
  24. import java.util.concurrent.TimeUnit;
  25. import java.util.concurrent.locks.Lock;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.IntStream;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.ExpectedException;
  31. import org.sonar.ce.taskprocessor.CeWorker;
  32. import org.sonar.ce.taskprocessor.CeWorkerFactory;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.fail;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.when;
  38. public class StandaloneCeDistributedInformationTest {
  39. @Rule
  40. public ExpectedException expectedException = ExpectedException.none();
  41. @Test
  42. public void broadcastWorkerUUIDs_must_retrieve_from_ceworkerfactory() {
  43. CeWorkerFactory ceWorkerFactory = mock(CeWorkerFactory.class);
  44. StandaloneCeDistributedInformation ceCluster = new StandaloneCeDistributedInformation(ceWorkerFactory);
  45. ceCluster.broadcastWorkerUUIDs();
  46. verify(ceWorkerFactory).getWorkers();
  47. }
  48. @Test
  49. public void getWorkerUUIDs_must_be_retrieved_from_ceworkerfactory() {
  50. CeWorkerFactory ceWorkerFactory = mock(CeWorkerFactory.class);
  51. CeWorker[] ceWorkers = IntStream.range(0, new Random().nextInt(10))
  52. .mapToObj(i -> {
  53. CeWorker ceWorker = mock(CeWorker.class);
  54. when(ceWorker.getUUID()).thenReturn("uuid_" + i);
  55. return ceWorker;
  56. })
  57. .toArray(CeWorker[]::new);
  58. when(ceWorkerFactory.getWorkers()).thenReturn(ImmutableSet.copyOf(ceWorkers));
  59. StandaloneCeDistributedInformation ceCluster = new StandaloneCeDistributedInformation(ceWorkerFactory);
  60. ceCluster.broadcastWorkerUUIDs();
  61. assertThat(ceCluster.getWorkerUUIDs()).isEqualTo(Arrays.stream(ceWorkers).map(CeWorker::getUUID).collect(Collectors.toSet()));
  62. }
  63. @Test
  64. public void getWorkerUUIDs_throws_ISE_if_broadcastWorkerUUIDs_has_not_been_called_before() {
  65. CeWorkerFactory ceWorkerFactory = mock(CeWorkerFactory.class);
  66. StandaloneCeDistributedInformation ceCluster = new StandaloneCeDistributedInformation(ceWorkerFactory);
  67. expectedException.expect(IllegalStateException.class);
  68. expectedException.expectMessage("Invalid call, broadcastWorkerUUIDs() must be called first.");
  69. ceCluster.getWorkerUUIDs();
  70. }
  71. @Test
  72. public void acquireCleanJobLock_returns_a_non_current_lock() {
  73. StandaloneCeDistributedInformation underTest = new StandaloneCeDistributedInformation(mock(CeWorkerFactory.class));
  74. Lock lock = underTest.acquireCleanJobLock();
  75. IntStream.range(0, 5 + Math.abs(new Random().nextInt(50)))
  76. .forEach(i -> {
  77. try {
  78. assertThat(lock.tryLock()).isTrue();
  79. assertThat(lock.tryLock(1, TimeUnit.MINUTES)).isTrue();
  80. lock.lock();
  81. lock.lockInterruptibly();
  82. lock.unlock();
  83. } catch (InterruptedException e) {
  84. fail("no InterruptedException should be thrown");
  85. }
  86. try {
  87. lock.newCondition();
  88. fail("a UnsupportedOperationException should have been thrown");
  89. } catch (UnsupportedOperationException e) {
  90. assertThat(e.getMessage()).isEqualTo("newCondition not supported");
  91. }
  92. });
  93. }
  94. }