[hornetq-commits] JBoss hornetq SVN: r12122 - in trunk: hornetq-core/src/main/java/org/hornetq/core/server/impl and 3 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Feb 15 11:39:11 EST 2012


Author: borges
Date: 2012-02-15 11:39:11 -0500 (Wed, 15 Feb 2012)
New Revision: 12122

Modified:
   trunk/hornetq-core/src/main/java/org/hornetq/core/server/HornetQServer.java
   trunk/hornetq-core/src/main/java/org/hornetq/core/server/impl/HornetQServerImpl.java
   trunk/hornetq-core/src/test/java/org/hornetq/tests/util/ServiceTestBase.java
   trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/bridge/BridgeTestBase.java
   trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/distribution/ClusterTestBase.java
Log:
Add a waitForInitialization method to simplify test code.

Modified: trunk/hornetq-core/src/main/java/org/hornetq/core/server/HornetQServer.java
===================================================================
--- trunk/hornetq-core/src/main/java/org/hornetq/core/server/HornetQServer.java	2012-02-15 16:38:34 UTC (rev 12121)
+++ trunk/hornetq-core/src/main/java/org/hornetq/core/server/HornetQServer.java	2012-02-15 16:39:11 UTC (rev 12122)
@@ -15,7 +15,9 @@
 
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import org.hornetq.api.core.HornetQException;
 import org.hornetq.api.core.Pair;
@@ -143,6 +145,15 @@
 
    boolean isInitialised();
 
+   /**
+    * Wait for server initialization.
+    * @param timeout
+    * @param unit
+    * @see CountDownLatch#await(long, TimeUnit)
+    * @throws InterruptedException
+    */
+   boolean waitForInitialization(long timeout, TimeUnit unit) throws InterruptedException;
+
    Queue createQueue(SimpleString address,
                      SimpleString queueName,
                      SimpleString filter,

Modified: trunk/hornetq-core/src/main/java/org/hornetq/core/server/impl/HornetQServerImpl.java
===================================================================
--- trunk/hornetq-core/src/main/java/org/hornetq/core/server/impl/HornetQServerImpl.java	2012-02-15 16:38:34 UTC (rev 12121)
+++ trunk/hornetq-core/src/main/java/org/hornetq/core/server/impl/HornetQServerImpl.java	2012-02-15 16:39:11 UTC (rev 12122)
@@ -28,6 +28,7 @@
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -225,8 +226,13 @@
 
    private final Map<String, ServerSession> sessions = new ConcurrentHashMap<String, ServerSession>();
 
+   /**
+    * We guard the {@code initialised} field because if we restart a {@code HornetQServer}, we need
+    * to replace the {@code CountDownLatch} by a new one.
+    */
    private final Object initialiseLock = new Object();
-   private boolean initialised;
+   private CountDownLatch initialised = new CountDownLatch(1);
+
    private final Object startUpLock = new Object();
    private final Object replicationLock = new Object();
 
@@ -666,7 +672,13 @@
          sessions.clear();
 
          started = false;
-         initialised = false;
+            synchronized (initialiseLock)
+            {
+               // replace the latch only if necessary. It could still be '1' in case of errors
+               // during start-up.
+               if (initialised.getCount() < 1)
+                  initialised = new CountDownLatch(1);
+            }
          }
          
          // to display in the log message
@@ -910,14 +922,26 @@
       return new HashSet<ServerSession>(sessions.values());
    }
 
+   @Override
    public boolean isInitialised()
    {
       synchronized (initialiseLock)
       {
-         return initialised;
+         return initialised.getCount() < 1;
       }
    }
 
