Remove RaftReplicator and move hearbeat logic to the leader
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractRaftActorBehaviorTest.java
1 package org.opendaylight.controller.cluster.raft.behaviors;
2
3 import akka.actor.ActorRef;
4 import akka.testkit.JavaTestKit;
5 import org.junit.Test;
6 import org.opendaylight.controller.cluster.raft.AbstractActorTest;
7 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
8 import org.opendaylight.controller.cluster.raft.RaftActorContext;
9 import org.opendaylight.controller.cluster.raft.RaftState;
10 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
11 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
12 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
13 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
14 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
15
16 import java.util.concurrent.atomic.AtomicLong;
17
18 import static org.junit.Assert.assertEquals;
19
20 public abstract class AbstractRaftActorBehaviorTest extends AbstractActorTest{
21    @Test
22     public void testHandlingOfRaftRPCWithNewerTerm() throws Exception {
23         new JavaTestKit(getSystem()) {{
24
25             assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(getTestActor(),
26                 createAppendEntriesWithNewerTerm());
27
28             assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(getTestActor(),
29                 createAppendEntriesReplyWithNewerTerm());
30
31             assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(getTestActor(),
32                 createRequestVoteWithNewerTerm());
33
34             assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(getTestActor(),
35                 createRequestVoteReplyWithNewerTerm());
36
37
38         }};
39     }
40
41     @Test
42     public void testHandlingOfAppendEntriesWithNewerCommitIndex() throws Exception{
43         new JavaTestKit(getSystem()) {{
44
45             RaftActorContext context =
46                 createActorContext();
47
48             ((MockRaftActorContext) context).setLastApplied(new AtomicLong(100));
49
50             AppendEntries appendEntries =
51                 new AppendEntries(100, "leader-1", 0, 0, null, 101);
52
53             RaftState raftState =
54                 createBehavior(context).handleMessage(getRef(), appendEntries);
55
56             assertEquals(new AtomicLong(101).get(), context.getLastApplied().get());
57
58         }};
59     }
60
61     protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(
62         ActorRef actorRef, RaftRPC rpc){
63         RaftState raftState = createBehavior()
64             .handleMessage(actorRef, rpc);
65
66         assertEquals(RaftState.Follower, raftState);
67     }
68
69     protected abstract RaftActorBehavior createBehavior(RaftActorContext actorContext);
70
71     protected RaftActorBehavior createBehavior(){
72         return createBehavior(createActorContext());
73     }
74
75     protected RaftActorContext createActorContext(){
76         return new MockRaftActorContext();
77     }
78
79     protected AppendEntries createAppendEntriesWithNewerTerm(){
80         return new AppendEntries(100, "leader-1", 0, 0, null, 1);
81     }
82
83     protected AppendEntriesReply createAppendEntriesReplyWithNewerTerm(){
84         return new AppendEntriesReply(100, false);
85     }
86
87     protected RequestVote createRequestVoteWithNewerTerm(){
88         return new RequestVote(100, "candidate-1", 10, 100);
89     }
90
91     protected RequestVoteReply createRequestVoteReplyWithNewerTerm(){
92         return new RequestVoteReply(100, false);
93     }
94
95 }

©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.