2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore.entityownership;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.anyBoolean;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.reset;
16 import static org.mockito.Mockito.timeout;
17 import static org.mockito.Mockito.verify;
18 import akka.actor.ActorContext;
19 import akka.actor.ActorRef;
20 import akka.actor.Props;
21 import akka.testkit.JavaTestKit;
22 import akka.testkit.TestActorRef;
23 import com.google.common.util.concurrent.Uninterruptibles;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
31 import org.opendaylight.controller.cluster.raft.TestActorFactory;
32 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
33 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
35 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import scala.collection.Iterator;
39 import scala.collection.immutable.Iterable;
42 * Unit tests for EntityOwnershipListenerSupport.
44 * @author Thomas Pantelis
46 public class EntityOwnershipListenerSupportTest extends AbstractActorTest {
47 private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
48 private ActorContext actorContext;
52 TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
53 Props.create(DoNothingActor.class), actorFactory.generateActorId("test"));
55 actorContext = actor.underlyingActor().getContext();
59 public void tearDown() {
64 public void testNotifyEntityOwnershipListeners() {
65 EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
67 EntityOwnershipListener mockListener1 = mock(EntityOwnershipListener.class, "EntityOwnershipListener1");
68 EntityOwnershipListener mockListener2 = mock(EntityOwnershipListener.class, "EntityOwnershipListener2");
69 EntityOwnershipListener mockListener3 = mock(EntityOwnershipListener.class, "EntityOwnershipListener3");
70 Entity entity1 = new Entity("type1", YangInstanceIdentifier.of(QName.create("test", "id1")));
71 Entity entity2 = new Entity("type1", YangInstanceIdentifier.of(QName.create("test", "id2")));
72 Entity entity3 = new Entity("type1", YangInstanceIdentifier.of(QName.create("test", "id3")));
73 Entity entity4 = new Entity("type2", YangInstanceIdentifier.of(QName.create("test", "id4")));
74 Entity entity5 = new Entity("noListener", YangInstanceIdentifier.of(QName.create("test", "id5")));
76 // Add EntityOwnershipListener registrations.
78 support.addEntityOwnershipListener(entity1, mockListener1);
79 support.addEntityOwnershipListener(entity1, mockListener1); // register again - should be noop
80 support.addEntityOwnershipListener(entity2, mockListener1);
81 support.addEntityOwnershipListener(entity1, mockListener2);
82 support.addEntityOwnershipListener(entity1.getType(), mockListener3);
84 // Notify entity1 changed and verify listeners are notified.
86 support.notifyEntityOwnershipListeners(entity1, false, true);
88 verify(mockListener1, timeout(5000)).ownershipChanged(entity1, false, true);
89 verify(mockListener2, timeout(5000)).ownershipChanged(entity1, false, true);
90 verify(mockListener3, timeout(5000)).ownershipChanged(entity1, false, true);
91 assertEquals("# of listener actors", 3, actorContext.children().size());
93 // Notify entity2 changed and verify only mockListener1 and mockListener3 are notified.
95 support.notifyEntityOwnershipListeners(entity2, false, true);
97 verify(mockListener1, timeout(5000)).ownershipChanged(entity2, false, true);
98 verify(mockListener3, timeout(5000)).ownershipChanged(entity2, false, true);
99 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
100 verify(mockListener2, never()).ownershipChanged(eq(entity2), anyBoolean(), anyBoolean());
101 assertEquals("# of listener actors", 3, actorContext.children().size());
103 // Notify entity3 changed and verify only mockListener3 is notified.
105 support.notifyEntityOwnershipListeners(entity3, false, true);
107 verify(mockListener3, timeout(5000)).ownershipChanged(entity3, false, true);
108 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
109 verify(mockListener1, never()).ownershipChanged(eq(entity3), anyBoolean(), anyBoolean());
110 verify(mockListener2, never()).ownershipChanged(eq(entity3), anyBoolean(), anyBoolean());
112 // Notify entity4 changed and verify no listeners are notified.
114 support.notifyEntityOwnershipListeners(entity4, false, true);
116 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
117 verify(mockListener1, never()).ownershipChanged(eq(entity4), anyBoolean(), anyBoolean());
118 verify(mockListener2, never()).ownershipChanged(eq(entity4), anyBoolean(), anyBoolean());
119 verify(mockListener3, never()).ownershipChanged(eq(entity4), anyBoolean(), anyBoolean());
121 // Notify entity5 changed and verify no listener is notified.
123 support.notifyEntityOwnershipListeners(entity5, false, true);
125 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
126 verify(mockListener1, never()).ownershipChanged(eq(entity4), anyBoolean(), anyBoolean());
127 verify(mockListener2, never()).ownershipChanged(eq(entity4), anyBoolean(), anyBoolean());
128 verify(mockListener3, never()).ownershipChanged(eq(entity4), anyBoolean(), anyBoolean());
130 reset(mockListener1, mockListener2, mockListener3);
132 // Unregister mockListener1 for entity1, issue a change and verify only mockListeners 2 & 3 are notified.
134 support.removeEntityOwnershipListener(entity1, mockListener1);
135 support.notifyEntityOwnershipListeners(entity1, false, true);
137 verify(mockListener2, timeout(5000)).ownershipChanged(entity1, false, true);
138 verify(mockListener3, timeout(5000)).ownershipChanged(entity1, false, true);
139 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
140 verify(mockListener1, never()).ownershipChanged(eq(entity1), anyBoolean(), anyBoolean());
142 // Unregister mockListener3, issue a change for entity1 and verify only mockListeners2 is notified.
144 reset(mockListener1, mockListener2, mockListener3);
146 support.removeEntityOwnershipListener(entity1.getType(), mockListener3);
147 support.notifyEntityOwnershipListeners(entity1, false, true);
149 verify(mockListener2, timeout(5000)).ownershipChanged(entity1, false, true);
150 Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
151 verify(mockListener1, never()).ownershipChanged(eq(entity1), anyBoolean(), anyBoolean());
152 verify(mockListener3, never()).ownershipChanged(eq(entity1), anyBoolean(), anyBoolean());
154 // Completely unregister all listeners and verify their listener actors are destroyed.
156 Iterable<ActorRef> listenerActors = actorContext.children();
157 assertEquals("# of listener actors", 2, listenerActors.size());
159 List<JavaTestKit> watchers = new ArrayList<>();
160 for(Iterator<ActorRef> iter = listenerActors.iterator(); iter.hasNext();) {
161 JavaTestKit kit = new JavaTestKit(getSystem());
162 kit.watch(iter.next());
166 support.removeEntityOwnershipListener(entity2, mockListener1);
167 support.removeEntityOwnershipListener(entity2, mockListener1); // un-register again - shoild be noop
168 support.removeEntityOwnershipListener(entity1, mockListener2);
170 Iterator<ActorRef> iter = listenerActors.iterator();
171 for(JavaTestKit kit: watchers) {
172 kit.expectTerminated(JavaTestKit.duration("3 seconds"), iter.next());
175 assertEquals("# of listener actors", 0, actorContext.children().size());
177 // Re-register mockListener1 for entity1 and verify it is notified.
179 reset(mockListener1, mockListener2);
181 support.addEntityOwnershipListener(entity1, mockListener1);
183 support.notifyEntityOwnershipListeners(entity1, false, true);
185 verify(mockListener1, timeout(5000)).ownershipChanged(entity1, false, true);
186 verify(mockListener2, never()).ownershipChanged(eq(entity1), anyBoolean(), anyBoolean());
187 verify(mockListener3, never()).ownershipChanged(eq(entity1), anyBoolean(), anyBoolean());
189 // Quickly register and unregister mockListener2 - expecting no exceptions.
191 support.addEntityOwnershipListener(entity1, mockListener2);
192 support.removeEntityOwnershipListener(entity1, mockListener2);
196 public void testHasCandidateForEntity() {
197 EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
198 Entity entity = new Entity("type", YangInstanceIdentifier.of(QName.create("test", "id")));
200 assertEquals("hasCandidateForEntity", false, support.hasCandidateForEntity(entity));
202 support.addEntityOwnershipListener(entity, mock(EntityOwnershipListener.class));
203 assertEquals("hasCandidateForEntity", false, support.hasCandidateForEntity(entity));
205 EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
206 support.addEntityOwnershipListener(entity, candidate);
207 assertEquals("hasCandidateForEntity", true, support.hasCandidateForEntity(entity));
209 support.removeEntityOwnershipListener(entity, candidate);
210 assertEquals("hasCandidateForEntity", false, support.hasCandidateForEntity(entity));