+   @Override
+   public boolean waitForInitialization(long timeout, TimeUnit unit) throws InterruptedException
+   {
+      CountDownLatch latch;
+      synchronized (initialiseLock)
+      {
+         latch = initialised;
+      }
+      return latch.await(timeout, unit);
+   }
+
    public HornetQServerControlImpl getHornetQServerControl()
    {
       return messagingServerControl;
@@ -1482,8 +1506,7 @@
 
       remotingService.start();
 
-      initialised = true;
-
+      initialised.countDown();
    }
 
    /**

Modified: trunk/hornetq-core/src/test/java/org/hornetq/tests/util/ServiceTestBase.java
===================================================================
--- trunk/hornetq-core/src/test/java/org/hornetq/tests/util/ServiceTestBase.java	2012-02-15 16:38:34 UTC (rev 12121)
+++ trunk/hornetq-core/src/test/java/org/hornetq/tests/util/ServiceTestBase.java	2012-02-15 16:39:11 UTC (rev 12122)
@@ -20,6 +20,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import javax.management.MBeanServer;
 
@@ -331,16 +332,8 @@
 
       if (!server.getConfiguration().isBackup())
       {
-         timetowait = System.currentTimeMillis() + 5000;
-         while (!server.isInitialised() && System.currentTimeMillis() < timetowait)
-         {
-            Thread.sleep(50);
-         }
-
-         if (!server.isInitialised())
-         {
+         if (!server.waitForInitialization(5000, TimeUnit.MILLISECONDS))
             fail("Server didn't initialize: " + server);
-         }
       }
    }
 

Modified: trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/bridge/BridgeTestBase.java
===================================================================
--- trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/bridge/BridgeTestBase.java	2012-02-15 16:38:34 UTC (rev 12121)
+++ trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/bridge/BridgeTestBase.java	2012-02-15 16:39:11 UTC (rev 12122)
@@ -15,12 +15,12 @@
 
 import java.util.ArrayList;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import javax.management.MBeanServer;
 
 import org.hornetq.api.core.TransportConfiguration;
 import org.hornetq.core.config.Configuration;
-import org.hornetq.core.config.impl.ConfigurationImpl;
 import org.hornetq.core.remoting.impl.invm.InVMConnector;
 import org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory;
 import org.hornetq.core.server.HornetQServer;
@@ -34,7 +34,7 @@
  * A BridgeTestBase
  *
  * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- * 
+ *
  * Created 21 Nov 2008 10:32:23
  *
  *
@@ -187,21 +187,8 @@
 
    protected void waitForServerStart(HornetQServer server) throws Exception
    {
-      long start = System.currentTimeMillis();
-      do
-      {
-         if (server.isInitialised())
-         {
-            return;
-         }
-         Thread.sleep(10);
-      }
-      while (System.currentTimeMillis() - start < 5000);
-
-      String msg = "Timed out waiting for server starting = " + server;
-
-
-      throw new IllegalStateException(msg);
+      if (!server.waitForInitialization(5000L, TimeUnit.MILLISECONDS))
+         throw new IllegalStateException("Timed out waiting for server starting = " + server);
    }
 
     // Inner classes -------------------------------------------------

Modified: trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/distribution/ClusterTestBase.java
===================================================================
--- trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/distribution/ClusterTestBase.java	2012-02-15 16:38:34 UTC (rev 12121)
+++ trunk/tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/distribution/ClusterTestBase.java	2012-02-15 16:39:11 UTC (rev 12122)
@@ -24,6 +24,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import junit.framework.Assert;
 
@@ -296,21 +297,14 @@
 
    protected void waitForServerRestart(final int node) throws Exception
    {
-      long start = System.currentTimeMillis();
-      do
+      if (!servers[node].waitForInitialization(ServiceTestBase.WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
       {
-         if (servers[node].isInitialised())
-         {
-            return;
-         }
-         Thread.sleep(10);
-      }
-      while (System.currentTimeMillis() - start < ServiceTestBase.WAIT_TIMEOUT);
-      String msg = "Timed out waiting for server starting = " + node;
+         String msg = "Timed out waiting for server starting = " + node;
 
-      log.error(msg);
+         log.error(msg);
 
-      throw new IllegalStateException(msg);
+         throw new IllegalStateException(msg);
+      }
    }
 
    protected void waitForBindings(final int node,
@@ -1865,7 +1859,7 @@
          serverFrom.getConfiguration().getConnectorConfigurations().put(serverTotc.getName(), serverTotc);
          pairs.add(serverTotc.getName());
       }
-      
+
 		ClusterConnectionConfiguration clusterConf = new ClusterConnectionConfiguration(
 				name, address, connectorFrom.getName(),
 				HornetQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE,



More information about the hornetq-commits mailing list