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 java.util.concurrent.TimeUnit;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.actor.Props;
14 import akka.actor.Terminated;
15 import akka.dispatch.ExecutionContexts;
16 import akka.dispatch.Futures;
17 import akka.testkit.JavaTestKit;
18 import akka.util.Timeout;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
25 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
26 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
27 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
28 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
29 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
30 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
31 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
32 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
33 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
34 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
35 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
36 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import com.google.common.util.concurrent.MoreExecutors;
40 import com.google.common.util.concurrent.Uninterruptibles;
41 import scala.concurrent.ExecutionContextExecutor;
42 import scala.concurrent.Future;
43 import scala.concurrent.duration.FiniteDuration;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.any;
46 import static org.mockito.Mockito.doReturn;
47 import static org.mockito.Mockito.doAnswer;
48 import static org.mockito.Mockito.eq;
51 * Unit tests for DataChangeListenerRegistrationProxy.
53 * @author Thomas Pantelis
55 public class DataChangeListenerRegistrationProxyTest extends AbstractActorTest {
57 @SuppressWarnings("unchecked")
58 private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockListener =
59 Mockito.mock(AsyncDataChangeListener.class);
62 public void testGetInstance() throws Exception {
63 DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
64 "shard", Mockito.mock(ActorContext.class), mockListener);
66 Assert.assertEquals(mockListener, proxy.getInstance());
69 @SuppressWarnings("unchecked")
71 public void testSuccessfulRegistration() {
72 new JavaTestKit(getSystem()) {{
73 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
74 mock(ClusterWrapper.class), mock(Configuration.class));
76 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
77 "shard-1", actorContext, mockListener);
79 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
80 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
84 proxy.init(path, scope);
89 FiniteDuration timeout = duration("5 seconds");
90 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
91 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
93 reply(new LocalShardFound(getRef()));
95 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
96 Assert.assertEquals("getPath", path, registerMsg.getPath());
97 Assert.assertEquals("getScope", scope, registerMsg.getScope());
99 reply(new RegisterChangeListenerReply(getRef().path()));
101 for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
102 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
105 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
106 proxy.getListenerRegistrationActor());
108 watch(proxy.getDataChangeListenerActor());
112 // The listener registration actor should get a Close message
113 expectMsgClass(timeout, CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
115 // The DataChangeListener actor should be terminated
116 expectMsgClass(timeout, Terminated.class);
125 public void testLocalShardNotFound() {
126 new JavaTestKit(getSystem()) {{
127 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
128 mock(ClusterWrapper.class), mock(Configuration.class));
130 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
131 "shard-1", actorContext, mockListener);
133 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
134 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
138 proxy.init(path, scope);
143 FiniteDuration timeout = duration("5 seconds");
144 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
145 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
147 reply(new LocalShardNotFound("shard-1"));
149 expectNoMsg(duration("1 seconds"));
154 public void testLocalShardNotInitialized() {
155 new JavaTestKit(getSystem()) {{
156 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
157 mock(ClusterWrapper.class), mock(Configuration.class));
159 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
160 "shard-1", actorContext, mockListener);
162 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
163 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
167 proxy.init(path, scope);
172 FiniteDuration timeout = duration("5 seconds");
173 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
174 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
176 reply(new ActorNotInitialized());
178 new Within(duration("1 seconds")) {
180 protected void run() {
188 public void testFailedRegistration() {
189 new JavaTestKit(getSystem()) {{
190 ActorSystem mockActorSystem = mock(ActorSystem.class);
192 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
193 "testFailedRegistration");
194 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
195 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
196 MoreExecutors.sameThreadExecutor());
197 doReturn(executor).when(mockActorSystem).dispatcher();
199 ActorContext actorContext = mock(ActorContext.class);
201 String shardName = "shard-1";
202 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
203 shardName, actorContext, mockListener);
205 doReturn(mockActorSystem).when(actorContext).getActorSystem();
206 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
207 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
208 doReturn(Futures.failed(new RuntimeException("mock"))).
209 when(actorContext).executeOperationAsync(any(ActorRef.class),
210 any(Object.class), any(Timeout.class));
212 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
213 AsyncDataBroker.DataChangeScope.ONE);
215 Assert.assertEquals("getListenerRegistrationActor", null,
216 proxy.getListenerRegistrationActor());
220 @SuppressWarnings("unchecked")
222 public void testCloseBeforeRegistration() {
223 new JavaTestKit(getSystem()) {{
224 ActorContext actorContext = mock(ActorContext.class);
226 String shardName = "shard-1";
227 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
228 shardName, actorContext, mockListener);
230 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
231 doReturn(getSystem()).when(actorContext).getActorSystem();
232 doReturn(getSystem().actorSelection(getRef().path())).
233 when(actorContext).actorSelection(getRef().path());
234 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
235 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
237 Answer<Future<Object>> answer = new Answer<Future<Object>>() {
239 public Future<Object> answer(InvocationOnMock invocation) {
241 return Futures.successful((Object)new RegisterChangeListenerReply(getRef().path()));
245 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
246 any(Object.class), any(Timeout.class));
248 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
249 AsyncDataBroker.DataChangeScope.ONE);
251 expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
253 Assert.assertEquals("getListenerRegistrationActor", null,
254 proxy.getListenerRegistrationActor());