@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
  try {
    result.get();
    Assert.fail("Callable should have thrown an exception");
  } catch (CancellationException expected) {
    Truth.assertThat(expected).hasMessageThat().contains("Task was cancelled.");
  }
}