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