@Override public ScheduledFuture<?> answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Runnable runnable = (Runnable) args[0]; Long delay = (Long) args[1]; TimeUnit unit = (TimeUnit) args[2]; iterationsCount.incrementAndGet(); sleepDurations.add(Duration.ofMillis(TimeUnit.MILLISECONDS.convert(delay, unit))); clock.incrementNanoTime(TimeUnit.NANOSECONDS.convert(delay, unit)); return executor.schedule(runnable, 0, TimeUnit.NANOSECONDS); } });
@Test public void testIdleTimeout() throws InterruptedException { clock.incrementNanoTime(idleTime.toNanos() - 1); watchdog.run(); Truth.assertThat(call.getController().isCancelled()).isFalse(); clock.incrementNanoTime(1); watchdog.run(); Truth.assertThat(call.getController().isCancelled()).isTrue(); call.getController() .getObserver() .onError(new RuntimeException("Some upstream exception representing cancellation")); Throwable actualError = null; try { innerObserver.done.get(); } catch (ExecutionException t) { actualError = t.getCause(); } Truth.assertThat(actualError).isInstanceOf(WatchdogTimeoutException.class); }
@Test public void testWaitTimeout() throws Exception { innerObserver.controller.get(1, TimeUnit.MILLISECONDS).request(1); clock.incrementNanoTime(waitTime.toNanos() - 1); watchdog.run(); Truth.assertThat(call.getController().isCancelled()).isFalse(); clock.incrementNanoTime(1); watchdog.run(); Truth.assertThat(call.getController().isCancelled()).isTrue(); call.getController() .getObserver() .onError(new RuntimeException("Some upstream exception representing cancellation")); Throwable actualError = null; try { innerObserver.done.get(); } catch (ExecutionException t) { actualError = t.getCause(); } Truth.assertThat(actualError).isInstanceOf(WatchdogTimeoutException.class); }
@Test public void testShouldRetryFalseOnMaxTimeout() { TimedAttemptSettings attempt = algorithm.createFirstAttempt(); for (int i = 0; i < 4; i++) { assertTrue(algorithm.shouldRetry(attempt)); attempt = algorithm.createNextAttempt(attempt); clock.incrementNanoTime(Duration.ofMillis(50L).toNanos()); } assertFalse(algorithm.shouldRetry(attempt)); } }
clock.incrementNanoTime(waitTime.toNanos()); call1.getController().getObserver().onResponse("resp1");