2 * Copyright (c) 2014 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;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
16 import akka.actor.ActorRef;
17 import akka.actor.ActorSystem;
18 import akka.actor.Props;
19 import akka.actor.Terminated;
20 import akka.dispatch.ExecutionContexts;
21 import akka.dispatch.Futures;
22 import akka.testkit.JavaTestKit;
23 import akka.util.Timeout;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import com.google.common.util.concurrent.Uninterruptibles;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.cluster.datastore.config.Configuration;
32 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
33 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
34 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
35 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
36 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
37 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
40 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
41 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
42 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
43 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
44 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
45 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
46 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataChangeListener;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import scala.concurrent.ExecutionContextExecutor;
50 import scala.concurrent.Future;
51 import scala.concurrent.duration.FiniteDuration;
54 * Unit tests for DataChangeListenerRegistrationProxy.
56 * @author Thomas Pantelis
58 public class DataChangeListenerRegistrationProxyTest extends AbstractActorTest {
60 @SuppressWarnings("unchecked")
61 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockListener =
62 Mockito.mock(AsyncDataChangeListener.class);
65 public void testGetInstance() throws Exception {
66 try (DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
67 "shard", Mockito.mock(ActorContext.class), mockListener)) {
68 Assert.assertEquals(mockListener, proxy.getInstance());
72 @Test(timeout = 10000)
73 public void testSuccessfulRegistration() {
74 new JavaTestKit(getSystem()) {
76 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
77 mock(ClusterWrapper.class), mock(Configuration.class));
79 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
80 "shard-1", actorContext, mockListener);
82 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
83 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
84 new Thread(() -> proxy.init(path, scope)).start();
86 FiniteDuration timeout = duration("5 seconds");
87 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
88 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
90 reply(new LocalShardFound(getRef()));
92 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
93 Assert.assertEquals("getPath", path, registerMsg.getPath());
94 Assert.assertEquals("getScope", scope, registerMsg.getScope());
95 Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
97 reply(new RegisterDataTreeNotificationListenerReply(getRef()));
99 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
100 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
103 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
104 proxy.getListenerRegistrationActor());
106 watch(proxy.getDataChangeListenerActor());
110 // The listener registration actor should get a Close message
111 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
113 // The DataChangeListener actor should be terminated
114 expectMsgClass(timeout, Terminated.class);
123 @Test(timeout = 10000)
124 public void testSuccessfulRegistrationForClusteredListener() {
125 new JavaTestKit(getSystem()) {
127 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
128 mock(ClusterWrapper.class), mock(Configuration.class));
130 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockClusteredListener =
131 Mockito.mock(ClusteredDOMDataChangeListener.class);
133 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
134 "shard-1", actorContext, mockClusteredListener);
136 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
137 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
138 new Thread(() -> proxy.init(path, scope)).start();
140 FiniteDuration timeout = duration("5 seconds");
141 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
142 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
144 reply(new LocalShardFound(getRef()));
146 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
147 Assert.assertEquals("getPath", path, registerMsg.getPath());
148 Assert.assertEquals("getScope", scope, registerMsg.getScope());
149 Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
151 reply(new RegisterDataTreeNotificationListenerReply(getRef()));
153 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
154 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
157 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
158 proxy.getListenerRegistrationActor());
160 watch(proxy.getDataChangeListenerActor());
164 // The listener registration actor should get a Close message
165 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
167 // The DataChangeListener actor should be terminated
168 expectMsgClass(timeout, Terminated.class);
177 @Test(timeout = 10000)
178 public void testLocalShardNotFound() {
179 new JavaTestKit(getSystem()) {
181 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
182 mock(ClusterWrapper.class), mock(Configuration.class));
184 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
185 "shard-1", actorContext, mockListener);
187 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
188 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
189 new Thread(() -> proxy.init(path, scope)).start();
191 FiniteDuration timeout = duration("5 seconds");
192 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
193 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
195 reply(new LocalShardNotFound("shard-1"));
197 expectNoMsg(duration("1 seconds"));
204 @Test(timeout = 10000)
205 public void testLocalShardNotInitialized() {
206 new JavaTestKit(getSystem()) {
208 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
209 mock(ClusterWrapper.class), mock(Configuration.class));
211 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
212 "shard-1", actorContext, mockListener);
214 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
215 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
216 new Thread(() -> proxy.init(path, scope)).start();
218 FiniteDuration timeout = duration("5 seconds");
219 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
220 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
222 reply(new NotInitializedException("not initialized"));
224 new Within(duration("1 seconds")) {
226 protected void run() {
237 public void testFailedRegistration() {
238 new JavaTestKit(getSystem()) {
240 ActorSystem mockActorSystem = mock(ActorSystem.class);
242 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
243 "testFailedRegistration");
244 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
245 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
246 MoreExecutors.directExecutor());
249 ActorContext actorContext = mock(ActorContext.class);
251 doReturn(executor).when(actorContext).getClientDispatcher();
253 String shardName = "shard-1";
254 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
255 shardName, actorContext, mockListener);
257 doReturn(mockActorSystem).when(actorContext).getActorSystem();
258 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
259 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
260 doReturn(Futures.failed(new RuntimeException("mock")))
261 .when(actorContext).executeOperationAsync(any(ActorRef.class),
262 any(Object.class), any(Timeout.class));
263 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
265 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
266 AsyncDataBroker.DataChangeScope.ONE);
268 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
276 public void testCloseBeforeRegistration() {
277 new JavaTestKit(getSystem()) {
279 ActorContext actorContext = mock(ActorContext.class);
281 String shardName = "shard-1";
282 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
283 shardName, actorContext, mockListener);
285 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
286 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
287 doReturn(getSystem()).when(actorContext).getActorSystem();
288 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
289 doReturn(getSystem().actorSelection(getRef().path()))
290 .when(actorContext).actorSelection(getRef().path());
291 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
292 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
294 Answer<Future<Object>> answer = invocation -> {
296 return Futures.successful((Object)new RegisterDataTreeNotificationListenerReply(getRef()));
299 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
300 any(Object.class), any(Timeout.class));
302 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
303 AsyncDataBroker.DataChangeScope.ONE);
305 expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
307 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());