2 * Copyright (c) 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
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.javadsl.TestKit;
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.stubbing.Answer;
30 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
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.RegisterDataTreeChangeListener;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
40 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
43 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import scala.concurrent.ExecutionContextExecutor;
46 import scala.concurrent.Future;
47 import scala.concurrent.duration.FiniteDuration;
49 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
50 private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
52 @Test(timeout = 10000)
53 public void testSuccessfulRegistration() {
54 new TestKit(getSystem()) {
56 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
57 mock(Configuration.class));
59 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
60 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
61 actorContext, mockListener, path);
63 new Thread(() -> proxy.init("shard-1")).start();
65 FiniteDuration timeout = duration("5 seconds");
66 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
67 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
69 reply(new LocalShardFound(getRef()));
71 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout,
72 RegisterDataTreeChangeListener.class);
73 Assert.assertEquals("getPath", path, registerMsg.getPath());
74 Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
76 reply(new RegisterDataTreeNotificationListenerReply(getRef()));
78 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
79 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
82 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
83 proxy.getListenerRegistrationActor());
85 watch(proxy.getDataChangeListenerActor());
89 // The listener registration actor should get a Close message
90 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
92 // The DataChangeListener actor should be terminated
93 expectMsgClass(timeout, Terminated.class);
102 @Test(timeout = 10000)
103 public void testSuccessfulRegistrationForClusteredListener() {
104 new TestKit(getSystem()) {
106 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
107 mock(Configuration.class));
109 ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(
110 ClusteredDOMDataTreeChangeListener.class);
112 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
113 final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy =
114 new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener, path);
116 new Thread(() -> proxy.init("shard-1")).start();
118 FiniteDuration timeout = duration("5 seconds");
119 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
120 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
122 reply(new LocalShardFound(getRef()));
124 RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout,
125 RegisterDataTreeChangeListener.class);
126 Assert.assertEquals("getPath", path, registerMsg.getPath());
127 Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
134 @Test(timeout = 10000)
135 public void testLocalShardNotFound() {
136 new TestKit(getSystem()) {
138 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
139 mock(Configuration.class));
141 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
142 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
143 actorContext, mockListener, path);
145 new Thread(() -> proxy.init("shard-1")).start();
147 FiniteDuration timeout = duration("5 seconds");
148 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
149 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
151 reply(new LocalShardNotFound("shard-1"));
153 expectNoMsg(duration("1 seconds"));
160 @Test(timeout = 10000)
161 public void testLocalShardNotInitialized() {
162 new TestKit(getSystem()) {
164 ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class),
165 mock(Configuration.class));
167 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
168 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
169 actorContext, mockListener, path);
171 new Thread(() -> proxy.init("shard-1")).start();
173 FiniteDuration timeout = duration("5 seconds");
174 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
175 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
177 reply(new NotInitializedException("not initialized"));
179 within(duration("1 seconds"), () -> {
190 public void testFailedRegistration() {
191 new TestKit(getSystem()) {
193 ActorSystem mockActorSystem = mock(ActorSystem.class);
195 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration");
196 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
197 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
199 ActorContext actorContext = mock(ActorContext.class);
200 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
202 doReturn(executor).when(actorContext).getClientDispatcher();
203 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
204 doReturn(mockActorSystem).when(actorContext).getActorSystem();
206 String shardName = "shard-1";
207 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
208 actorContext, mockListener, path);
210 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
211 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
212 doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext)
213 .executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
214 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
216 proxy.init("shard-1");
218 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
226 public void testCloseBeforeRegistration() {
227 new TestKit(getSystem()) {
229 ActorContext actorContext = mock(ActorContext.class);
231 String shardName = "shard-1";
233 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
234 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
235 doReturn(getSystem()).when(actorContext).getActorSystem();
236 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
237 doReturn(getSystem().actorSelection(getRef().path())).when(actorContext)
238 .actorSelection(getRef().path());
239 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
240 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
242 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
243 actorContext, mockListener, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
245 Answer<Future<Object>> answer = invocation -> {
247 return Futures.successful((Object) new RegisterDataTreeNotificationListenerReply(getRef()));
250 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class),
253 proxy.init(shardName);
255 expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
257 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());