f6c8f07f6b18cd51b5b7ba72ae77a693a3885ec0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerRegistrationProxyTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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.Dispatchers;
40 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
43 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
44 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import scala.concurrent.ExecutionContextExecutor;
48 import scala.concurrent.Future;
49 import scala.concurrent.duration.FiniteDuration;
50
51 /**
52  * Unit tests for DataChangeListenerRegistrationProxy.
53  *
54  * @author Thomas Pantelis
55  */
56 public class DataChangeListenerRegistrationProxyTest extends AbstractActorTest {
57
58     @SuppressWarnings("unchecked")
59     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockListener =
60             Mockito.mock(AsyncDataChangeListener.class);
61
62     @Test
63     public void testGetInstance() throws Exception {
64         DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
65                 "shard", Mockito.mock(ActorContext.class), mockListener);
66
67         Assert.assertEquals(mockListener, proxy.getInstance());
68     }
69
70     @Test(timeout=10000)
71     public void testSuccessfulRegistration() {
72         new JavaTestKit(getSystem()) {{
73             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
74                     mock(ClusterWrapper.class), mock(Configuration.class));
75
76             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
77                     "shard-1", actorContext, mockListener);
78
79             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
80             final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
81             new Thread() {
82                 @Override
83                 public void run() {
84                     proxy.init(path, scope);
85                 }
86
87             }.start();
88
89             FiniteDuration timeout = duration("5 seconds");
90             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
91             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
92
93             reply(new LocalShardFound(getRef()));
94
95             RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
96             Assert.assertEquals("getPath", path, registerMsg.getPath());
97             Assert.assertEquals("getScope", scope, registerMsg.getScope());
98
99             reply(new RegisterChangeListenerReply(getRef().path()));
100
101             for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
102                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
103             }
104
105             Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
106                     proxy.getListenerRegistrationActor());
107
108             watch(proxy.getDataChangeListenerActor());
109
110             proxy.close();
111
112             // The listener registration actor should get a Close message
113             expectMsgClass(timeout, CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
114
115             // The DataChangeListener actor should be terminated
116             expectMsgClass(timeout, Terminated.class);
117
118             proxy.close();
119
120             expectNoMsg();
121         }};
122     }
123
124     @Test(timeout=10000)
125     public void testLocalShardNotFound() {
126         new JavaTestKit(getSystem()) {{
127             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
128                     mock(ClusterWrapper.class), mock(Configuration.class));
129
130             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
131                     "shard-1", actorContext, mockListener);
132
133             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
134             final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
135             new Thread() {
136                 @Override
137                 public void run() {
138                     proxy.init(path, scope);
139                 }
140
141             }.start();
142
143             FiniteDuration timeout = duration("5 seconds");
144             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
145             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
146
147             reply(new LocalShardNotFound("shard-1"));
148
149             expectNoMsg(duration("1 seconds"));
150         }};
151     }
152
153     @Test(timeout=10000)
154     public void testLocalShardNotInitialized() {
155         new JavaTestKit(getSystem()) {{
156             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
157                     mock(ClusterWrapper.class), mock(Configuration.class));
158
159             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
160                     "shard-1", actorContext, mockListener);
161
162             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
163             final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
164             new Thread() {
165                 @Override
166                 public void run() {
167                     proxy.init(path, scope);
168                 }
169
170             }.start();
171
172             FiniteDuration timeout = duration("5 seconds");
173             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
174             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
175
176             reply(new ActorNotInitialized());
177
178             new Within(duration("1 seconds")) {
179                 @Override
180                 protected void run() {
181                     expectNoMsg();
182                 }
183             };
184         }};
185     }
186
187     @Test
188     public void testFailedRegistration() {
189         new JavaTestKit(getSystem()) {{
190             ActorSystem mockActorSystem = mock(ActorSystem.class);
191
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
198
199             ActorContext actorContext = mock(ActorContext.class);
200
201             doReturn(executor).when(actorContext).getClientDispatcher();
202
203             String shardName = "shard-1";
204             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
205                     shardName, actorContext, mockListener);
206
207             doReturn(mockActorSystem).when(actorContext).getActorSystem();
208             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
209             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
210             doReturn(Futures.failed(new RuntimeException("mock"))).
211                     when(actorContext).executeOperationAsync(any(ActorRef.class),
212                             any(Object.class), any(Timeout.class));
213             doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
214
215             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
216                     AsyncDataBroker.DataChangeScope.ONE);
217
218             Assert.assertEquals("getListenerRegistrationActor", null,
219                     proxy.getListenerRegistrationActor());
220         }};
221     }
222
223     @Test
224     public void testCloseBeforeRegistration() {
225         new JavaTestKit(getSystem()) {{
226             ActorContext actorContext = mock(ActorContext.class);
227
228             String shardName = "shard-1";
229             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
230                     shardName, actorContext, mockListener);
231
232             doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
233             doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
234             doReturn(getSystem()).when(actorContext).getActorSystem();
235             doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
236             doReturn(getSystem().actorSelection(getRef().path())).
237                     when(actorContext).actorSelection(getRef().path());
238             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
239             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
240
241             Answer<Future<Object>> answer = new Answer<Future<Object>>() {
242                 @Override
243                 public Future<Object> answer(InvocationOnMock invocation) {
244                     proxy.close();
245                     return Futures.successful((Object)new RegisterChangeListenerReply(getRef().path()));
246                 }
247             };
248
249             doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
250                     any(Object.class), any(Timeout.class));
251
252             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
253                     AsyncDataBroker.DataChangeScope.ONE);
254
255             expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
256
257             Assert.assertEquals("getListenerRegistrationActor", null,
258                     proxy.getListenerRegistrationActor());
259         }};
260     }
261 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.