Fix RaftActorTest.testRaftRoleChangeNotifier failure 47/15047/3
authortpantelis <tpanteli@brocade.com>
Fri, 20 Feb 2015 06:47:58 +0000 (01:47 -0500)
committertpantelis <tpanteli@brocade.com>
Fri, 20 Feb 2015 08:13:42 +0000 (03:13 -0500)
I saw this test fail once:

   java.lang.AssertionError: expected:<3> but was:<2>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)
        at org.junit.Assert.assertEquals(Assert.java:542)
        at
org.opendaylight.controller.cluster.raft.RaftActorTest$13.<init>(RaftActorTest.java:894)
        at
org.opendaylight.controller.cluster.raft.RaftActorTest.testRaftRoleChangeNotifier(RaftActorTest.java:881)

The test has an arbitrary 2 sec wait before retrieving the collected
RoleChanged messages. With the election timeout and arbitrary GC and threading
delays, 2 sec may not be enough.

Arbitrary sleeps in tests are usually trouble - I made it deterministic
by waiting 5 sec for the expected messages.

Change-Id: I51c24a5844321bbd000a0460777d08b51a5f82c8
Signed-off-by: tpantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTest.java

index ba8f49d8f6249b5ae3b4340128eeea2a9dc1bc3e..c9145e56a854fd758f66cdcf9cae497ba5ee5e02 100644 (file)
@@ -892,17 +892,28 @@ public class RaftActorTest extends AbstractActorTest {
     public void testRaftRoleChangeNotifier() throws Exception {
         new JavaTestKit(getSystem()) {{
             ActorRef notifierActor = factory.createActor(Props.create(MessageCollectorActor.class));
+            MessageCollectorActor.waitUntilReady(notifierActor);
+
             DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
+            long heartBeatInterval = 100;
+            config.setHeartBeatInterval(FiniteDuration.create(heartBeatInterval, TimeUnit.MILLISECONDS));
+            config.setElectionTimeoutFactor(1);
+
             String persistenceId = factory.generateActorId("notifier-");
 
             factory.createTestActor(MockRaftActor.props(persistenceId,
                     Collections.<String, String>emptyMap(), Optional.<ConfigParams>of(config), notifierActor), persistenceId);
 
-            // sleeping for a minimum of 2 seconds, if it spans more its fine.
-            Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
+            List<RoleChanged> matches =  null;
+            for(int i = 0; i < 5000 / heartBeatInterval; i++) {
+                matches = MessageCollectorActor.getAllMatching(notifierActor, RoleChanged.class);
+                assertNotNull(matches);
+                if(matches.size() == 3) {
+                    break;
+                }
+                Uninterruptibles.sleepUninterruptibly(heartBeatInterval, TimeUnit.MILLISECONDS);
+            }
 
-            List<RoleChanged> matches = MessageCollectorActor.getAllMatching(notifierActor, RoleChanged.class);
-            assertNotNull(matches);
             assertEquals(3, matches.size());
 
             // check if the notifier got a role change from null to Follower