CheckStatusManager { public void resetCheckStatusTask(ClientSubscriptionChannel channel, int checkStatusInterval) { synchronized (channel) { channel.setCheckStatusInterval(checkStatusInterval); _log.debug("resetting check status task: channel={} interval={}", channel.getAddress(), checkStatusInterval); ScheduledFuture<?> checkStatusTask = channel.getCheckStatusTask(); if (checkStatusTask != null) { checkStatusTask.cancel(true); channel.setCheckStatusTask(null); } if (checkStatusInterval > 0) { CheckStatusTask task = new CheckStatusTask(channel); checkStatusTask = _schedulingService.scheduleAtFixedRate(task, checkStatusInterval, checkStatusInterval, TimeUnit.SECONDS); channel.setCheckStatusTask(checkStatusTask); } } }  @Inject void setSubscriptionManager(
      SiriClientSubscriptionManager subscriptionManager); @Inject void setClient(SiriClientHandler client); @Inject void setScheduleService(SchedulingService schedulingService); void resetCheckStatusTask(ClientSubscriptionChannel channel,
      int checkStatusInterval); void handleCheckStatusResponse(CheckStatusResponseStructure response); void logErrorInCheckStatusResponse(ClientSubscriptionChannel channel,
      CheckStatusResponseStructure response, boolean isNewer, boolean isInError);  }
@Test public void testResetCheckStatusTask() { ClientSubscriptionChannel channel = new ClientSubscriptionChannel( "http: @SuppressWarnings("unchecked") ScheduledFuture<Object> taskMonitor = Mockito.mock(ScheduledFuture.class); Mockito.when( _schedulingService.scheduleAtFixedRate(Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(TimeUnit.class))).thenReturn( taskMonitor); _checkStatusManager.resetCheckStatusTask(channel, 30); assertSame(taskMonitor, channel.getCheckStatusTask()); assertEquals(30, channel.getCheckStatusInterval()); _checkStatusManager.resetCheckStatusTask(channel, 0); assertEquals(0, channel.getCheckStatusInterval()); Mockito.verify(taskMonitor).cancel(true); assertNull(channel.getCheckStatusTask()); }
@Test public void testCheckStatusTaskWithResponseTimeout() { ClientSubscriptionChannel channel = new ClientSubscriptionChannel( "http: @SuppressWarnings("unchecked") ScheduledFuture<Object> taskMonitor = Mockito.mock(ScheduledFuture.class); ArgumentCaptor<Runnable> taskArgument = ArgumentCaptor.forClass(Runnable.class); Mockito.when( _schedulingService.scheduleAtFixedRate(taskArgument.capture(), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(TimeUnit.class))).thenReturn( taskMonitor); _checkStatusManager.resetCheckStatusTask(channel, 30); Runnable task = taskArgument.getValue(); assertNotNull(task); @SuppressWarnings("unchecked") ScheduledFuture<Object> timeoutTaskMonitor = Mockito.mock(ScheduledFuture.class); ArgumentCaptor<Runnable> timeoutTaskArgument = ArgumentCaptor.forClass(Runnable.class); Mockito.when( _schedulingService.scheduleResponseTimeoutTask(timeoutTaskArgument.capture())).thenReturn( timeoutTaskMonitor); task.run(); Mockito.verify(_client).handleRequest(Mockito.any(SiriClientRequest.class)); Runnable timeoutTask = timeoutTaskArgument.getValue(); timeoutTask.run(); Mockito.verify(_subscriptionManager).handleChannelDisconnectAndReconnect( channel); }