305ec316611b8f61364bea8053da910429fb45d0
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / SequencedQueueTest.java
1 /*
2  * Copyright (c) 2016 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.access.client;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
20 import akka.actor.ActorRef;
21 import akka.actor.ActorSystem;
22 import akka.testkit.TestProbe;
23 import java.util.Optional;
24 import java.util.concurrent.CompletableFuture;
25 import java.util.concurrent.ThreadLocalRandom;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.controller.cluster.access.ABIVersion;
36 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestFailureProxy;
37 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestProxy;
38 import org.opendaylight.controller.cluster.access.concepts.FailureEnvelope;
39 import org.opendaylight.controller.cluster.access.concepts.Request;
40 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
41 import org.opendaylight.controller.cluster.access.concepts.RequestException;
42 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
43 import org.opendaylight.controller.cluster.common.actor.TestTicker;
44 import org.opendaylight.yangtools.concepts.WritableIdentifier;
45 import scala.concurrent.duration.FiniteDuration;
46
47 /**
48  * Test suite covering logic contained in {@link SequencedQueue}. It assumes {@link SequencedQueueEntryTest} passes.
49  *
50  * @author Robert Varga
51  */
52 public class SequencedQueueTest {
53     private static class MockFailure extends RequestFailure<WritableIdentifier, MockFailure> {
54         private static final long serialVersionUID = 1L;
55
56         MockFailure(final WritableIdentifier target, final RequestException cause) {
57             super(target, 0, cause);
58         }
59
60         @Override
61         protected AbstractRequestFailureProxy<WritableIdentifier, MockFailure> externalizableProxy(final ABIVersion version) {
62             return null;
63         }
64
65         @Override
66         protected MockFailure cloneAsVersion(final ABIVersion version) {
67             return this;
68         }
69     }
70
71     private static class MockRequest extends Request<WritableIdentifier, MockRequest> {
72         private static final long serialVersionUID = 1L;
73
74         MockRequest(final WritableIdentifier target, final ActorRef replyTo) {
75             super(target, 0, replyTo);
76         }
77
78         @Override
79         public RequestFailure<WritableIdentifier, ?> toRequestFailure(final RequestException cause) {
80             return new MockFailure(getTarget(), cause);
81         }
82
83         @Override
84         protected AbstractRequestProxy<WritableIdentifier, MockRequest> externalizableProxy(final ABIVersion version) {
85             return null;
86         }
87
88         @Override
89         protected MockRequest cloneAsVersion(final ABIVersion version) {
90             return this;
91         }
92     }
93
94     @Mock
95     private ActorRef mockReplyTo;
96     @Mock
97     private WritableIdentifier mockIdentifier;
98     @Mock
99     private RequestException mockCause;
100     @Mock
101     private RequestCallback mockCallback;
102     @Mock
103     private ClientActorBehavior mockBehavior;
104
105     private TestTicker ticker;
106     private BackendInfo mockBackendInfo;
107     private MockRequest mockRequest;
108     private MockRequest mockRequest2;
109     private RequestFailure<WritableIdentifier, ?> mockResponse;
110     private FailureEnvelope mockResponseEnvelope;
111     private Long mockCookie;
112
113     private static ActorSystem actorSystem;
114     private TestProbe mockActor;
115
116     private SequencedQueue queue;
117
118     @BeforeClass
119     public static void setupClass() {
120         actorSystem = ActorSystem.apply();
121     }
122
123     @AfterClass
124     public static void teardownClass() {
125         actorSystem.terminate();
126     }
127
128     @Before
129     public void setup() {
130         MockitoAnnotations.initMocks(this);
131
132         doReturn(mockBehavior).when(mockCallback).complete(any(MockFailure.class));
133
134         ticker = new TestTicker();
135         ticker.increment(ThreadLocalRandom.current().nextLong());
136
137         mockActor = TestProbe.apply(actorSystem);
138         mockBackendInfo = new BackendInfo(mockActor.ref(), 0, ABIVersion.current(), 5);
139         mockRequest = new MockRequest(mockIdentifier, mockReplyTo);
140         mockRequest2 = new MockRequest(mockIdentifier, mockReplyTo);
141         mockResponse = mockRequest.toRequestFailure(mockCause);
142         mockResponseEnvelope = new FailureEnvelope(mockResponse, 0, 0);
143         mockCookie = ThreadLocalRandom.current().nextLong();
144
145         queue = new SequencedQueue(mockCookie, ticker);
146     }
147
148     @After
149     public void teardown() {
150         actorSystem.stop(mockActor.ref());
151     }
152
153     @Test
154     public void testGetCookie() {
155         assertSame(mockCookie, queue.getCookie());
156     }
157
158     @Test
159     public void testEmptyClose() {
160         assertFalse(queue.hasCompleted());
161         queue.close();
162         assertTrue(queue.hasCompleted());
163     }
164
165     @Test(expected=IllegalStateException.class)
166     public void testClosedEnqueueRequest() {
167         queue.close();
168
169         // Kaboom
170         queue.enqueueRequest(mockRequest, mockCallback);
171     }
172
173     @Test
174     public void testCloseIdempotent() {
175         queue.close();
176         queue.close();
177     }
178
179     @Test
180     public void testPoison() {
181         queue.enqueueRequest(mockRequest, mockCallback);
182         queue.poison(mockCause);
183
184         final ArgumentCaptor<MockFailure> captor = ArgumentCaptor.forClass(MockFailure.class);
185         verify(mockCallback).complete(captor.capture());
186         assertSame(mockCause, captor.getValue().getCause());
187     }
188
189     @Test(expected=IllegalStateException.class)
190     public void testPoisonPerformsClose() {
191         // Implies close()
192         queue.poison(mockCause);
193
194         // Kaboom
195         queue.enqueueRequest(mockRequest, mockCallback);
196     }
197
198     @Test
199     public void testPoisonIdempotent() {
200         queue.poison(mockCause);
201         queue.poison(mockCause);
202     }
203
204     @Test
205     public void testEnqueueRequestNeedsBackend() {
206         final Optional<FiniteDuration> ret = queue.enqueueRequest(mockRequest, mockCallback);
207
208         assertNotNull(ret);
209         assertFalse(ret.isPresent());
210     }
211
212     @Test
213     public void testExpectProof() {
214         final CompletableFuture<BackendInfo> proof = new CompletableFuture<>();
215         assertTrue(queue.expectProof(proof));
216         assertFalse(queue.expectProof(proof));
217     }
218
219     @Test(expected=NullPointerException.class)
220     public void testSetBackendNull() {
221         final CompletableFuture<BackendInfo> proof = new CompletableFuture<>();
222         assertTrue(queue.expectProof(proof));
223         queue.setBackendInfo(proof, null);
224     }
225
226     @Test
227     public void testSetBackendWithNoResolution() {
228         queue.enqueueRequest(mockRequest, mockCallback);
229
230         final CompletableFuture<BackendInfo> proof = new CompletableFuture<>();
231         final Optional<FiniteDuration> ret = queue.setBackendInfo(proof, mockBackendInfo);
232         assertNotNull(ret);
233         assertFalse(ret.isPresent());
234     }
235
236     @Test
237     public void testSetBackendWithWrongProof() {
238         queue.enqueueRequest(mockRequest, mockCallback);
239
240         final CompletableFuture<BackendInfo> proof = new CompletableFuture<>();
241         assertTrue(queue.expectProof(proof));
242
243         final Optional<FiniteDuration> ret = queue.setBackendInfo(new CompletableFuture<>(), mockBackendInfo);
244         assertNotNull(ret);
245         assertFalse(ret.isPresent());
246     }
247
248     @Test
249     public void testSetBackendWithNoRequests() {
250         // this utility method covers the entire test
251         setupBackend();
252     }
253
254     @Test
255     public void testSetBackendWithRequestsNoTimer() {
256         queue.enqueueRequest(mockRequest, mockCallback);
257
258         final CompletableFuture<BackendInfo> proof = new CompletableFuture<>();
259         assertTrue(queue.expectProof(proof));
260         assertFalse(mockActor.msgAvailable());
261
262         final Optional<FiniteDuration> ret = queue.setBackendInfo(proof, mockBackendInfo);
263         assertNotNull(ret);
264         assertTrue(ret.isPresent());
265
266         assertTransmit(mockRequest, 0);
267     }
268
269     @Test
270     public void testEnqueueRequestNeedsTimer() {
271         setupBackend();
272
273         final Optional<FiniteDuration> ret = queue.enqueueRequest(mockRequest, mockCallback);
274         assertNotNull(ret);
275         assertTrue(ret.isPresent());
276         assertTransmit(mockRequest, 0);
277     }
278
279     @Test
280     public void testEnqueueRequestWithoutTimer() {
281         setupBackend();
282
283         // First request
284         Optional<FiniteDuration> ret = queue.enqueueRequest(mockRequest, mockCallback);
285         assertNotNull(ret);
286         assertTrue(ret.isPresent());
287         assertTransmit(mockRequest, 0);
288
289         // Second request, no timer fired
290         ret = queue.enqueueRequest(mockRequest2, mockCallback);
291         assertNull(ret);
292         assertTransmit(mockRequest2, 1);
293     }
294
295     @Test
296     public void testRunTimeoutEmpty() throws NoProgressException {
297         final boolean ret = queue.runTimeout();
298         assertFalse(ret);
299     }
300
301     @Test
302     public void testRunTimeoutWithoutShift() throws NoProgressException {
303         queue.enqueueRequest(mockRequest, mockCallback);
304         final boolean ret = queue.runTimeout();
305         assertFalse(ret);
306     }
307
308     @Test
309     public void testRunTimeoutWithTimeoutLess() throws NoProgressException {
310         queue.enqueueRequest(mockRequest, mockCallback);
311
312         ticker.increment(SequencedQueue.REQUEST_TIMEOUT_NANOS - 1);
313
314         final boolean ret = queue.runTimeout();
315         assertFalse(ret);
316     }
317
318     @Test
319     public void testRunTimeoutWithTimeoutExact() throws NoProgressException {
320         setupBackend();
321
322         queue.enqueueRequest(mockRequest, mockCallback);
323
324         ticker.increment(SequencedQueue.REQUEST_TIMEOUT_NANOS);
325
326         final boolean ret = queue.runTimeout();
327         assertTrue(ret);
328     }
329
330     @Test
331     public void testRunTimeoutWithTimeoutMore() throws NoProgressException {
332         setupBackend();
333
334         queue.enqueueRequest(mockRequest, mockCallback);
335
336         ticker.increment(SequencedQueue.REQUEST_TIMEOUT_NANOS + 1);
337
338         final boolean ret = queue.runTimeout();
339         assertTrue(ret);
340     }
341
342     @Test(expected=NoProgressException.class)
343     public void testRunTimeoutWithoutProgressExact() throws NoProgressException {
344         queue.enqueueRequest(mockRequest, mockCallback);
345
346         ticker.increment(SequencedQueue.NO_PROGRESS_TIMEOUT_NANOS);
347
348         // Kaboom
349         queue.runTimeout();
350     }
351
352     @Test(expected=NoProgressException.class)
353     public void testRunTimeoutWithoutProgressMore() throws NoProgressException {
354         queue.enqueueRequest(mockRequest, mockCallback);
355
356         ticker.increment(SequencedQueue.NO_PROGRESS_TIMEOUT_NANOS + 1);
357
358         // Kaboom
359         queue.runTimeout();
360     }
361
362     @Test
363     public void testRunTimeoutEmptyWithoutProgressExact() throws NoProgressException {
364         ticker.increment(SequencedQueue.NO_PROGRESS_TIMEOUT_NANOS);
365
366         // No problem
367         final boolean ret = queue.runTimeout();
368         assertFalse(ret);
369     }
370
371     @Test
372     public void testRunTimeoutEmptyWithoutProgressMore() throws NoProgressException {
373         ticker.increment(SequencedQueue.NO_PROGRESS_TIMEOUT_NANOS + 1);
374
375         // No problem
376         final boolean ret = queue.runTimeout();
377         assertFalse(ret);
378     }
379
380     @Test
381     public void testCompleteEmpty() {
382         final ClientActorBehavior ret = queue.complete(mockBehavior, mockResponseEnvelope);
383         assertSame(mockBehavior, ret);
384         verifyNoMoreInteractions(mockCallback);
385     }
386
387     @Test
388     public void testCompleteSingle() {
389         setupBackend();
390
391         queue.enqueueRequest(mockRequest, mockCallback);
392
393         ClientActorBehavior ret = queue.complete(mockBehavior, mockResponseEnvelope);
394         verify(mockCallback).complete(mockResponse);
395         assertSame(mockBehavior, ret);
396
397         ret = queue.complete(mockBehavior, mockResponseEnvelope);
398         assertSame(mockBehavior, ret);
399         verifyNoMoreInteractions(mockCallback);
400     }
401
402     @Test
403     public void testCompleteNull() {
404         setupBackend();
405
406         queue.enqueueRequest(mockRequest, mockCallback);
407
408         doReturn(null).when(mockCallback).complete(mockResponse);
409
410         ClientActorBehavior ret = queue.complete(mockBehavior, mockResponseEnvelope);
411         verify(mockCallback).complete(mockResponse);
412         assertNull(ret);
413     }
414
415     @Test
416     public void testProgressRecord() throws NoProgressException {
417         setupBackend();
418
419         queue.enqueueRequest(mockRequest, mockCallback);
420
421         ticker.increment(10);
422         queue.enqueueRequest(mockRequest2, mockCallback);
423         queue.complete(mockBehavior, mockResponseEnvelope);
424
425         ticker.increment(SequencedQueue.NO_PROGRESS_TIMEOUT_NANOS - 11);
426         assertTrue(queue.runTimeout());
427     }
428
429     private void setupBackend() {
430         final CompletableFuture<BackendInfo> proof = new CompletableFuture<>();
431         assertTrue(queue.expectProof(proof));
432         final Optional<FiniteDuration> ret = queue.setBackendInfo(proof, mockBackendInfo);
433         assertNotNull(ret);
434         assertFalse(ret.isPresent());
435         assertFalse(mockActor.msgAvailable());
436     }
437
438     private void assertTransmit(final Request<?, ?> expected, final long sequence) {
439         assertTrue(mockActor.msgAvailable());
440         assertRequestEquals(expected, sequence, mockActor.receiveOne(FiniteDuration.apply(5, TimeUnit.SECONDS)));
441     }
442
443     private static void assertRequestEquals(final Request<?, ?> expected, final long sequence, final Object o) {
444         assertTrue(o instanceof RequestEnvelope);
445
446         final RequestEnvelope actual = (RequestEnvelope) o;
447         assertEquals(0, actual.getSessionId());
448         assertEquals(sequence, actual.getTxSequence());
449         assertSame(expected, actual.getMessage());
450     }
451 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.