]> source.dussan.org Git - sonarqube.git/commitdiff
Make tests on clustering more robust
authorEric Hartmann <hartmann.eric@gmail.com>
Thu, 11 May 2017 16:26:46 +0000 (18:26 +0200)
committerEric Hartmann <hartmann.eric@gmail.com>
Thu, 11 May 2017 16:26:58 +0000 (18:26 +0200)
server/sonar-ce/src/test/java/org/sonar/ce/cluster/HazelcastClientWrapperImplTest.java
server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastClusterTest.java
server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java

index 095d0411e76884e55c0155d1ae7167627353a56e..75877871fa39734a0082be6ae47549d77e2456c4 100644 (file)
@@ -60,7 +60,7 @@ public class HazelcastClientWrapperImplTest {
   public ExpectedException expectedException = ExpectedException.none();
 
   @Rule
-  public TestRule safeGuard = new DisableOnDebug(Timeout.seconds(10));
+  public TestRule safeGuard = new DisableOnDebug(Timeout.seconds(20));
 
   private static HazelcastInstance hzCluster;
   private static HazelcastClientWrapperImpl hzClient;
index d8b8295127d028cbc512173c76e52f4bed539b45..3f7eb072808ee019f6536ebe167353084865d421 100644 (file)
@@ -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());
index c284cc869ff3bebd8078ca56e6fae38cffa63d62..76724b38c4da14748da3f77a8bdf7a994f23385e 100644 (file)
@@ -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) {