1 package org.opendaylight.controller.cluster.datastore;
4 import akka.actor.ActorRef;
5 import akka.actor.Props;
6 import akka.testkit.JavaTestKit;
7 import akka.testkit.TestActorRef;
9 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
10 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
11 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
12 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
13 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
14 import org.opendaylight.controller.cluster.notifications.RoleChanged;
15 import org.opendaylight.controller.cluster.raft.RaftState;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
20 public class RoleChangeNotifierTest extends AbstractActorTest {
23 public void testHandleRegisterRoleChangeListener() throws Exception {
24 new JavaTestKit(getSystem()) {{
25 String memberId = "testHandleRegisterRoleChangeListener";
26 ActorRef listenerActor = getSystem().actorOf(Props.create(MessageCollectorActor.class));
28 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
29 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
31 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
33 RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
34 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
37 RoleChangeNotification notification = (RoleChangeNotification)
38 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
39 assertNull(notification);
45 public void testHandleRaftRoleChanged() throws Exception {
46 new JavaTestKit(getSystem()) {{
47 String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
48 ActorRef listenerActor = getSystem().actorOf(Props.create(MessageCollectorActor.class));
49 ActorRef shardActor = getTestActor();
51 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
52 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
54 RoleChangeNotifier roleChangeNotifier = notifierTestActorRef.underlyingActor();
56 notifierTestActorRef.tell(new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
58 // no notification should be sent as listener has not yet registered
59 assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
61 // listener registers after role has been changed, ensure we sent the latest role change after a reply
62 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
64 RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
65 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
68 RoleChangeNotification notification = (RoleChangeNotification)
69 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
70 assertNotNull(notification);
71 assertEquals(RaftState.Candidate.name(), notification.getOldRole());
72 assertEquals(RaftState.Leader.name(), notification.getNewRole());