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.Mockito.any;
11 import static org.mockito.Mockito.doAnswer;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.eq;
14 import static org.mockito.Mockito.mock;
15 import akka.actor.ActorRef;
16 import akka.actor.ActorSystem;
17 import akka.actor.Props;
18 import akka.actor.Terminated;
19 import akka.dispatch.ExecutionContexts;
20 import akka.dispatch.Futures;
21 import akka.testkit.JavaTestKit;
22 import akka.util.Timeout;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import com.google.common.util.concurrent.Uninterruptibles;
25 import java.util.concurrent.TimeUnit;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.cluster.datastore.messages.ActorNotInitialized;
32 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
33 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
34 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
35 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
36 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
37 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
38 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
39 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
40 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
41 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
42 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
43 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import scala.concurrent.ExecutionContextExecutor;
47 import scala.concurrent.Future;
48 import scala.concurrent.duration.FiniteDuration;
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());
70 public void testSuccessfulRegistration() {
71 new JavaTestKit(getSystem()) {{
72 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
73 mock(ClusterWrapper.class), mock(Configuration.class));
75 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
76 "shard-1", actorContext, mockListener);
78 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
79 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
83 proxy.init(path, scope);
88 FiniteDuration timeout = duration("5 seconds");
89 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
90 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
92 reply(new LocalShardFound(getRef()));
94 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
95 Assert.assertEquals("getPath", path, registerMsg.getPath());
96 Assert.assertEquals("getScope", scope, registerMsg.getScope());
98 reply(new RegisterChangeListenerReply(getRef().path()));
100 for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
101 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
104 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
105 proxy.getListenerRegistrationActor());
107 watch(proxy.getDataChangeListenerActor());
111 // The listener registration actor should get a Close message
112 expectMsgClass(timeout, CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
114 // The DataChangeListener actor should be terminated
115 expectMsgClass(timeout, Terminated.class);
124 public void testLocalShardNotFound() {
125 new JavaTestKit(getSystem()) {{
126 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
127 mock(ClusterWrapper.class), mock(Configuration.class));
129 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
130 "shard-1", actorContext, mockListener);
132 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
133 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
137 proxy.init(path, scope);
142 FiniteDuration timeout = duration("5 seconds");
143 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
144 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
146 reply(new LocalShardNotFound("shard-1"));
148 expectNoMsg(duration("1 seconds"));
153 public void testLocalShardNotInitialized() {
154 new JavaTestKit(getSystem()) {{
155 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
156 mock(ClusterWrapper.class), mock(Configuration.class));
158 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
159 "shard-1", actorContext, mockListener);
161 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
162 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
166 proxy.init(path, scope);
171 FiniteDuration timeout = duration("5 seconds");
172 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
173 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
175 reply(new ActorNotInitialized());
177 new Within(duration("1 seconds")) {
179 protected void run() {
187 public void testFailedRegistration() {
188 new JavaTestKit(getSystem()) {{
189 ActorSystem mockActorSystem = mock(ActorSystem.class);
191 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
192 "testFailedRegistration");
193 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
194 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
195 MoreExecutors.sameThreadExecutor());
196 doReturn(executor).when(mockActorSystem).dispatcher();
198 ActorContext actorContext = mock(ActorContext.class);
200 String shardName = "shard-1";
201 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
202 shardName, actorContext, mockListener);
204 doReturn(mockActorSystem).when(actorContext).getActorSystem();
205 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
206 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
207 doReturn(Futures.failed(new RuntimeException("mock"))).
208 when(actorContext).executeOperationAsync(any(ActorRef.class),
209 any(Object.class), any(Timeout.class));
210 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
212 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
213 AsyncDataBroker.DataChangeScope.ONE);
215 Assert.assertEquals("getListenerRegistrationActor", null,
216 proxy.getListenerRegistrationActor());
221 public void testCloseBeforeRegistration() {
222 new JavaTestKit(getSystem()) {{
223 ActorContext actorContext = mock(ActorContext.class);
225 String shardName = "shard-1";
226 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
227 shardName, actorContext, mockListener);
229 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
230 doReturn(getSystem()).when(actorContext).getActorSystem();
231 doReturn(getSystem().actorSelection(getRef().path())).
232 when(actorContext).actorSelection(getRef().path());
233 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
234 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
236 Answer<Future<Object>> answer = new Answer<Future<Object>>() {
238 public Future<Object> answer(InvocationOnMock invocation) {
240 return Futures.successful((Object)new RegisterChangeListenerReply(getRef().path()));
244 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
245 any(Object.class), any(Timeout.class));
247 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
248 AsyncDataBroker.DataChangeScope.ONE);
250 expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
252 Assert.assertEquals("getListenerRegistrationActor", null,
253 proxy.getListenerRegistrationActor());