Merge "Serialization/Deserialization and a host of other fixes"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / LeaderTest.java
1 package org.opendaylight.controller.cluster.raft.behaviors;
2
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
6 import junit.framework.Assert;
7 import org.junit.Test;
8 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
9 import org.opendaylight.controller.cluster.raft.RaftActorContext;
10 import org.opendaylight.controller.cluster.raft.RaftState;
11 import org.opendaylight.controller.cluster.raft.internal.messages.ApplyState;
12 import org.opendaylight.controller.cluster.raft.internal.messages.Replicate;
13 import org.opendaylight.controller.cluster.raft.internal.messages.SendHeartBeat;
14 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
15 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import static org.junit.Assert.assertEquals;
21
22 public class LeaderTest extends AbstractRaftActorBehaviorTest {
23
24     private ActorRef leaderActor =
25         getSystem().actorOf(Props.create(DoNothingActor.class));
26     private ActorRef senderActor =
27         getSystem().actorOf(Props.create(DoNothingActor.class));
28
29     @Test
30     public void testHandleMessageForUnknownMessage() throws Exception {
31         new JavaTestKit(getSystem()) {{
32             Leader leader =
33                 new Leader(createActorContext());
34
35             // handle message should return the Leader state when it receives an
36             // unknown message
37             RaftState state = leader.handleMessage(senderActor, "foo");
38             Assert.assertEquals(RaftState.Leader, state);
39         }};
40     }
41
42
43     @Test
44     public void testThatLeaderSendsAHeartbeatMessageToAllFollowers() {
45         new JavaTestKit(getSystem()) {{
46
47             new Within(duration("1 seconds")) {
48                 protected void run() {
49
50                     ActorRef followerActor = getTestActor();
51
52                     MockRaftActorContext actorContext =
53                         (MockRaftActorContext) createActorContext();
54
55                     Map<String, String> peerAddresses = new HashMap();
56
57                     peerAddresses.put(followerActor.path().toString(),
58                         followerActor.path().toString());
59
60                     actorContext.setPeerAddresses(peerAddresses);
61
62                     Leader leader = new Leader(actorContext);
63                     leader.handleMessage(senderActor, new SendHeartBeat());
64
65                     final String out =
66                         new ExpectMsg<String>(duration("1 seconds"),
67                             "match hint") {
68                             // do not put code outside this method, will run afterwards
69                             protected String match(Object in) {
70                                 if (in instanceof AppendEntries) {
71                                     if (((AppendEntries) in).getTerm()
72                                         == 0) {
73                                         return "match";
74                                     }
75                                     return null;
76                                 } else {
77                                     throw noMatch();
78                                 }
79                             }
80                         }.get(); // this extracts the received message
81
82                     assertEquals("match", out);
83
84                 }
85
86
87             };
88         }};
89     }
90
91     @Test
92     public void testHandleReplicateMessageSendAppendEntriesToFollower() {
93         new JavaTestKit(getSystem()) {{
94
95             new Within(duration("1 seconds")) {
96                 protected void run() {
97
98                     ActorRef followerActor = getTestActor();
99
100                     MockRaftActorContext actorContext =
101                         (MockRaftActorContext) createActorContext();
102
103                     Map<String, String> peerAddresses = new HashMap();
104
105                     peerAddresses.put(followerActor.path().toString(),
106                         followerActor.path().toString());
107
108                     actorContext.setPeerAddresses(peerAddresses);
109
110                     Leader leader = new Leader(actorContext);
111                     RaftState raftState = leader
112                         .handleMessage(senderActor, new Replicate(null, null,
113                             new MockRaftActorContext.MockReplicatedLogEntry(1,
114                                 100,
115                                 "foo")
116                         ));
117
118                     // State should not change
119                     assertEquals(RaftState.Leader, raftState);
120
121                     final String out =
122                         new ExpectMsg<String>(duration("1 seconds"),
123                             "match hint") {
124                             // do not put code outside this method, will run afterwards
125                             protected String match(Object in) {
126                                 if (in instanceof AppendEntries) {
127                                     if (((AppendEntries) in).getTerm()
128                                         == 0) {
129                                         return "match";
130                                     }
131                                     return null;
132                                 } else {
133                                     throw noMatch();
134                                 }
135                             }
136                         }.get(); // this extracts the received message
137
138                     assertEquals("match", out);
139
140                 }
141
142
143             };
144         }};
145     }
146
147     @Test
148     public void testHandleReplicateMessageWhenThereAreNoFollowers() {
149         new JavaTestKit(getSystem()) {{
150
151             new Within(duration("1 seconds")) {
152                 protected void run() {
153
154                     ActorRef raftActor = getTestActor();
155
156                     MockRaftActorContext actorContext =
157                         new MockRaftActorContext("test", getSystem(), raftActor);
158
159                     Leader leader = new Leader(actorContext);
160                     RaftState raftState = leader
161                         .handleMessage(senderActor, new Replicate(null, "state-id",
162                             new MockRaftActorContext.MockReplicatedLogEntry(1,
163                                 100,
164                                 "foo")
165                         ));
166
167                     // State should not change
168                     assertEquals(RaftState.Leader, raftState);
169
170                     assertEquals(100, actorContext.getCommitIndex());
171
172                     final String out =
173                         new ExpectMsg<String>(duration("1 seconds"),
174                             "match hint") {
175                             // do not put code outside this method, will run afterwards
176                             protected String match(Object in) {
177                                 if (in instanceof ApplyState) {
178                                     if (((ApplyState) in).getIdentifier().equals("state-id")) {
179                                         return "match";
180                                     }
181                                     return null;
182                                 } else {
183                                     throw noMatch();
184                                 }
185                             }
186                         }.get(); // this extracts the received message
187
188                     assertEquals("match", out);
189
190                 }
191
192
193             };
194         }};
195     }
196
197     @Override protected RaftActorBehavior createBehavior(
198         RaftActorContext actorContext) {
199         return new Leader(actorContext);
200     }
201
202     @Override protected RaftActorContext createActorContext() {
203         return new MockRaftActorContext("test", getSystem(), leaderActor);
204     }
205 }