HeartbeatManager { public void resetHeartbeat(ClientSubscriptionChannel channel, int heartbeatInterval) { synchronized (channel) { channel.setHeartbeatInterval(heartbeatInterval); ScheduledFuture<?> heartbeatTask = channel.getHeartbeatTask(); if (heartbeatTask != null) { heartbeatTask.cancel(true); channel.setHeartbeatTask(null); } if (heartbeatInterval > 0) { HeartbeatTimeoutTask task = new HeartbeatTimeoutTask( _subscriptionManager, channel); long interval = heartbeatInterval + _schedulingService.getResponseTimeout(); heartbeatTask = _schedulingService.schedule(task, interval, TimeUnit.SECONDS); channel.setHeartbeatTask(heartbeatTask); } } }  @Inject void setSchedulingService(SchedulingService schedulingService); @Inject void setSubscriptionManager(
      SiriClientSubscriptionManager subscriptionManager); void resetHeartbeat(ClientSubscriptionChannel channel,
      int heartbeatInterval);  }
@Test public void testResetHeartbeat() { ClientSubscriptionChannel channel = new ClientSubscriptionChannel( "http: Mockito.when(_schedulingService.getResponseTimeout()).thenReturn(10); _manager.resetHeartbeat(channel, 30); assertEquals(30, channel.getHeartbeatInterval()); Mockito.verify(_schedulingService).schedule(Mockito.any(Runnable.class), Mockito.eq(40L), Mockito.eq(TimeUnit.SECONDS)); }
@Test public void testResetHeartbeatToZero() { ClientSubscriptionChannel channel = new ClientSubscriptionChannel( "http: @SuppressWarnings("unchecked") ScheduledFuture<Object> future = Mockito.mock(ScheduledFuture.class); Mockito.when( _schedulingService.schedule(Mockito.any(Runnable.class), Mockito.eq(30L), Mockito.eq(TimeUnit.SECONDS))).thenReturn(future); _manager.resetHeartbeat(channel, 30); assertSame(future, channel.getHeartbeatTask()); Mockito.verify(_schedulingService).getResponseTimeout(); Mockito.verify(_schedulingService).schedule(Mockito.any(Runnable.class), Mockito.eq(30L), Mockito.eq(TimeUnit.SECONDS)); _manager.resetHeartbeat(channel, 0); Mockito.verify(future).cancel(true); Mockito.verifyNoMoreInteractions(_schedulingService); }
@Test public void testResetHeartbeatTimeoutTask() { ClientSubscriptionChannel channel = new ClientSubscriptionChannel( "http: _manager.resetHeartbeat(channel, 30); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); Mockito.verify(_schedulingService).schedule(captor.capture(), Mockito.eq(30L), Mockito.eq(TimeUnit.SECONDS)); Runnable timeoutTask = captor.getValue(); timeoutTask.run(); Mockito.verify(_subscriptionManager).handleChannelDisconnectAndReconnect( channel); }