X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FAbstractLeaderElectionScenarioTest.java;h=5d445bc6a556dd22c6bc1f1b8137bdf8e66a92c7;hp=31f2d629641fd92820c549c9dcaf2a7eb3a08beb;hb=e1eca73a5ae2ffae8dd78c6fe5281cd2f45d5ef3;hpb=24ace09aacc620fd9768e0a7004e802f9385bcfc diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeaderElectionScenarioTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeaderElectionScenarioTest.java index 31f2d62964..5d445bc6a5 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeaderElectionScenarioTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeaderElectionScenarioTest.java @@ -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; @@ -56,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(); } } @@ -126,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); } } @@ -198,15 +202,34 @@ public class AbstractLeaderElectionScenarioTest { assertEquals(name + " behavior state", expState, actor.behavior.state()); } - void initializeLeaderBehavior(MemberActor actor, MockRaftActorContext 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); - 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; + } - Leader leader = new Leader(context); context.setCurrentBehavior(leader); - 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 newMemberActor(String name) throws Exception { - TestActorRef actor = factory.createTestActor(MemberActor.props(). - withDispatcher(Dispatchers.DefaultDispatcherId()), name); + TestActorRef actor = factory.createTestActor(MemberActor.props() + .withDispatcher(Dispatchers.DefaultDispatcherId()), name); MessageCollectorActor.waitUntilReady(actor); return actor; }