2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
15 import akka.actor.ActorRef;
16 import akka.testkit.JavaTestKit;
17 import akka.testkit.TestActorRef;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
20 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
21 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
22 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
23 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
24 import org.opendaylight.controller.cluster.notifications.RoleChanged;
25 import org.opendaylight.controller.cluster.raft.RaftState;
26 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
28 public class RoleChangeNotifierTest extends AbstractActorTest {
31 public void testHandleRegisterRoleChangeListener() throws Exception {
32 new JavaTestKit(getSystem()) {
34 String memberId = "testHandleRegisterRoleChangeListener";
35 ActorRef listenerActor = getSystem().actorOf(MessageCollectorActor.props());
37 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(getSystem(),
38 RoleChangeNotifier.getProps(memberId), memberId);
40 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
42 RegisterRoleChangeListenerReply reply = MessageCollectorActor.getFirstMatching(listenerActor,
43 RegisterRoleChangeListenerReply.class);
46 RoleChangeNotification notification = MessageCollectorActor.getFirstMatching(listenerActor,
47 RoleChangeNotification.class);
48 assertNull(notification);
54 public void testHandleRaftRoleChanged() throws Exception {
55 new JavaTestKit(getSystem()) {
57 String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
58 ActorRef listenerActor = getSystem().actorOf(MessageCollectorActor.props());
59 ActorRef shardActor = getTestActor();
61 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(getSystem(),
62 RoleChangeNotifier.getProps(memberId), memberId);
64 notifierTestActorRef.tell(
65 new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
67 // no notification should be sent as listener has not yet
69 assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
71 // listener registers after role has been changed, ensure we
72 // sent the latest role change after a reply
73 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
75 RegisterRoleChangeListenerReply reply = MessageCollectorActor.getFirstMatching(listenerActor,
76 RegisterRoleChangeListenerReply.class);
79 RoleChangeNotification notification = MessageCollectorActor.getFirstMatching(listenerActor,
80 RoleChangeNotification.class);
81 assertNotNull(notification);
82 assertEquals(RaftState.Candidate.name(), notification.getOldRole());
83 assertEquals(RaftState.Leader.name(), notification.getNewRole());
91 public void testHandleLeaderStateChanged() throws Exception {
92 new JavaTestKit(getSystem()) {
94 String actorId = "testHandleLeaderStateChanged";
95 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(getSystem(),
96 RoleChangeNotifier.getProps(actorId), actorId);
98 notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader1", (short) 5), ActorRef.noSender());
100 // listener registers after the sate has been changed, ensure we
101 // sent the latest state change after a reply
102 notifierTestActorRef.tell(new RegisterRoleChangeListener(), getRef());
104 expectMsgClass(RegisterRoleChangeListenerReply.class);
106 LeaderStateChanged leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
107 assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
108 assertEquals("getLeaderId", "leader1", leaderStateChanged.getLeaderId());
109 assertEquals("getLeaderPayloadVersion", 5, leaderStateChanged.getLeaderPayloadVersion());
111 notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader2", (short) 6), ActorRef.noSender());
113 leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
114 assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
115 assertEquals("getLeaderId", "leader2", leaderStateChanged.getLeaderId());
116 assertEquals("getLeaderPayloadVersion", 6, leaderStateChanged.getLeaderPayloadVersion());