8ad2e899aab5b5fa8ad7bf3e4a74c3ca66210964
[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.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;
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
197
198             ActorContext actorContext = mock(ActorContext.class);
199
200             doReturn(executor).when(actorContext).getClientDispatcher();
201
202             String shardName = "shard-1";
203             final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(
204                     shardName, actorContext, mockListener);
205
206             doReturn(mockActorSystem).when(actorContext).getActorSystem();
207             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
208             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
209             doReturn(Futures.failed(new RuntimeException("mock"))).
210                     when(actorContext).executeOperationAsync(any(ActorRef.class),
211                             any(Object.class), any(Timeout.class));
212             doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
213
214             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
215                     AsyncDataBroker.DataChangeScope.ONE);
216
217             Assert.assertEquals("getListenerRegistrationActor", null,
218                     proxy.getListenerRegistrationActor());
219         }};
220     }
221
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(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
232             doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
233             doReturn(getSystem()).when(actorContext).getActorSystem();
234             doReturn(getSystem().actorSelection(getRef().path())).
235                     when(actorContext).actorSelection(getRef().path());
236             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
237             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
238
239             Answer<Future<Object>> answer = new Answer<Future<Object>>() {
240                 @Override
241                 public Future<Object> answer(InvocationOnMock invocation) {
242                     proxy.close();
243                     return Futures.successful((Object)new RegisterChangeListenerReply(getRef().path()));
244                 }
245             };
246
247             doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
248                     any(Object.class), any(Timeout.class));
249
250             proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME),
251                     AsyncDataBroker.DataChangeScope.ONE);
252
253             expectMsgClass(duration("5 seconds"), CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS);
254
255             Assert.assertEquals("getListenerRegistrationActor", null,
256                     proxy.getListenerRegistrationActor());
257         }};
258     }
259 }