diff options
author | Eric Hartmann <hartmann.eric@gmail.com> | 2017-05-11 18:26:46 +0200 |
---|---|---|
committer | Eric Hartmann <hartmann.eric@gmail.com> | 2017-05-11 18:26:58 +0200 |
commit | 35cace9be8a1dc08046f4c3fa50885bc6d4b3051 (patch) | |
tree | 01c8f2fed72224f4b16bf061f1d6c1c8192976f0 /server/sonar-process-monitor | |
parent | f6554b56731131f5d97b6d2f22801237250d8920 (diff) | |
download | sonarqube-35cace9be8a1dc08046f4c3fa50885bc6d4b3051.tar.gz sonarqube-35cace9be8a1dc08046f4c3fa50885bc6d4b3051.zip |
Make tests on clustering more robust
Diffstat (limited to 'server/sonar-process-monitor')
2 files changed, 27 insertions, 1 deletions
diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java index d8b8295127d..3f7eb072808 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java @@ -33,6 +33,7 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.DisableOnDebug; @@ -52,6 +53,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.sonar.application.cluster.HazelcastTestHelper.closeAllHazelcastClients; import static org.sonar.application.cluster.HazelcastTestHelper.createHazelcastClient; import static org.sonar.application.cluster.HazelcastTestHelper.newClusterSettings; import static org.sonar.process.ProcessProperties.CLUSTER_NAME; @@ -66,6 +68,11 @@ public class HazelcastClusterTest { @Rule public ExpectedException expectedException = ExpectedException.none(); + @AfterClass + public static void closeHazelcastClients() { + closeAllHazelcastClients(); + } + @Test public void test_two_tryToLockWebLeader_must_return_true() { ClusterProperties clusterProperties = new ClusterProperties(newClusterSettings()); diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java index c284cc869ff..76724b38c4d 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java @@ -24,11 +24,16 @@ import com.hazelcast.client.HazelcastClient; import com.hazelcast.client.config.ClientConfig; import com.hazelcast.core.HazelcastInstance; import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; import org.sonar.application.config.TestAppSettings; import org.sonar.process.ProcessProperties; public class HazelcastTestHelper { + // Be careful this test won't work if parallel tests is used + private static final List<HazelcastInstance> HAZELCAST_INSTANCES = new ArrayList<>(); + static HazelcastInstance createHazelcastClient(HazelcastCluster hzCluster) { ClientConfig clientConfig = new ClientConfig(); InetSocketAddress socketAddress = (InetSocketAddress) hzCluster.hzInstance.getLocalEndpoint().getSocketAddress(); @@ -39,7 +44,21 @@ public class HazelcastTestHelper { socketAddress.getPort() )); clientConfig.getGroupConfig().setName(hzCluster.getName()); - return HazelcastClient.newHazelcastClient(clientConfig); + HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(clientConfig); + HAZELCAST_INSTANCES.add(hazelcastInstance); + return hazelcastInstance; + } + + static void closeAllHazelcastClients() { + HAZELCAST_INSTANCES.stream().forEach( + hz -> { + try { + hz.shutdown(); + } catch (Exception ex) { + // Ignore it + } + } + ); } static HazelcastInstance createHazelcastClient(AppStateClusterImpl appStateCluster) { |