@Test public void testWithTimeout() { Truth.assertThat(GrpcCallContext.createDefault().withTimeout(null).getTimeout()).isNull(); }
@Test public void testWithShorterTimeout() { GrpcCallContext ctxWithLongTimeout = GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(10)); // Sanity check Truth.assertThat(ctxWithLongTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(10)); // Shorten the timeout and make sure it changed GrpcCallContext ctxWithShorterTimeout = ctxWithLongTimeout.withTimeout(Duration.ofSeconds(5)); Truth.assertThat(ctxWithShorterTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(5)); }
@Test public void testWithNegativeTimeout() { Truth.assertThat( GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(-1L)).getTimeout()) .isNull(); }
@Test public void testWithZeroTimeout() { Truth.assertThat( GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(0L)).getTimeout()) .isNull(); }
@Test public void testWithLongerTimeout() { GrpcCallContext ctxWithShortTimeout = GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(5)); // Sanity check Truth.assertThat(ctxWithShortTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(5)); // Try to extend the timeout and verify that it was ignored GrpcCallContext ctxWithUnchangedTimeout = ctxWithShortTimeout.withTimeout(Duration.ofSeconds(10)); Truth.assertThat(ctxWithUnchangedTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(5)); }
@Test public void testMergeWithNullTimeout() { Duration timeout = Duration.ofSeconds(10); GrpcCallContext baseContext = GrpcCallContext.createDefault().withTimeout(timeout); GrpcCallContext defaultOverlay = GrpcCallContext.createDefault(); Truth.assertThat(baseContext.merge(defaultOverlay).getTimeout()).isEqualTo(timeout); GrpcCallContext explicitNullOverlay = GrpcCallContext.createDefault().withTimeout(null); Truth.assertThat(baseContext.merge(explicitNullOverlay).getTimeout()).isEqualTo(timeout); }
@Test public void testTimeoutAfterDeadline() { MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE; @SuppressWarnings("unchecked") ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class); @SuppressWarnings("unchecked") ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class); @SuppressWarnings("unchecked") Channel mockChannel = Mockito.mock(ManagedChannel.class); ArgumentCaptor<CallOptions> capturedCallOptions = ArgumentCaptor.forClass(CallOptions.class); Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), capturedCallOptions.capture())) .thenReturn(mockClientCall); // Configure a timeout that occurs after the grpc deadline Deadline priorDeadline = Deadline.after(5, TimeUnit.SECONDS); Duration timeout = Duration.ofSeconds(10); GrpcCallContext context = GrpcCallContext.createDefault() .withChannel(mockChannel) .withCallOptions(CallOptions.DEFAULT.withDeadline(priorDeadline)) .withTimeout(timeout); GrpcClientCalls.newCall(descriptor, context).start(mockListener, new Metadata()); // Verify that the timeout is ignored Truth.assertThat(capturedCallOptions.getValue().getDeadline()).isEqualTo(priorDeadline); }
@Test public void testTimeoutToDeadlineConversion() { MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE; @SuppressWarnings("unchecked") ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class); @SuppressWarnings("unchecked") ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class); @SuppressWarnings("unchecked") Channel mockChannel = Mockito.mock(ManagedChannel.class); ArgumentCaptor<CallOptions> capturedCallOptions = ArgumentCaptor.forClass(CallOptions.class); Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), capturedCallOptions.capture())) .thenReturn(mockClientCall); Duration timeout = Duration.ofSeconds(10); Deadline minExpectedDeadline = Deadline.after(timeout.getSeconds(), TimeUnit.SECONDS); GrpcCallContext context = GrpcCallContext.createDefault().withChannel(mockChannel).withTimeout(timeout); GrpcClientCalls.newCall(descriptor, context).start(mockListener, new Metadata()); Deadline maxExpectedDeadline = Deadline.after(timeout.getSeconds(), TimeUnit.SECONDS); Truth.assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline); Truth.assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline); }
.withChannel(mockChannel) .withCallOptions(CallOptions.DEFAULT.withDeadline(subsequentDeadline)) .withTimeout(timeout);
@Test public void testMergeWithTimeout() { Duration timeout = Duration.ofSeconds(19); GrpcCallContext ctx1 = GrpcCallContext.createDefault(); GrpcCallContext ctx2 = GrpcCallContext.createDefault().withTimeout(timeout); Truth.assertThat(ctx1.merge(ctx2).getTimeout()).isEqualTo(timeout); }