1 package org.opendaylight.controller.cluster.datastore;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7 import akka.actor.ActorRef;
8 import akka.actor.Props;
9 import akka.testkit.JavaTestKit;
10 import akka.testkit.TestActorRef;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
13 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
14 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
15 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
16 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
17 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
18 import org.opendaylight.controller.cluster.notifications.RoleChanged;
19 import org.opendaylight.controller.cluster.raft.RaftState;
21 public class RoleChangeNotifierTest extends AbstractActorTest {
24 public void testHandleRegisterRoleChangeListener() throws Exception {
25 new JavaTestKit(getSystem()) {{
26 String memberId = "testHandleRegisterRoleChangeListener";
27 ActorRef listenerActor = getSystem().actorOf(Props.create(MessageCollectorActor.class));
29 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
30 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
32 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
34 RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
35 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
38 RoleChangeNotification notification = (RoleChangeNotification)
39 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
40 assertNull(notification);
46 public void testHandleRaftRoleChanged() throws Exception {
47 new JavaTestKit(getSystem()) {{
48 String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
49 ActorRef listenerActor = getSystem().actorOf(Props.create(MessageCollectorActor.class));
50 ActorRef shardActor = getTestActor();
52 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
53 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
55 notifierTestActorRef.tell(new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
57 // no notification should be sent as listener has not yet registered
58 assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
60 // listener registers after role has been changed, ensure we sent the latest role change after a reply
61 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
63 RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
64 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
67 RoleChangeNotification notification = (RoleChangeNotification)
68 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
69 assertNotNull(notification);
70 assertEquals(RaftState.Candidate.name(), notification.getOldRole());
71 assertEquals(RaftState.Leader.name(), notification.getNewRole());
78 public void testHandleLeaderStateChanged() throws Exception {
79 new JavaTestKit(getSystem()) {{
80 String actorId = "testHandleLeaderStateChanged";
81 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
82 getSystem(), RoleChangeNotifier.getProps(actorId), actorId);
84 notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader1", (short)5), ActorRef.noSender());
86 // listener registers after the sate has been changed, ensure we sent the latest state change after a reply
87 notifierTestActorRef.tell(new RegisterRoleChangeListener(), getRef());
89 expectMsgClass(RegisterRoleChangeListenerReply.class);
91 LeaderStateChanged leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
92 assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
93 assertEquals("getLeaderId", "leader1", leaderStateChanged.getLeaderId());
94 assertEquals("getLeaderPayloadVersion", 5, leaderStateChanged.getLeaderPayloadVersion());
96 notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader2", (short)6), ActorRef.noSender());
98 leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
99 assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
100 assertEquals("getLeaderId", "leader2", leaderStateChanged.getLeaderId());
101 assertEquals("getLeaderPayloadVersion", 6, leaderStateChanged.getLeaderPayloadVersion());