From 8431d91d7350d8de826963f5ccdf8dc3d09ae0e3 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 21 Aug 2015 14:33:17 +0000
Subject: [PATCH] AssuredReplicationPluginTest.java: Used ReplicationTestCase.waitForSpecificMsg() and waitForSpecificMsgs() to ignore unrelated messages.

---
 opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java |   10 +++++-----
 opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java                 |   25 ++++++++++++++++++-------
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java
index c95e530..7a56c7d 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java
@@ -833,7 +833,7 @@
    * @return The expected message if it comes in time or fails (assertion).
    */
   protected static <T extends ReplicationMsg> T waitForSpecificMsg(Session session, Class<T> msgType) {
-    return waitForSpecificMsg(session, null, msgType);
+    return (T) waitForSpecificMsgs(session, (ReplicationBroker) null, msgType);
   }
 
   /**
@@ -844,14 +844,24 @@
    * @return The expected message if it comes in time or fails (assertion).
    */
   protected static <T extends ReplicationMsg> T waitForSpecificMsg(ReplicationBroker broker, Class<T> msgType) {
-    return waitForSpecificMsg(null, broker, msgType);
+    return (T) waitForSpecificMsgs(null, broker, msgType);
   }
 
-  protected static <T extends ReplicationMsg> T waitForSpecificMsg(Session session, ReplicationBroker broker, Class<T> msgType)
+  protected static ReplicationMsg waitForSpecificMsgs(Session session, Class<?>... msgTypes) {
+    return waitForSpecificMsgs(session, null, msgTypes);
+  }
+
+  protected static ReplicationMsg waitForSpecificMsgs(ReplicationBroker broker, Class<?>... msgTypes) {
+    return waitForSpecificMsgs(null, broker, msgTypes);
+  }
+
+  private static ReplicationMsg waitForSpecificMsgs(Session session, ReplicationBroker broker, Class<?>... msgTypes)
   {
     assertTrue(session != null || broker != null, "One of Session or ReplicationBroker parameter must not be null");
     assertTrue(session == null || broker == null, "Only one of Session or ReplicationBroker parameter must not be null");
 
+    List<Class<?>> msgTypes2 = Arrays.asList(msgTypes);
+
     final int timeOut = 5000; // 5 seconds max to wait for the desired message
     final long startTime = System.currentTimeMillis();
     final List<ReplicationMsg> msgs = new ArrayList<>();
@@ -872,21 +882,22 @@
       }
       catch (Exception ex)
       {
-        fail("Exception waiting for " + msgType + " message : "
+        ex.printStackTrace();
+        fail("Exception waiting for " + msgTypes2 + " message : "
             + ex.getClass().getName() + " : " + ex.getMessage());
       }
 
-      if (replMsg.getClass().equals(msgType))
+      if (msgTypes2.contains(replMsg.getClass()))
       {
         // Ok, got it, let's return the expected message
-        return (T) replMsg;
+        return replMsg;
       }
       logger.trace("waitForSpecificMsg received : " + replMsg);
       msgs.add(replMsg);
       timedOut = System.currentTimeMillis() - startTime > timeOut;
     }
     // Timeout
-    fail("Failed to receive an expected " + msgType + " message after 5 seconds."
+    fail("Failed to receive an expected " + msgTypes2 + " message after 5 seconds."
         + " Also received the following messages during wait time: " + msgs);
     return null;
   }
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java
index b8a0731..03ff434 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java
@@ -38,6 +38,7 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.ldap.ByteString;
+import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldap.SearchScope;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.core.AddOperation;
@@ -45,12 +46,10 @@
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.protocols.internal.InternalSearchOperation;
 import org.opends.server.protocols.internal.SearchRequest;
-import static org.opends.server.protocols.internal.Requests.*;
 import org.opends.server.replication.ReplicationTestCase;
 import org.opends.server.replication.common.*;
 import org.opends.server.replication.protocol.*;
 import org.opends.server.types.*;
-import org.forgerock.opendj.ldap.ResultCode;
 import org.opends.server.util.StaticUtils;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
@@ -58,6 +57,7 @@
 
 import static org.assertj.core.data.MapEntry.*;
 import static org.opends.server.TestCaseUtils.*;
+import static org.opends.server.protocols.internal.Requests.*;
 import static org.testng.Assert.*;
 
 /**
@@ -443,7 +443,7 @@
     {
       {
         // Receive server start
-        ServerStartMsg serverStartMsg = (ServerStartMsg) session.receive();
+        ServerStartMsg serverStartMsg = waitForSpecificMsg(session, ServerStartMsg.class);
 
         baseDN = serverStartMsg.getBaseDN();
         serverState = serverStartMsg.getServerState();
@@ -464,7 +464,7 @@
         }
 
         // Read start session or stop
-        ReplicationMsg msg = session.receive();
+        ReplicationMsg msg = waitForSpecificMsgs(session, StopMsg.class, ServerStartMsg.class);
         if (msg instanceof StopMsg){
           // Disconnection of DS looking for best server
           return false;
@@ -584,7 +584,7 @@
         session.publish(addMsg);
 
         // Read and return matching ack
-        return (AckMsg)session.receive();
+        return waitForSpecificMsg(session, AckMsg.class);
       }
     }
 

--
Gitblit v1.10.0