BUG 5656 : Entity ownership candidates not removed consistently on leadership change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerProxyTest.java
1 /*
2  * Copyright (c) 2015 Cisco 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 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.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.cluster.datastore.config.Configuration;
31 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
32 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration;
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.RegisterDataTreeChangeListener;
37 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
38 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
39 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
40 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import scala.concurrent.ExecutionContextExecutor;
46 import scala.concurrent.Future;
47 import scala.concurrent.duration.FiniteDuration;
48
49 public class DataTreeChangeListenerProxyTest extends AbstractActorTest {
50     private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class);
51
52     @Test(timeout=10000)
53     public void testSuccessfulRegistration() {
54         new JavaTestKit(getSystem()) {{
55             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
56                     mock(ClusterWrapper.class), mock(Configuration.class));
57
58             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
59                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
60
61             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
62             new Thread() {
63                 @Override
64                 public void run() {
65                     proxy.init("shard-1", path);
66                 }
67
68             }.start();
69
70             FiniteDuration timeout = duration("5 seconds");
71             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
72             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
73
74             reply(new LocalShardFound(getRef()));
75
76             RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
77             Assert.assertEquals("getPath", path, registerMsg.getPath());
78             Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
79
80             reply(new RegisterDataTreeChangeListenerReply(getRef()));
81
82
83             for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) {
84                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
85             }
86
87             Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()),
88                     proxy.getListenerRegistrationActor());
89
90             watch(proxy.getDataChangeListenerActor());
91
92             proxy.close();
93
94             // The listener registration actor should get a Close message
95             expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class);
96
97             // The DataChangeListener actor should be terminated
98             expectMsgClass(timeout, Terminated.class);
99
100             proxy.close();
101
102             expectNoMsg();
103         }};
104     }
105
106     @Test(timeout=10000)
107     public void testSuccessfulRegistrationForClusteredListener() {
108         new JavaTestKit(getSystem()) {{
109             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
110                     mock(ClusterWrapper.class), mock(Configuration.class));
111
112             ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(ClusteredDOMDataTreeChangeListener.class);
113
114             final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy =
115                     new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener);
116
117             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
118             new Thread() {
119                 @Override
120                 public void run() {
121                     proxy.init("shard-1", path);
122                 }
123
124             }.start();
125
126             FiniteDuration timeout = duration("5 seconds");
127             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
128             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
129
130             reply(new LocalShardFound(getRef()));
131
132             RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
133             Assert.assertEquals("getPath", path, registerMsg.getPath());
134             Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
135         }};
136     }
137
138     @Test(timeout=10000)
139     public void testLocalShardNotFound() {
140         new JavaTestKit(getSystem()) {{
141             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
142                     mock(ClusterWrapper.class), mock(Configuration.class));
143
144             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
145                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
146
147             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
148             new Thread() {
149                 @Override
150                 public void run() {
151                     proxy.init("shard-1", path);
152                 }
153
154             }.start();
155
156             FiniteDuration timeout = duration("5 seconds");
157             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
158             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
159
160             reply(new LocalShardNotFound("shard-1"));
161
162             expectNoMsg(duration("1 seconds"));
163         }};
164     }
165
166     @Test(timeout=10000)
167     public void testLocalShardNotInitialized() {
168         new JavaTestKit(getSystem()) {{
169             ActorContext actorContext = new ActorContext(getSystem(), getRef(),
170                     mock(ClusterWrapper.class), mock(Configuration.class));
171
172             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
173                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
174
175             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
176             new Thread() {
177                 @Override
178                 public void run() {
179                     proxy.init("shard-1", path);
180                 }
181
182             }.start();
183
184             FiniteDuration timeout = duration("5 seconds");
185             FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
186             Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
187
188             reply(new NotInitializedException("not initialized"));
189
190             new Within(duration("1 seconds")) {
191                 @Override
192                 protected void run() {
193                     expectNoMsg();
194                 }
195             };
196         }};
197     }
198
199     @Test
200     public void testFailedRegistration() {
201         new JavaTestKit(getSystem()) {{
202             ActorSystem mockActorSystem = mock(ActorSystem.class);
203
204             ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class),
205                     "testFailedRegistration");
206             doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
207             ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(
208                     MoreExecutors.directExecutor());
209
210
211             ActorContext actorContext = mock(ActorContext.class);
212             final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
213
214             doReturn(executor).when(actorContext).getClientDispatcher();
215             doReturn(mockActorSystem).when(actorContext).getActorSystem();
216
217             String shardName = "shard-1";
218             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
219                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
220
221             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
222             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
223             doReturn(Futures.failed(new RuntimeException("mock"))).
224                     when(actorContext).executeOperationAsync(any(ActorRef.class),
225                     any(Object.class), any(Timeout.class));
226             doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
227
228             proxy.init("shard-1", path);
229
230             Assert.assertEquals("getListenerRegistrationActor", null,
231                     proxy.getListenerRegistrationActor());
232         }};
233     }
234
235     @Test
236     public void testCloseBeforeRegistration() {
237         new JavaTestKit(getSystem()) {{
238             ActorContext actorContext = mock(ActorContext.class);
239
240             String shardName = "shard-1";
241
242             doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
243             doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher();
244             doReturn(getSystem()).when(actorContext).getActorSystem();
245             doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath();
246             doReturn(getSystem().actorSelection(getRef().path())).
247                     when(actorContext).actorSelection(getRef().path());
248             doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
249             doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
250
251             final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy =
252                     new DataTreeChangeListenerProxy<>(actorContext, mockListener);
253
254
255             Answer<Future<Object>> answer = new Answer<Future<Object>>() {
256                 @Override
257                 public Future<Object> answer(InvocationOnMock invocation) {
258                     proxy.close();
259                     return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef()));
260                 }
261             };
262
263             doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class),
264                     any(Object.class), any(Timeout.class));
265
266             proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
267
268             expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class);
269
270             Assert.assertEquals("getListenerRegistrationActor", null,
271                     proxy.getListenerRegistrationActor());
272         }};
273     }
274 }