Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractLeaderElectionScenarioTest.java
index 63d9643..5d445bc 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.cluster.raft.behaviors;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.actor.Props;
@@ -25,7 +26,6 @@ import org.junit.After;
 import org.junit.Before;
 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
-import org.opendaylight.controller.cluster.raft.RaftActorContext;
 import org.opendaylight.controller.cluster.raft.RaftState;
 import org.opendaylight.controller.cluster.raft.TestActorFactory;
 import org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat;
@@ -57,23 +57,26 @@ public class AbstractLeaderElectionScenarioTest {
         @Override
         public void onReceive(Object message) throws Exception {
             // Ignore scheduled SendHeartBeat messages.
-            if(message instanceof SendHeartBeat) {
+            if (message instanceof SendHeartBeat) {
                 return;
             }
 
             try {
-                if(behavior != null && !dropMessagesToBehavior.containsKey(message.getClass())) {
-                    RaftActorBehavior oldBehavior = behavior;
-                    behavior = behavior.handleMessage(getSender(), message);
-                    if(behavior != oldBehavior && behaviorStateChangeLatch != null) {
-                        behaviorStateChangeLatch.countDown();
+                if (behavior != null && !dropMessagesToBehavior.containsKey(message.getClass())) {
+                    final RaftActorBehavior nextBehavior = behavior.handleMessage(getSender(), message);
+                    if (nextBehavior != null) {
+                        RaftActorBehavior oldBehavior = behavior;
+                        behavior = nextBehavior;
+                        if (behavior != oldBehavior && behaviorStateChangeLatch != null) {
+                            behaviorStateChangeLatch.countDown();
+                        }
                     }
                 }
             } finally {
                 super.onReceive(message);
 
                 CountDownLatch latch = messagesReceivedLatches.get(message.getClass());
-                if(latch != null) {
+                if (latch != null) {
                     latch.countDown();
                 }
             }
@@ -127,7 +130,7 @@ public class AbstractLeaderElectionScenarioTest {
         }
 
         void forwardCapturedMessagesToBehavior(Class<?> msgClass, ActorRef sender) throws Exception {
-            for(Object m: getAllMatching(getSelf(), msgClass)) {
+            for (Object m: getAllMatching(getSelf(), msgClass)) {
                 getSelf().tell(m, sender);
             }
         }
@@ -199,14 +202,34 @@ public class AbstractLeaderElectionScenarioTest {
         assertEquals(name + " behavior state", expState, actor.behavior.state());
     }
 
-    void initializeLeaderBehavior(MemberActor actor, RaftActorContext context, int numActiveFollowers) throws Exception {
+    void initializeLeaderBehavior(MemberActor actor, MockRaftActorContext context, int numActiveFollowers)
+            throws Exception {
         // Leader sends immediate heartbeats - we don't care about it so ignore it.
+        // Sometimes the initial AppendEntries messages go to dead letters, probably b/c the follower actors
+        // haven't been fully created/initialized by akka. So we try up to 3 times to create the Leader as
+        // a workaround.
+
+        Leader leader = null;
+        AssertionError lastAssertError = null;
+        for (int i = 1; i <= 3; i++) {
+            actor.expectMessageClass(AppendEntriesReply.class, numActiveFollowers);
+
+            leader = new Leader(context);
+            try {
+                actor.waitForExpectedMessages(AppendEntriesReply.class);
+                lastAssertError = null;
+                break;
+            } catch (AssertionError e) {
+                lastAssertError = e;
+            }
+        }
+
+        if (lastAssertError != null) {
+            throw lastAssertError;
+        }
 
-        actor.expectMessageClass(AppendEntriesReply.class, numActiveFollowers);
+        context.setCurrentBehavior(leader);
 
-        @SuppressWarnings("resource")
-        Leader leader = new Leader(context);
-        actor.waitForExpectedMessages(AppendEntriesReply.class);
         // Delay assignment here so the AppendEntriesReply isn't forwarded to the behavior.
         actor.behavior = leader;
 
@@ -216,8 +239,8 @@ public class AbstractLeaderElectionScenarioTest {
     }
 
     TestActorRef<MemberActor> newMemberActor(String name) throws Exception {
-        TestActorRef<MemberActor> actor = factory.createTestActor(MemberActor.props().
-                withDispatcher(Dispatchers.DefaultDispatcherId()), name);
+        TestActorRef<MemberActor> actor = factory.createTestActor(MemberActor.props()
+                .withDispatcher(Dispatchers.DefaultDispatcherId()), name);
         MessageCollectorActor.waitUntilReady(actor);
         return actor;
     }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.