Fix intermittent unit test failures
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / RoleChangeNotifierTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import akka.actor.ActorRef;
16 import akka.actor.Props;
17 import akka.testkit.JavaTestKit;
18 import akka.testkit.TestActorRef;
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;
28
29 public class RoleChangeNotifierTest extends AbstractActorTest  {
30
31     @Test
32     public void testHandleRegisterRoleChangeListener() throws Exception {
33         new JavaTestKit(getSystem()) {{
34             String memberId = "testHandleRegisterRoleChangeListener";
35             ActorRef listenerActor =  getSystem().actorOf(Props.create(MessageCollectorActor.class));
36
37             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
38                 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
39
40             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
41
42             RegisterRoleChangeListenerReply reply =
43                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
44             assertNotNull(reply);
45
46             RoleChangeNotification notification =
47                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
48             assertNull(notification);
49         }};
50
51     }
52
53     @Test
54     public void testHandleRaftRoleChanged() throws Exception {
55         new JavaTestKit(getSystem()) {{
56             String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
57             ActorRef listenerActor =  getSystem().actorOf(Props.create(MessageCollectorActor.class));
58             ActorRef shardActor =  getTestActor();
59
60             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
61                 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
62
63             notifierTestActorRef.tell(new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
64
65             // no notification should be sent as listener has not yet registered
66             assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
67
68             // listener registers after role has been changed, ensure we sent the latest role change after a reply
69             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
70
71             RegisterRoleChangeListenerReply reply =
72                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
73             assertNotNull(reply);
74
75             RoleChangeNotification notification =
76                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
77             assertNotNull(notification);
78             assertEquals(RaftState.Candidate.name(), notification.getOldRole());
79             assertEquals(RaftState.Leader.name(), notification.getNewRole());
80
81         }};
82
83     }
84
85     @Test
86     public void testHandleLeaderStateChanged() throws Exception {
87         new JavaTestKit(getSystem()) {{
88             String actorId = "testHandleLeaderStateChanged";
89             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
90                 getSystem(), RoleChangeNotifier.getProps(actorId), actorId);
91
92             notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader1", (short)5), ActorRef.noSender());
93
94             // listener registers after the sate has been changed, ensure we sent the latest state change after a reply
95             notifierTestActorRef.tell(new RegisterRoleChangeListener(), getRef());
96
97             expectMsgClass(RegisterRoleChangeListenerReply.class);
98
99             LeaderStateChanged leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
100             assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
101             assertEquals("getLeaderId", "leader1", leaderStateChanged.getLeaderId());
102             assertEquals("getLeaderPayloadVersion", 5, leaderStateChanged.getLeaderPayloadVersion());
103
104             notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader2", (short)6), ActorRef.noSender());
105
106             leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
107             assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
108             assertEquals("getLeaderId", "leader2", leaderStateChanged.getLeaderId());
109             assertEquals("getLeaderPayloadVersion", 6, leaderStateChanged.getLeaderPayloadVersion());
110         }};
111     }
112 }
113
114