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

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