Merge "Bug 2055: Handle shard not initialized resiliently"
[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     @SuppressWarnings("unchecked")
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             doReturn(executor).when(mockActorSystem).dispatcher();
198
199             ActorContext actorContext = mock(ActorContext.class);
200
201             String shardName = "shard-1";
202             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
203                     shardName, actorContext, mockListener);
204
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                     any(Timeout.class));
209             doReturn(Futures.failed(new RuntimeException("mock"))).
210                     when(actorContext).executeOperationAsync(any(ActorRef.class),
211                             any(Object.class), any(Timeout.class));
212
213             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
214                     AsyncDataBroker.DataChangeScope.ONE);
215
216             Assert.assertEquals("getListenerRegistrationActor", null,
217                     proxy.getListenerRegistrationActor());
218         }};
219     }
220
221     @SuppressWarnings("unchecked")
222     @Test
223     public void testCloseBeforeRegistration() {
224         new JavaTestKit(getSystem()) {{
225             ActorContext actorContext = mock(ActorContext.class);
226
227             String shardName = "shard-1";
228             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
229                     shardName, actorContext, mockListener);
230
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),
236                     any(Timeout.class));
237
238             Answer<Future<Object>> answer = new Answer<Future<Object>>() {
239                 @Override
240                 public Future<Object> answer(InvocationOnMock invocation) {
241                     proxy.close();
242                     return Futures.successful((Object)new RegisterChangeListenerReply(getRef().path()));
243                 }
244             };
245
246             doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
247                     any(Object.class), any(Timeout.class));
248
249             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
250                     AsyncDataBroker.DataChangeScope.ONE);
251
252             expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
253
254             Assert.assertEquals("getListenerRegistrationActor", null,
255                     proxy.getListenerRegistrationActor());
256         }};
257     }
258 }