X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FAbstractRaftActorBehaviorTest.java;h=ead6bb7bbdffdf09763ccfd7a67cb5aff1bc22fd;hb=e1eca73a5ae2ffae8dd78c6fe5281cd2f45d5ef3;hp=30ca63b0634a74c7ecbe0887ba0e8452f72b6176;hpb=24ace09aacc620fd9768e0a7004e802f9385bcfc;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehaviorTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehaviorTest.java index 30ca63b063..ead6bb7bbd 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehaviorTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehaviorTest.java @@ -10,6 +10,8 @@ package org.opendaylight.controller.cluster.raft.behaviors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + import akka.actor.ActorRef; import akka.actor.Props; import akka.testkit.TestActorRef; @@ -28,7 +30,6 @@ 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.ReplicatedLogEntry; -import org.opendaylight.controller.cluster.raft.SerializationUtils; import org.opendaylight.controller.cluster.raft.TestActorFactory; import org.opendaylight.controller.cluster.raft.messages.AppendEntries; import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply; @@ -51,7 +52,7 @@ public abstract class AbstractRaftActorBehaviorTest @After public void tearDown() throws Exception { - if(behavior != null) { + if (behavior != null) { behavior.close(); } @@ -61,8 +62,6 @@ public abstract class AbstractRaftActorBehaviorTest /** * This test checks that when a new Raft RPC message is received with a newer * term the RaftActor gets into the Follower state. - * - * @throws Exception */ @Test public void testHandleRaftRPCWithNewerTerm() throws Exception { @@ -85,9 +84,7 @@ public abstract class AbstractRaftActorBehaviorTest /** * This test verifies that when an AppendEntries is received with a term that * is less that the currentTerm of the RaftActor then the RaftActor does not - * change it's state and it responds back with a failure - * - * @throws Exception + * change it's state and it responds back with a failure. */ @Test public void testHandleAppendEntriesSenderTermLessThanReceiverTerm() throws Exception { @@ -102,12 +99,11 @@ public abstract class AbstractRaftActorBehaviorTest behavior = createBehavior(context); - // Send an unknown message so that the state of the RaftActor remains unchanged - RaftActorBehavior expected = behavior.handleMessage(behaviorActor, "unknown"); + RaftState expected = behavior.state(); RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries); - assertEquals("Raft state", expected.state(), raftBehavior.state()); + assertEquals("Raft state", expected, raftBehavior.state()); // Also expect an AppendEntriesReply to be sent where success is false @@ -132,18 +128,20 @@ public abstract class AbstractRaftActorBehaviorTest List entries = new ArrayList<>(); entries.add(new MockRaftActorContext.MockReplicatedLogEntry(2, 0, payload)); - AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short)0); + final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short)0); behavior = createBehavior(context); assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate); - // Send an unknown message so that the state of the RaftActor remains unchanged - RaftActorBehavior expected = behavior.handleMessage(behaviorActor, "unknown"); + RaftState expected = behavior.state(); + + // Check that the behavior does not handle unknwon message + assertNull(behavior.handleMessage(behaviorActor, "unknown")); RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries); - assertEquals("Raft state", expected.state(), raftBehavior.state()); + assertEquals("Raft state", expected, raftBehavior.state()); assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size()); @@ -180,7 +178,7 @@ public abstract class AbstractRaftActorBehaviorTest /** * This test verifies that when a RaftActor receives a RequestVote message * with a term that is greater than it's currentTerm but a less up-to-date - * log then the receiving RaftActor will not grant the vote to the sender + * log then the receiving RaftActor will not grant the vote to the sender. */ @Test public void testHandleRequestVoteWhenSenderLogLessUptoDate() { @@ -208,7 +206,7 @@ public abstract class AbstractRaftActorBehaviorTest /** * This test verifies that the receiving RaftActor will not grant a vote * to a sender if the sender's term is lesser than the currentTerm of the - * recipient RaftActor + * recipient RaftActor. */ @Test public void testHandleRequestVoteWhenSenderTermLessThanCurrentTerm() { @@ -256,7 +254,8 @@ public abstract class AbstractRaftActorBehaviorTest assertEquals(0, abstractBehavior.getReplicatedToAllIndex()); assertEquals(1, context.getReplicatedLog().size()); - //5 entries, lastApplied =2 and replicatedIndex = 3, but since we want to keep the lastapplied, indices 0 and 1 will only get purged + // 5 entries, lastApplied =2 and replicatedIndex = 3, but since we want to keep the lastapplied, indices 0 and + // 1 will only get purged context.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 5, 1).build()); context.setLastApplied(2); abstractBehavior.performSnapshotWithoutCapture(3); @@ -275,8 +274,8 @@ public abstract class AbstractRaftActorBehaviorTest protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(MockRaftActorContext actorContext, ActorRef actorRef, RaftRPC rpc) throws Exception { - Payload p = new MockRaftActorContext.MockPayload(""); - setLastLogEntry(actorContext, 1, 0, p); + Payload payload = new MockRaftActorContext.MockPayload(""); + setLastLogEntry(actorContext, 1, 0, payload); actorContext.getTermInformation().update(1, "test"); RaftActorBehavior origBehavior = createBehavior(actorContext); @@ -340,13 +339,9 @@ public abstract class AbstractRaftActorBehaviorTest return new RequestVoteReply(100, false); } - protected Object fromSerializableMessage(Object serializable){ - return SerializationUtils.fromSerializable(serializable); - } - protected ByteString toByteString(Map state) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try(ObjectOutputStream oos = new ObjectOutputStream(bos)) { + try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { oos.writeObject(state); return ByteString.copyFrom(bos.toByteArray()); } catch (IOException e) { @@ -359,7 +354,7 @@ public abstract class AbstractRaftActorBehaviorTest } protected RaftPolicy createRaftPolicy(final boolean automaticElectionsEnabled, - final boolean applyModificationToStateBeforeConsensus){ + final boolean applyModificationToStateBeforeConsensus) { return new RaftPolicy() { @Override public boolean automaticElectionsEnabled() {