@Test public void cancellationDuringRetryDelay() throws Exception { Throwable throwable = new UnavailableException(null, FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), true); CancellationTrackingFuture<Integer> innerFuture = CancellationTrackingFuture.create(); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) .thenReturn(RetryingTest.<Integer>immediateFailedFuture(throwable)) .thenReturn(innerFuture); CountDownLatch retryScheduledLatch = new CountDownLatch(1); LatchCountDownScheduler scheduler = LatchCountDownScheduler.get(retryScheduledLatch, 0L, 0L); UnaryCallSettings<Integer, Integer> callSettings = RetryingTest.createSettings(SLOW_RETRY_SETTINGS); UnaryCallable<Integer, Integer> callable = FakeCallableFactory.createUnaryCallable( callInt, callSettings, clientContext.toBuilder().setExecutor(scheduler).build()); ApiFuture<Integer> resultFuture = callable.futureCall(0); CancellationHelpers.cancelInThreadAfterLatchCountDown(resultFuture, retryScheduledLatch); CancellationException gotException = null; try { resultFuture.get(); } catch (CancellationException e) { gotException = e; } Truth.assertThat(gotException).isNotNull(); Truth.assertThat(resultFuture.isDone()).isTrue(); Truth.assertThat(resultFuture.isCancelled()).isTrue(); Truth.assertThat(innerFuture.isCancelled()).isFalse(); scheduler.shutdownNow(); }
@Test public void testAuth() throws InterruptedException, ExecutionException, CancellationException { StashCallable<Integer, Integer> stash = new StashCallable<>(42); Truth.assertThat(stash.getContext()).isNull(); UnaryCallSettings<Integer, Integer> callSettings = UnaryCallSettings.<Integer, Integer>newUnaryCallSettingsBuilder().build(); UnaryCallable<Integer, Integer> callable = FakeCallableFactory.createUnaryCallable( stash, callSettings, clientContext.toBuilder().setCredentials(Mockito.mock(Credentials.class)).build()); Truth.assertThat(callable.futureCall(0).get()).isEqualTo(42); Truth.assertThat(stash.getContext()).isNotNull(); FakeCallContext callContext = (FakeCallContext) stash.getContext(); Truth.assertThat(callContext.getCredentials()).isNotNull(); } }
@Test public void cancellationDuringFirstCall() throws Exception { CancellationTrackingFuture<Integer> innerFuture = CancellationTrackingFuture.<Integer>create(); CountDownLatch callIssuedLatch = new CountDownLatch(1); UnaryCallable<Integer, Integer> innerCallable = new LatchCountDownFutureCallable<>(callIssuedLatch, innerFuture); UnaryCallSettings<Integer, Integer> callSettings = RetryingTest.createSettings(FAST_RETRY_SETTINGS); UnaryCallable<Integer, Integer> callable = FakeCallableFactory.createUnaryCallable( innerCallable, callSettings, clientContext.toBuilder().setExecutor(new ScheduledThreadPoolExecutor(1)).build()); ApiFuture<Integer> resultFuture = callable.futureCall(0); CancellationHelpers.cancelInThreadAfterLatchCountDown(resultFuture, callIssuedLatch); CancellationException gotException = null; try { resultFuture.get(); } catch (CancellationException e) { gotException = e; } Truth.assertThat(gotException).isNotNull(); Truth.assertThat(innerFuture.isCancelled()).isTrue(); }
@Test public void testBadContext() { streamingCallable = GrpcCallableFactory.createServerStreamingCallable( GrpcCallSettings.create(METHOD_SERVER_STREAMING_RECOGNIZE), streamingCallSettings, clientContext .toBuilder() .setDefaultCallContext(FakeCallContext.createDefault()) .build()); CountDownLatch latch = new CountDownLatch(1); MoneyObserver observer = new MoneyObserver(true, latch); thrown.expect(IllegalArgumentException.class); streamingCallable.call(DEFAULT_REQUEST, observer); }
innerCallable, callSettings, clientContext.toBuilder().setExecutor(new ScheduledThreadPoolExecutor(1)).build());