[jbosscache-commits] JBoss Cache SVN: r5910 - core/trunk/src/test/java/org/jboss/cache/util/internals.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu May 29 08:20:13 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-05-29 08:20:13 -0400 (Thu, 29 May 2008)
New Revision: 5910

Modified:
   core/trunk/src/test/java/org/jboss/cache/util/internals/ReplicationListener.java
Log:
1.  Genericized
2.  Added an additional wait() method to allow for more detailed time units

Modified: core/trunk/src/test/java/org/jboss/cache/util/internals/ReplicationListener.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/internals/ReplicationListener.java	2008-05-29 12:01:48 UTC (rev 5909)
+++ core/trunk/src/test/java/org/jboss/cache/util/internals/ReplicationListener.java	2008-05-29 12:20:13 UTC (rev 5910)
@@ -20,8 +20,9 @@
 /**
  * Utility class that notifies when certain commands were asynchronously replicated on secondary cache.
  * Especially useful for avaoiding Thread.sleep() statements.
+ * <p/>
+ * Usage:
  * <pre>
- * Usage:
  *   Cache c1, c2; //these being two async caches
  *   AsyncReplicationListener listener2 = new AsyncReplicationListener(c2);
  *   listener2.expect(PutKeyValueCommand.class);
@@ -30,7 +31,7 @@
  * </pre>
  * Lifecycle - after being used (i.e. waitForReplicationToOccur returns sucessfully) the object returns to the
  * non-initialized state and *can* be reused through expect-wait cycle.
- * Note:  this class might be used aswell for sync caches, though it does not really make sence using it in these scenarios  
+ * Note:  this class might be used aswell for sync caches, though it does not really make sence using it in these scenarios
  *
  * @author Mircea.Markus at jboss.com
  * @since 2.2
@@ -38,7 +39,7 @@
 public class ReplicationListener
 {
    private CountDownLatch latch = new CountDownLatch(1);
-   private Set expectedCommands;
+   private Set<Class<? extends ReplicableCommand>> expectedCommands;
 
    /**
     * Builds a listener that will observe the given cache for recieving replication commands.
@@ -74,10 +75,10 @@
          if (result instanceof ReplicateCommand && expectedCommands != null)
          {
             ReplicateCommand replicateCommand = (ReplicateCommand) result;
-            Iterator it = expectedCommands.iterator();
+            Iterator<Class<? extends ReplicableCommand>> it = expectedCommands.iterator();
             while (it.hasNext())
             {
-               Class<? extends ReplicableCommand> replicableCommandClass = (Class<? extends ReplicableCommand>) it.next();
+               Class<? extends ReplicableCommand> replicableCommandClass = it.next();
                if (replicateCommand.containsCommandType(replicableCommandClass))
                {
                   it.remove();
@@ -103,20 +104,35 @@
    /**
     * Blocks for the elements specified through {@link #expect(Class[])} invocations to be replicated in this cache.
     * if replication does not occur in the give timeout then an exception is being thrown.
+    *
+    * @param timeoutMillis timeout in milliseconds
     */
    public void waitForReplicationToOccur(long timeoutMillis)
    {
+      waitForReplicationToOccur(timeoutMillis, TimeUnit.MILLISECONDS);
+   }
+
+   /**
+    * Similar to {@link #waitForReplicationToOccur(long)} except that this method provides more flexibility in time units.
+    *
+    * @param timeout  the maximum time to wait
+    * @param timeUnit the time unit of the <tt>timeout</tt> argument.
+    */
+   public void waitForReplicationToOccur(long timeout, TimeUnit timeUnit)
+   {
       assert expectedCommands != null : "there are no replication expectations; please use AsyncReplicationListener.expect(...) before calling this method";
       try
       {
-         if (!latch.await(timeoutMillis, TimeUnit.MILLISECONDS))
+         if (!latch.await(timeout, timeUnit))
          {
-            assert false : "waiting for more than " + timeoutMillis + " millis and following commands did not replicate: " + expectedCommands;
+            assert false : "waiting for more than " + timeout + " " + timeUnit + " and following commands did not replicate: " + expectedCommands;
          }
-      } catch (InterruptedException e)
+      }
+      catch (InterruptedException e)
       {
          throw new IllegalStateException("unexpected", e);
-      } finally
+      }
+      finally
       {
          expectedCommands = null;
          latch = new CountDownLatch(1);
@@ -131,7 +147,7 @@
    {
       if (this.expectedCommands == null)
       {
-         this.expectedCommands = new HashSet();
+         this.expectedCommands = new HashSet<Class<? extends ReplicableCommand>>();
       }
       this.expectedCommands.addAll(Arrays.asList(expectedCommands));
    }




More information about the jbosscache-commits mailing list