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.RegisterChangeListenerReply;
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;
87 proxy.init(path, scope);
92 FiniteDuration timeout = duration("5 seconds");
93 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
94 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
96 reply(new LocalShardFound(getRef()));
98 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
99 Assert.assertEquals("getPath", path, registerMsg.getPath());
100 Assert.assertEquals("getScope", scope, registerMsg.getScope());
101 Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
103 reply(new RegisterChangeListenerReply(getRef()));
105 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
106 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
109 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
110 proxy.getListenerRegistrationActor());
112 watch(proxy.getDataChangeListenerActor());
116 // The listener registration actor should get a Close message
117 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
119 // The DataChangeListener actor should be terminated
120 expectMsgClass(timeout, Terminated.class);
129 @Test(timeout = 10000)
130 public void testSuccessfulRegistrationForClusteredListener() {
131 new JavaTestKit(getSystem()) {
133 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
134 mock(ClusterWrapper.class), mock(Configuration.class));
136 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockClusteredListener =
137 Mockito.mock(ClusteredDOMDataChangeListener.class);
139 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
140 "shard-1", actorContext, mockClusteredListener);
142 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
143 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
147 proxy.init(path, scope);
152 FiniteDuration timeout = duration("5 seconds");
153 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
154 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
156 reply(new LocalShardFound(getRef()));
158 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
159 Assert.assertEquals("getPath", path, registerMsg.getPath());
160 Assert.assertEquals("getScope", scope, registerMsg.getScope());
161 Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
163 reply(new RegisterChangeListenerReply(getRef()));
165 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
166 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
169 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
170 proxy.getListenerRegistrationActor());
172 watch(proxy.getDataChangeListenerActor());
176 // The listener registration actor should get a Close message
177 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
179 // The DataChangeListener actor should be terminated
180 expectMsgClass(timeout, Terminated.class);
189 @Test(timeout = 10000)
190 public void testLocalShardNotFound() {
191 new JavaTestKit(getSystem()) {
193 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
194 mock(ClusterWrapper.class), mock(Configuration.class));
196 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
197 "shard-1", actorContext, mockListener);
199 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
200 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
204 proxy.init(path, scope);
209 FiniteDuration timeout = duration("5 seconds");
210 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
211 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
213 reply(new LocalShardNotFound("shard-1"));
215 expectNoMsg(duration("1 seconds"));
222 @Test(timeout = 10000)
223 public void testLocalShardNotInitialized() {
224 new JavaTestKit(getSystem()) {
226 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
227 mock(ClusterWrapper.class), mock(Configuration.class));
229 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
230 "shard-1", actorContext, mockListener);
232 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
233 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
237 proxy.init(path, scope);
242 FiniteDuration timeout = duration("5 seconds");
243 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
244 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
246 reply(new NotInitializedException("not initialized"));
248 new Within(duration("1 seconds")) {
250 protected void run() {
261 public void testFailedRegistration() {
262 new JavaTestKit(getSystem()) {
264 ActorSystem mockActorSystem = mock(ActorSystem.class);
266 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
267 "testFailedRegistration");
268 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
269 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
270 MoreExecutors.directExecutor());
273 ActorContext actorContext = mock(ActorContext.class);
275 doReturn(executor).when(actorContext).getClientDispatcher();
277 String shardName = "shard-1";
278 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
279 shardName, actorContext, mockListener);
281 doReturn(mockActorSystem).when(actorContext).getActorSystem();
282 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
283 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
284 doReturn(Futures.failed(new RuntimeException("mock")))
285 .when(actorContext).executeOperationAsync(any(ActorRef.class),
286 any(Object.class), any(Timeout.class));
287 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
289 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
290 AsyncDataBroker.DataChangeScope.ONE);
292 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
300 public void testCloseBeforeRegistration() {
301 new JavaTestKit(getSystem()) {
303 ActorContext actorContext = mock(ActorContext.class);
305 String shardName = "shard-1";
306 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
307 shardName, actorContext, mockListener);
309 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
310 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
311 doReturn(getSystem()).when(actorContext).getActorSystem();
312 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
313 doReturn(getSystem().actorSelection(getRef().path()))
314 .when(actorContext).actorSelection(getRef().path());
315 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
316 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
318 Answer<Future<Object>> answer = invocation -> {
320 return Futures.successful((Object)new RegisterChangeListenerReply(getRef()));
323 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
324 any(Object.class), any(Timeout.class));
326 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
327 AsyncDataBroker.DataChangeScope.ONE);
329 expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
331 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());