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.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.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.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
43 import org.opendaylight.controller.md.sal.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 JavaTestKit(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 JavaTestKit(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 JavaTestKit(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 JavaTestKit(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 new Within(duration("1 seconds")) {
181 protected void run() {
192 public void testFailedRegistration() {
193 new JavaTestKit(getSystem()) {
195 ActorSystem mockActorSystem = mock(ActorSystem.class);
197 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration");
198 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
199 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
201 ActorContext actorContext = mock(ActorContext.class);
202 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
204 doReturn(executor).when(actorContext).getClientDispatcher();
205 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
206 doReturn(mockActorSystem).when(actorContext).getActorSystem();
208 String shardName = "shard-1";
209 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
210 actorContext, mockListener, path);
212 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
213 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
214 doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext)
215 .executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
216 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
218 proxy.init("shard-1");
220 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
228 public void testCloseBeforeRegistration() {
229 new JavaTestKit(getSystem()) {
231 ActorContext actorContext = mock(ActorContext.class);
233 String shardName = "shard-1";
235 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
236 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
237 doReturn(getSystem()).when(actorContext).getActorSystem();
238 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
239 doReturn(getSystem().actorSelection(getRef().path())).when(actorContext)
240 .actorSelection(getRef().path());
241 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
242 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
244 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(
245 actorContext, mockListener, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
247 Answer<Future<Object>> answer = invocation -> {
249 return Futures.successful((Object) new RegisterDataTreeNotificationListenerReply(getRef()));
252 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class),
255 proxy.init(shardName);
257 expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
259 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());