private GrpcCallContext newCallContext(@Nullable Map<Option, ?> options, String resource) { GrpcCallContext context = GrpcCallContext.createDefault(); if (options != null) { context = context.withChannelAffinity(Option.CHANNEL_HINT.getLong(options).intValue()); } context = context.withExtraHeaders(metadataProvider.newExtraHeaders(resource, projectName)); return context.withStreamWaitTimeout(waitTimeout).withStreamIdleTimeout(idleTimeout); }
.setExecutor(executor) .setTransportChannel(transportChannel) .setDefaultCallContext(GrpcCallContext.of(managedChannel, CallOptions.DEFAULT)) .setBackgroundResources( Collections.<BackgroundResource>singletonList(transportChannel))
.splitCall( responseObserver, GrpcCallContext.createDefault().withChannelAffinity(channelAffinity));
private ApiCallContext contextWithParamsEncoder(RequestT request, ApiCallContext inputContext) { return GrpcCallContext.createDefault() .nullToSelf(inputContext) .withRequestParamsDynamicHeaderOption(paramsEncoder.encode(request)); } }
Preconditions.checkNotNull(grpcContext.getChannel()); CallOptions callOptions = grpcContext.getCallOptions(); Preconditions.checkNotNull(callOptions); if (grpcContext.getTimeout() != null) { Deadline newDeadline = Deadline.after(grpcContext.getTimeout().toMillis(), TimeUnit.MILLISECONDS); Deadline oldDeadline = callOptions.getDeadline(); Channel channel = grpcContext.getChannel(); if (grpcContext.getChannelAffinity() != null && channel instanceof ChannelPool) { channel = ((ChannelPool) channel).getChannel(grpcContext.getChannelAffinity()); if (!grpcContext.getExtraHeaders().isEmpty()) { ClientInterceptor interceptor = MetadataUtils.newAttachHeadersInterceptor(grpcContext.getMetadata()); channel = ClientInterceptors.intercept(channel, interceptor);
@Test public void testWithTimeout() { Truth.assertThat(GrpcCallContext.createDefault().withTimeout(null).getTimeout()).isNull(); }
@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); }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { GrpcCallContext context = GrpcCallContext.createDefault().nullToSelf(inputContext); ApiFuture<ResponseT> innerCallFuture = callable.futureCall(request, context); ExceptionTransformingFuture transformingFuture = new ExceptionTransformingFuture(innerCallFuture); ApiFutures.addCallback(innerCallFuture, transformingFuture, directExecutor()); return transformingFuture; }
@Before public void setUp() { innerCallable = new MockInnerCallable(); retryCodes = ImmutableSet.of(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE); callContext = GrpcCallContext.createDefault(); parentFuture = new MockRetryingFuture(); }
@Test public void testMergeWithCustomCallOptions() { CallOptions.Key<String> key = CallOptions.Key.createWithDefault("somekey", "somedefault"); GrpcCallContext ctx1 = GrpcCallContext.createDefault(); GrpcCallContext ctx2 = GrpcCallContext.createDefault() .withCallOptions(CallOptions.DEFAULT.withOption(key, "somevalue")); GrpcCallContext merged = (GrpcCallContext) ctx1.merge(ctx2); Truth.assertThat(merged.getCallOptions().getOption(key)) .isNotEqualTo(ctx1.getCallOptions().getOption(key)); Truth.assertThat(merged.getCallOptions().getOption(key)) .isEqualTo(ctx2.getCallOptions().getOption(key)); }
@Test public void testWithZeroStreamingIdleTimeout() { Duration timeout = Duration.ZERO; Truth.assertThat( GrpcCallContext.createDefault().withStreamIdleTimeout(timeout).getStreamIdleTimeout()) .isEqualTo(timeout); }
@Test public void testWithZeroStreamingWaitTimeout() { Duration timeout = Duration.ZERO; Truth.assertThat( GrpcCallContext.createDefault().withStreamWaitTimeout(timeout).getStreamWaitTimeout()) .isEqualTo(timeout); }
@Test public void testMergeWithNullStreamingWaitTimeout() { Duration timeout = Duration.ofSeconds(10); GrpcCallContext baseContext = GrpcCallContext.createDefault().withStreamWaitTimeout(timeout); GrpcCallContext defaultOverlay = GrpcCallContext.createDefault(); Truth.assertThat(baseContext.merge(defaultOverlay).getStreamWaitTimeout()).isEqualTo(timeout); GrpcCallContext explicitNullOverlay = GrpcCallContext.createDefault().withStreamWaitTimeout(null); Truth.assertThat(baseContext.merge(explicitNullOverlay).getStreamWaitTimeout()) .isEqualTo(timeout); }
@Test public void testMergeWithNullStreamingIdleTimeout() { Duration timeout = Duration.ofSeconds(10); GrpcCallContext baseContext = GrpcCallContext.createDefault().withStreamIdleTimeout(timeout); GrpcCallContext defaultOverlay = GrpcCallContext.createDefault(); Truth.assertThat(baseContext.merge(defaultOverlay).getStreamIdleTimeout()).isEqualTo(timeout); GrpcCallContext explicitNullOverlay = GrpcCallContext.createDefault().withStreamIdleTimeout(null); Truth.assertThat(baseContext.merge(explicitNullOverlay).getStreamIdleTimeout()) .isEqualTo(timeout); }
@Test public void testWithRequestParamsDynamicHeaderOption() { String encodedRequestParams = "param1=value¶m2.param3=value23"; GrpcCallContext context = GrpcCallContext.createDefault().withRequestParamsDynamicHeaderOption(encodedRequestParams); Map<Key<String>, String> headers = CallOptionsUtil.getDynamicHeadersOption(context.getCallOptions()); assertEquals( ImmutableMap.of(CallOptionsUtil.REQUEST_PARAMS_HEADER_KEY, encodedRequestParams), headers); }
@Test public void testMergeWithExtraHeaders() { Map<String, List<String>> extraHeaders1 = createTestExtraHeaders("key1", "value1", "key1", "value2"); GrpcCallContext ctx1 = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders1); Map<String, List<String>> extraHeaders2 = createTestExtraHeaders("key1", "value2", "key2", "value2"); GrpcCallContext ctx2 = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders2); ApiCallContext mergedApiCallContext = ctx1.merge(ctx2); Truth.assertThat(mergedApiCallContext).isInstanceOf(GrpcCallContext.class); GrpcCallContext mergedGrpcCallContext = (GrpcCallContext) mergedApiCallContext; Map<String, List<String>> gotExtraHeaders = mergedGrpcCallContext.getExtraHeaders(); Map<String, List<String>> expectedExtraHeaders = createTestExtraHeaders( "key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2"); Truth.assertThat(gotExtraHeaders).containsExactlyEntriesIn(expectedExtraHeaders); }
@Test public void testAffinity() { MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE; @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall0 = Mockito.mock(ClientCall.class); @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall1 = Mockito.mock(ClientCall.class); ManagedChannel channel0 = Mockito.mock(ManagedChannel.class); ManagedChannel channel1 = Mockito.mock(ManagedChannel.class); Mockito.when(channel0.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())) .thenReturn(clientCall0); Mockito.when(channel1.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())) .thenReturn(clientCall1); Channel pool = new ChannelPool(Arrays.asList(channel0, channel1)); GrpcCallContext context = GrpcCallContext.createDefault().withChannel(pool); ClientCall<Color, Money> gotCallA = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0)); ClientCall<Color, Money> gotCallB = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0)); ClientCall<Color, Money> gotCallC = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(1)); assertThat(gotCallA).isSameAs(gotCallB); assertThat(gotCallA).isNotSameAs(gotCallC); }
@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 testWithExtraHeaders() { Map<String, List<String>> extraHeaders = createTestExtraHeaders("key1", "value1", "key1", "value2"); GrpcCallContext ctx = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders); Map<String, List<String>> moreExtraHeaders = createTestExtraHeaders("key1", "value2", "key2", "value2"); ctx = ctx.withExtraHeaders(moreExtraHeaders); Map<String, List<String>> gotExtraHeaders = ctx.getExtraHeaders(); Map<String, List<String>> expectedExtraHeaders = createTestExtraHeaders( "key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2"); Truth.assertThat(gotExtraHeaders).containsExactlyEntriesIn(expectedExtraHeaders); }
GrpcCallContext.createDefault().withChannel(mockChannel).withExtraHeaders(extraHeaders); GrpcClientCalls.newCall(descriptor, context).start(mockListener, emptyHeaders);