@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); } });
@Override public long millisTime() { return TimeUnit.MILLISECONDS.convert(nanoTime(), TimeUnit.NANOSECONDS); }
@Before public void resetClock() { fakeClock = new FakeApiClock(System.nanoTime()); executor = RecordingScheduler.create(fakeClock); clientContext = ClientContext.newBuilder() .setExecutor(executor) .setClock(fakeClock) .setDefaultCallContext(HttpJsonCallContext.createDefault()) .build(); }
@Before public void setUp() { clock = new FakeApiClock(0); watchdog = new Watchdog(clock); callable = new MockServerStreamingCallable<>(); innerObserver = new AccumulatingObserver<>(); callable.call("request", watchdog.watch(innerObserver, waitTime, idleTime)); call = callable.popLastCall(); }
@Before public void resetClock() { fakeClock = new FakeApiClock(System.nanoTime()); executor = RecordingScheduler.create(fakeClock); clientContext = ClientContext.newBuilder() .setExecutor(executor) .setClock(fakeClock) .setDefaultCallContext(FakeCallContext.createDefault()) .setTransportChannel(FakeTransportChannel.create(new FakeChannel())) .build(); }
@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); }
@Before public void resetClock() { fakeClock = new FakeApiClock(System.nanoTime()); executor = RecordingScheduler.create(fakeClock); clientContext = ClientContext.newBuilder() .setExecutor(executor) .setClock(fakeClock) .setDefaultCallContext(FakeCallContext.createDefault()) .setTransportChannel(FakeTransportChannel.create(new FakeChannel())) .build(); }
@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); }
@Before public void setUp() throws IOException { initialChannel = mock(FakeChannel.class); pollTransportChannel = mock(TransportChannel.class); TransportChannelProvider operationsChannelProvider = mock(TransportChannelProvider.class); when(operationsChannelProvider.getTransportChannel()).thenReturn(pollTransportChannel); clock = new FakeApiClock(0L); executor = RecordingScheduler.create(clock); pollingAlgorithm = OperationTimedPollAlgorithm.create(FAST_RECHECKING_SETTINGS, clock); UnaryCallSettings<Integer, OperationSnapshot> initialCallSettings = UnaryCallSettings.<Integer, OperationSnapshot>newUnaryCallSettingsBuilder() .setRetrySettings(FAST_RETRY_SETTINGS.toBuilder().setMaxAttempts(1).build()) .build(); callSettings = OperationCallSettings.<Integer, Color, Currency>newBuilder() .setInitialCallSettings(initialCallSettings) .setResponseTransformer(new ResponseTransformer()) .setMetadataTransformer(new MetadataTransformer()) .setPollingAlgorithm(pollingAlgorithm) .build(); initialContext = getClientContext(initialChannel, executor); }
@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)); } }
when(operationsChannelProvider.getTransportChannel()).thenReturn(transportChannel); clock = new FakeApiClock(0L); executor = RecordingScheduler.create(clock); pollingAlgorithm = OperationTimedPollAlgorithm.create(FAST_RETRY_SETTINGS, clock);
clock.incrementNanoTime(waitTime.toNanos()); call1.getController().getObserver().onResponse("resp1");