d0497f3d0a0727cf5a86a5b7579e827c8acd0cc9
[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.testkit.JavaTestKit;
4 import junit.framework.Assert;
5 import org.junit.Test;
6 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
7 import org.opendaylight.controller.cluster.raft.RaftActorContext;
8 import org.opendaylight.controller.cluster.raft.RaftState;
9 import org.opendaylight.controller.cluster.raft.internal.messages.SendHeartBeat;
10 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
11
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15
16 import static org.junit.Assert.assertEquals;
17
18 public class LeaderTest extends AbstractRaftActorBehaviorTest {
19
20     @Test
21     public void testHandleMessageForUnknownMessage() throws Exception {
22         new JavaTestKit(getSystem()) {{
23             Leader leader =
24                 new Leader(new MockRaftActorContext(), Collections.EMPTY_LIST);
25
26             // handle message should return the Leader state when it receives an
27             // unknown message
28             RaftState state = leader.handleMessage(getRef(), "foo");
29             Assert.assertEquals(RaftState.Leader, state);
30         }};
31     }
32
33
34     @Test
35     public void testThatLeaderSendsAHeartbeatMessageToAllFollowers(){
36         new JavaTestKit(getSystem()) {{
37
38             new Within(duration("1 seconds")) {
39                 protected void run() {
40
41                     List<String> followers = new ArrayList();
42
43                     followers.add(getTestActor().path().toString());
44
45                     Leader leader = new Leader(new MockRaftActorContext("test", getSystem(), getTestActor()), followers);
46                     leader.handleMessage(getRef(), new SendHeartBeat());
47
48                     final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
49                         // do not put code outside this method, will run afterwards
50                         protected String match(Object in) {
51                             if (in instanceof AppendEntries) {
52                                 if (((AppendEntries) in).getTerm()
53                                     == 0) {
54                                     return "match";
55                                 }
56                                 return null;
57                             } else {
58                                 throw noMatch();
59                             }
60                         }
61                     }.get(); // this extracts the received message
62
63                     assertEquals("match", out);
64
65                 }
66
67
68             };
69         }};
70     }
71
72     @Override protected RaftActorBehavior createBehavior(RaftActorContext actorContext) {
73         return new Leader(actorContext, Collections.EMPTY_LIST);
74     }
75 }

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