import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import com.google.common.util.concurrent.Futures;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Matchers;
import org.mockito.Mock;
+import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import com.google.common.util.concurrent.Futures;
+
public class KeepaliveSalFacadeTest {
private static final RemoteDeviceId REMOTE_DEVICE_ID = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
@Mock
private RemoteDeviceHandler<NetconfSessionPreferences> underlyingSalFacade;
- private static java.util.concurrent.ScheduledExecutorService executorService;
+ private ScheduledExecutorService executorServiceSpy;
@Mock
private NetconfDeviceCommunicator listener;
private DOMRpcService proxyRpc;
+ @Mock
+ private ScheduledFuture currentKeepalive;
+
@Before
public void setUp() throws Exception {
- executorService = Executors.newScheduledThreadPool(1);
+ executorServiceSpy = Executors.newScheduledThreadPool(1);
MockitoAnnotations.initMocks(this);
doReturn("mockedRpc").when(deviceRpc).toString();
doNothing().when(underlyingSalFacade).onDeviceConnected(
any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
+
+ ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
+ executorServiceSpy = Mockito.spy(executorService);
+ doAnswer(new Answer<ScheduledFuture>() {
+ @Override
+ public ScheduledFuture answer(InvocationOnMock invocationOnMock)
+ throws Throwable {
+ invocationOnMock.callRealMethod();
+ return currentKeepalive;
+ }
+ }).when(executorServiceSpy).schedule(Mockito.<Runnable> any(),
+ Mockito.anyLong(), Matchers.<TimeUnit> any());
+
+ Mockito.when(currentKeepalive.isDone()).thenReturn(true);
}
@After
public void tearDown() throws Exception {
- executorService.shutdownNow();
+ executorServiceSpy.shutdownNow();
}
@Test
doReturn(Futures.immediateCheckedFuture(result)).when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
final KeepaliveSalFacade keepaliveSalFacade =
- new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 1L);
+ new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 1L);
keepaliveSalFacade.setListener(listener);
keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
verify(underlyingSalFacade).onDeviceConnected(
any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
- verify(deviceRpc, timeout(15000).times(5)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+ verify(deviceRpc, timeout(15000).atLeast(5)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
}
@Test
.when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
final KeepaliveSalFacade keepaliveSalFacade =
- new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 1L);
+ new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 1L);
keepaliveSalFacade.setListener(listener);
keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
// 1 failed that results in disconnect, 3 total with previous fail
verify(listener, timeout(15000).times(3)).disconnect();
+
+
+ Mockito.when(currentKeepalive.isDone()).thenReturn(false);
+ keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
+ // 1 failed that results in disconnect, 4 total with previous fail
+ verify(listener, timeout(15000).times(4)).disconnect();
}
@Test
.when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
final KeepaliveSalFacade keepaliveSalFacade =
- new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 100L);
+ new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 100L);
keepaliveSalFacade.setListener(listener);
keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);