@SuppressWarnings("unchecked")
@Test
public void testTimeoutBeforeDeadline() {

  HttpJsonChannel mockChannel = Mockito.mock(HttpJsonChannel.class);
  String expectedRequest = "fake";
  HttpJsonDirectCallable<String, String> callable = new HttpJsonDirectCallable<>(API_DESCRIPTOR);
  // Mock the channel that captures the call options
  ArgumentCaptor<HttpJsonCallOptions> capturedCallOptions =
      ArgumentCaptor.forClass(HttpJsonCallOptions.class);
  Mockito.when(
          mockChannel.issueFutureUnaryCall(
              capturedCallOptions.capture(),
              Mockito.anyString(),
              Mockito.any(ApiMethodDescriptor.class)))
      .thenReturn(SettableApiFuture.create());
  // Compose the call context
  Duration timeout = Duration.ofSeconds(10);
  Instant subsequentDeadline = Instant.now().plusSeconds(15);
  Instant minExpectedDeadline = Instant.now().plus(timeout);
  HttpJsonCallContext callContext =
      HttpJsonCallContext.createDefault()
          .withChannel(mockChannel)
          .withDeadline(subsequentDeadline)
          .withTimeout(timeout);
  callable.futureCall(expectedRequest, callContext);
  Instant maxExpectedDeadline = Instant.now().plus(timeout);
  // Verify that the timeout was converted into a deadline
  assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline);
  assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline);
}