2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.translate.util.read;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
26 import io.fd.honeycomb.translate.read.ReadContext;
27 import io.fd.honeycomb.translate.read.Reader;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.ScheduledExecutorService;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 public class KeepaliveReaderWrapperTest {
44 private ReadContext ctx;
45 private InstanceIdentifier<DataObject> iid = InstanceIdentifier.create(DataObject.class);
47 private Reader<DataObject, Builder<DataObject>> delegate;
49 private Builder<DataObject> builder;
51 private InstanceIdentifier<DataObject> id = InstanceIdentifier.create(DataObject.class);
52 private ScheduledExecutorService exec;
55 public void setUp() throws Exception {
56 MockitoAnnotations.initMocks(this);
57 exec = Executors.newScheduledThreadPool(1);
58 when(delegate.getManagedDataObjectType()).thenReturn(iid);
59 when(delegate.read(eq(iid), any(ReadContext.class))).thenThrow(TestingException.class);
63 public void tearDown() throws Exception {
67 @Test(timeout = 10000)
68 public void testKeepalive() throws Exception {
69 final CapturingFailListener listener = new CapturingFailListener();
71 final KeepaliveReaderWrapper<DataObject, Builder<DataObject>> keepaliveWrapper =
72 new KeepaliveReaderWrapper<>(delegate, exec, TestingException.class, 1, listener);
74 keepaliveWrapper.readCurrentAttributes(id, builder, ctx);
75 verify(delegate).readCurrentAttributes(id, builder, ctx);
77 keepaliveWrapper.getBuilder(id);
78 verify(delegate).getBuilder(id);
81 assertTrue(listener.getTriggerFuture().get());
82 verify(delegate).read(any(InstanceIdentifier.class), any(ReadContext.class));
84 keepaliveWrapper.close();
87 @Test(timeout = 10000)
88 public void testKeepaliveCancel() throws Exception {
89 final CapturingFailListener listener = new CapturingFailListener();
91 final KeepaliveReaderWrapper<DataObject, Builder<DataObject>> keepaliveWrapper =
92 new KeepaliveReaderWrapper<>(delegate, exec, TestingException.class, 100000, listener);
93 keepaliveWrapper.close();
94 assertFalse(listener.getTriggerFuture().isDone());
97 private static final class TestingException extends RuntimeException {}
99 private static class CapturingFailListener implements KeepaliveReaderWrapper.KeepaliveFailureListener {
100 private CompletableFuture<Boolean> booleanCompletableFuture = new CompletableFuture<>();
103 public synchronized void onKeepaliveFailure() {
104 booleanCompletableFuture.complete(true);
107 synchronized Future<Boolean> getTriggerFuture() {
108 return booleanCompletableFuture;