Bug 8231: Fix testChangeListenerRegistration failure
[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.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;
15
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.Mockito;
30 import org.mockito.stubbing.Answer;
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.RegisterChangeListener;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
40 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
41 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
42 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
43 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
44 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
45 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
46 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataChangeListener;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import scala.concurrent.ExecutionContextExecutor;
50 import scala.concurrent.Future;
51 import scala.concurrent.duration.FiniteDuration;
52
53 /**
54  * Unit tests for DataChangeListenerRegistrationProxy.
55  *
56  * @author Thomas Pantelis
57  */
58 public class DataChangeListenerRegistrationProxyTest extends AbstractActorTest {
59
60     @SuppressWarnings("unchecked")
61     private final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockListener =
62             Mockito.mock(AsyncDataChangeListener.class);
63
64     @Test
65     public void testGetInstance() throws Exception {
66         try (DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
67                 "shard", Mockito.mock(ActorContext.class), mockListener)) {
68             Assert.assertEquals(mockListener, proxy.getInstance());
69         }
70     }
71
72     @Test(timeout = 10000)
73     public void testSuccessfulRegistration() {
74         new JavaTestKit(getSystem()) {
75             {
76                 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
77                         mock(ClusterWrapper.class), mock(Configuration.class));
78
79                 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
80                         "shard-1", actorContext, mockListener);
81
82                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
83                 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
84                 new Thread() {
85                     @Override
86                     public void run() {
87                         proxy.init(path, scope);
88                     }
89
90                 }.start();
91
92                 FiniteDuration timeout = duration("5 seconds");
93                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
94                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
95
96                 reply(new LocalShardFound(getRef()));
97
98                 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
99                 Assert.assertEquals("getPath", path, registerMsg.getPath());
100                 Assert.assertEquals("getScope", scope, registerMsg.getScope());
101                 Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
102
103                 reply(new RegisterChangeListenerReply(getRef()));
104
105                 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
106                     Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
107                 }
108
109                 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
110                         proxy.getListenerRegistrationActor());
111
112                 watch(proxy.getDataChangeListenerActor());
113
114                 proxy.close();
115
116                 // The listener registration actor should get a Close message
117                 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
118
119                 // The DataChangeListener actor should be terminated
120                 expectMsgClass(timeout, Terminated.class);
121
122                 proxy.close();
123
124                 expectNoMsg();
125             }
126         };
127     }
128
129     @Test(timeout = 10000)
130     public void testSuccessfulRegistrationForClusteredListener() {
131         new JavaTestKit(getSystem()) {
132             {
133                 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
134                     mock(ClusterWrapper.class), mock(Configuration.class));
135
136                 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> mockClusteredListener =
137                         Mockito.mock(ClusteredDOMDataChangeListener.class);
138
139                 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
140                         "shard-1", actorContext, mockClusteredListener);
141
142                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
143                 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
144                 new Thread() {
145                     @Override
146                     public void run() {
147                         proxy.init(path, scope);
148                     }
149
150                 }.start();
151
152                 FiniteDuration timeout = duration("5 seconds");
153                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
154                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
155
156                 reply(new LocalShardFound(getRef()));
157
158                 RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
159                 Assert.assertEquals("getPath", path, registerMsg.getPath());
160                 Assert.assertEquals("getScope", scope, registerMsg.getScope());
161                 Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
162
163                 reply(new RegisterChangeListenerReply(getRef()));
164
165                 for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
166                     Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
167                 }
168
169                 Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
170                         proxy.getListenerRegistrationActor());
171
172                 watch(proxy.getDataChangeListenerActor());
173
174                 proxy.close();
175
176                 // The listener registration actor should get a Close message
177                 expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
178
179                 // The DataChangeListener actor should be terminated
180                 expectMsgClass(timeout, Terminated.class);
181
182                 proxy.close();
183
184                 expectNoMsg();
185             }
186         };
187     }
188
189     @Test(timeout = 10000)
190     public void testLocalShardNotFound() {
191         new JavaTestKit(getSystem()) {
192             {
193                 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
194                         mock(ClusterWrapper.class), mock(Configuration.class));
195
196                 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
197                         "shard-1", actorContext, mockListener);
198
199                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
200                 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
201                 new Thread() {
202                     @Override
203                     public void run() {
204                         proxy.init(path, scope);
205                     }
206
207                 }.start();
208
209                 FiniteDuration timeout = duration("5 seconds");
210                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
211                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
212
213                 reply(new LocalShardNotFound("shard-1"));
214
215                 expectNoMsg(duration("1 seconds"));
216
217                 proxy.close();
218             }
219         };
220     }
221
222     @Test(timeout = 10000)
223     public void testLocalShardNotInitialized() {
224         new JavaTestKit(getSystem()) {
225             {
226                 ActorContext actorContext = new ActorContext(getSystem(), getRef(),
227                         mock(ClusterWrapper.class), mock(Configuration.class));
228
229                 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
230                         "shard-1", actorContext, mockListener);
231
232                 final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
233                 final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
234                 new Thread() {
235                     @Override
236                     public void run() {
237                         proxy.init(path, scope);
238                     }
239
240                 }.start();
241
242                 FiniteDuration timeout = duration("5 seconds");
243                 FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
244                 Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
245
246                 reply(new NotInitializedException("not initialized"));
247
248                 new Within(duration("1 seconds")) {
249                     @Override
250                     protected void run() {
251                         expectNoMsg();
252                     }
253                 };
254
255                 proxy.close();
256             }
257         };
258     }
259
260     @Test
261     public void testFailedRegistration() {
262         new JavaTestKit(getSystem()) {
263             {
264                 ActorSystem mockActorSystem = mock(ActorSystem.class);
265
266                 ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
267                         "testFailedRegistration");
268                 doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
269                 ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
270                         MoreExecutors.directExecutor());
271
272
273                 ActorContext actorContext = mock(ActorContext.class);
274
275                 doReturn(executor).when(actorContext).getClientDispatcher();
276
277                 String shardName = "shard-1";
278                 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
279                         shardName, actorContext, mockListener);
280
281                 doReturn(mockActorSystem).when(actorContext).getActorSystem();
282                 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
283                 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
284                 doReturn(Futures.failed(new RuntimeException("mock")))
285                     .when(actorContext).executeOperationAsync(any(ActorRef.class),
286                         any(Object.class), any(Timeout.class));
287                 doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
288
289                 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
290                         AsyncDataBroker.DataChangeScope.ONE);
291
292                 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
293
294                 proxy.close();
295             }
296         };
297     }
298
299     @Test
300     public void testCloseBeforeRegistration() {
301         new JavaTestKit(getSystem()) {
302             {
303                 ActorContext actorContext = mock(ActorContext.class);
304
305                 String shardName = "shard-1";
306                 final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
307                         shardName, actorContext, mockListener);
308
309                 doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
310                 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
311                 doReturn(getSystem()).when(actorContext).getActorSystem();
312                 doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
313                 doReturn(getSystem().actorSelection(getRef().path()))
314                     .when(actorContext).actorSelection(getRef().path());
315                 doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
316                 doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
317
318                 Answer<Future<Object>> answer = invocation -> {
319                     proxy.close();
320                     return Futures.successful((Object)new RegisterChangeListenerReply(getRef()));
321                 };
322
323                 doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
324                         any(Object.class), any(Timeout.class));
325
326                 proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
327                         AsyncDataBroker.DataChangeScope.ONE);
328
329                 expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
330
331                 Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
332                 proxy.close();
333             }
334         };
335     }
336 }