use of io.grpc.InternalConfigSelector in project grpc-java by grpc.
the class ConfigSelectingClientCallTest method configSelectorInterceptsCall.
@Test
public void configSelectorInterceptsCall() {
Map<String, ?> rawMethodConfig = ImmutableMap.of("retryPolicy", ImmutableMap.of("maxAttempts", 3.0D, "initialBackoff", "1s", "maxBackoff", "10s", "backoffMultiplier", 1.5D, "retryableStatusCodes", ImmutableList.of("UNAVAILABLE")));
final MethodInfo methodInfo = new MethodInfo(rawMethodConfig, true, 4, 4);
final Metadata.Key<String> metadataKey = Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER);
final CallOptions.Key<String> callOptionsKey = CallOptions.Key.create("test");
InternalConfigSelector configSelector = new InternalConfigSelector() {
@Override
public Result selectConfig(final PickSubchannelArgs args) {
ManagedChannelServiceConfig config = new ManagedChannelServiceConfig(methodInfo, ImmutableMap.<String, MethodInfo>of(), ImmutableMap.<String, MethodInfo>of(), null, null, null);
return Result.newBuilder().setConfig(config).setInterceptor(// An interceptor that mutates CallOptions based on headers value.
new ClientInterceptor() {
String value = args.getHeaders().get(metadataKey);
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
callOptions = callOptions.withOption(callOptionsKey, value);
return next.newCall(method, callOptions);
}
}).build();
}
};
ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT.withAuthority("bar.authority"));
Metadata metadata = new Metadata();
metadata.put(metadataKey, "fooValue");
configSelectingClientCall.start(callListener, metadata);
assertThat(call.callOptions.getAuthority()).isEqualTo("bar.authority");
assertThat(call.callOptions.getOption(MethodInfo.KEY)).isEqualTo(methodInfo);
assertThat(call.callOptions.getOption(callOptionsKey)).isEqualTo("fooValue");
}
use of io.grpc.InternalConfigSelector in project grpc-java by grpc.
the class ConfigSelectingClientCallTest method selectionErrorPropagatedToListener.
@Test
public void selectionErrorPropagatedToListener() {
InternalConfigSelector configSelector = new InternalConfigSelector() {
@Override
public Result selectConfig(PickSubchannelArgs args) {
return Result.forError(Status.FAILED_PRECONDITION);
}
};
ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT);
configSelectingClientCall.start(callListener, new Metadata());
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
verify(callListener).onClose(statusCaptor.capture(), any(Metadata.class));
assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.FAILED_PRECONDITION);
// The call should not delegate to null and fail methods with NPE.
configSelectingClientCall.request(1);
}
use of io.grpc.InternalConfigSelector in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method invalidConfig_2ndResolution.
@Test
public void invalidConfig_2ndResolution() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> rawServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"1st raw config\"}}]}");
nameResolverFactory.nextRawServiceConfig.set(rawServiceConfig);
InternalConfigSelector configSelector = mock(InternalConfigSelector.class);
nameResolverFactory.nextAttributes.set(Attributes.newBuilder().set(InternalConfigSelector.KEY, configSelector).build());
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
reset(mockLoadBalancer);
// 2nd resolution lbConfig is error
nextLbPolicyConfigError.set(Status.UNKNOWN);
nameResolverFactory.allResolved();
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses newResolvedAddress = resultCaptor.getValue();
// should use previous service config because new service config is invalid.
assertThat(newResolvedAddress.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isEqualTo(ConnectivityState.IDLE);
}
Aggregations