@Test
public void testCancel() {

  observer = new AccumulatingObserver(false);
  ServerStreamingAttemptCallable<String, String> callable = createCallable();
  callable.start();
  observer.controller.request(1);
  Truth.assertThat(observer.controller).isNotNull();
  MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
  Truth.assertThat(call).isNotNull();
  Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isFalse();
  observer.controller.cancel();
  // Check upstream is cancelled
  Truth.assertThat(call.getController().isCancelled()).isTrue();
  // and after upstream cancellation is processed, downstream is cancelled, but the cause is
  // replaced
  RuntimeException innerException =
      new RuntimeException("Some internal representation of cancel");
  call.getController().getObserver().onError(innerException);
  Throwable outerError = null;
  try {
    fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
  } catch (ExecutionException e) {
    outerError = e.getCause();
  } catch (Throwable e) {
    outerError = e;
  }
  Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
  Truth.assertThat(outerError.getCause()).isInstanceOf(CancellationException.class);
  // Make sure that the stack trace of the cancellation is preserved
  boolean includesMeInStackTrace = false;
  for (StackTraceElement e : outerError.getCause().getStackTrace()) {
    if (ServerStreamingAttemptCallableTest.class.getName().equals(e.getClassName())) {
      includesMeInStackTrace = true;
      break;
    }
  }
  Truth.assertWithMessage("Cancel caller included in stack trace")
      .that(includesMeInStackTrace)
      .isTrue();
}