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.TestActorRef;
17 import akka.testkit.javadsl.TestKit;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
21 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
22 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
23 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
24 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
25 import org.opendaylight.controller.cluster.notifications.RoleChanged;
26 import org.opendaylight.controller.cluster.raft.RaftState;
27 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
29 public class RoleChangeNotifierTest extends AbstractActorTest {
30 private TestKit testKit;
34 testKit = new TestKit(getSystem());
38 public void testHandleRegisterRoleChangeListener() {
39 String memberId = "testHandleRegisterRoleChangeListener";
40 ActorRef listenerActor = getSystem().actorOf(MessageCollectorActor.props());
42 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(getSystem(),
43 RoleChangeNotifier.getProps(memberId), memberId);
45 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
47 RegisterRoleChangeListenerReply reply = MessageCollectorActor.getFirstMatching(listenerActor,
48 RegisterRoleChangeListenerReply.class);
51 RoleChangeNotification notification = MessageCollectorActor.getFirstMatching(listenerActor,
52 RoleChangeNotification.class);
53 assertNull(notification);
57 public void testHandleRaftRoleChanged() {
58 String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
59 ActorRef listenerActor = getSystem().actorOf(MessageCollectorActor.props());
60 ActorRef shardActor = testKit.getTestActor();
62 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(getSystem(),
63 RoleChangeNotifier.getProps(memberId), memberId);
65 notifierTestActorRef.tell(
66 new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
68 // no notification should be sent as listener has not yet
70 assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
72 // listener registers after role has been changed, ensure we
73 // sent the latest role change after a reply
74 notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
76 RegisterRoleChangeListenerReply reply = MessageCollectorActor.getFirstMatching(listenerActor,
77 RegisterRoleChangeListenerReply.class);
80 RoleChangeNotification notification = MessageCollectorActor.getFirstMatching(listenerActor,
81 RoleChangeNotification.class);
82 assertNotNull(notification);
83 assertEquals(RaftState.Candidate.name(), notification.getOldRole());
84 assertEquals(RaftState.Leader.name(), notification.getNewRole());
88 public void testHandleLeaderStateChanged() {
89 String actorId = "testHandleLeaderStateChanged";
90 TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(getSystem(),
91 RoleChangeNotifier.getProps(actorId), actorId);
93 notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader1", (short) 5), ActorRef.noSender());
95 // listener registers after the sate has been changed, ensure we
96 // sent the latest state change after a reply
97 notifierTestActorRef.tell(new RegisterRoleChangeListener(), testKit.getRef());
99 testKit.expectMsgClass(RegisterRoleChangeListenerReply.class);
101 LeaderStateChanged leaderStateChanged = testKit.expectMsgClass(LeaderStateChanged.class);
102 assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
103 assertEquals("getLeaderId", "leader1", leaderStateChanged.getLeaderId());
104 assertEquals("getLeaderPayloadVersion", 5, leaderStateChanged.getLeaderPayloadVersion());
106 notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader2", (short) 6), ActorRef.noSender());
108 leaderStateChanged = testKit.expectMsgClass(LeaderStateChanged.class);
109 assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
110 assertEquals("getLeaderId", "leader2", leaderStateChanged.getLeaderId());
111 assertEquals("getLeaderPayloadVersion", 6, leaderStateChanged.getLeaderPayloadVersion());