@Override public boolean cancel(final boolean mayInterruptIfRunning) { future.cancel(mayInterruptIfRunning); return super.cancel(mayInterruptIfRunning); }
@Override public void run() { try { latch.await(); while (!resultFuture.cancel(true) && !resultFuture.isDone()) { Thread.sleep(1L); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } });
@Test public void cancellationBeforeGetOnRetryingCallable() throws Exception { thrown.expect(CancellationException.class); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) .thenReturn(SettableApiFuture.<Integer>create()); UnaryCallSettings<Integer, Integer> callSettings = RetryingTest.createSettings(FAST_RETRY_SETTINGS); UnaryCallable<Integer, Integer> callable = FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); ApiFuture<Integer> resultFuture = callable.futureCall(0); resultFuture.cancel(true); resultFuture.get(); }
@Test public void translateException_cancelled() throws Exception { GrpcExceptionCallable<Integer, Integer> exceptionCallable = new GrpcExceptionCallable<>(inactiveCallable(), Collections.<StatusCode.Code>emptySet()); ApiFuture<Integer> result = exceptionCallable.futureCall(0); Truth.assertThat(result.isDone()).isFalse(); result.cancel(true); Truth.assertThat(result.isCancelled()).isTrue(); Truth.assertThat(result.isDone()).isTrue(); }
@Test public void testEarlyTermination() throws Exception { ApiFuture<String> result = callable.futureCall("request"); MockServerStreamingCall<String, String> call = upstream.popLastCall(); // callable should request a single element on start Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isFalse(); Truth.assertThat(call.getController().popLastPull()).isEqualTo(1); // Then the user promptly cancels it result.cancel(true); // The cancellation should propagate to the inner callable Truth.assertThat(call.getController().isCancelled()).isTrue(); // Then we fake a cancellation going the other way (it will be wrapped in StatusRuntimeException // for grpc) call.getController() .getObserver() .onError(new RuntimeException("Some other upstream cancellation notice")); Throwable actualError = null; try { result.get(1, TimeUnit.SECONDS); } catch (Throwable e) { actualError = e; } // However, that exception will be ignored and will be replaced by a generic CancellationException Truth.assertThat(actualError).isInstanceOf(CancellationException.class); }
@Test public void testEarlyTermination() throws Exception { ApiFuture<List<String>> result = callable.futureCall("request"); MockServerStreamingCall<String, String> call = upstream.popLastCall(); // The caller cancels the stream while receiving responses call.getController().getObserver().onResponse("response1"); result.cancel(true); call.getController().getObserver().onResponse("response2"); // The cancellation should propagate upstream Truth.assertThat(call.getController().isCancelled()).isTrue(); // Then we fake a cancellation going the other way (it will be wrapped in StatusRuntimeException // for grpc) call.getController() .getObserver() .onError(new RuntimeException("Some other upstream cancellation indicator")); // However the inner cancellation exception will be masked by an outer CancellationException expectedException.expect(CancellationException.class); result.get(); }