Search in sources :

Example 1 with ClientTransportFactoryBuilder

use of io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder in project grpc-java by grpc.

the class ManagedChannelImplTest method badServiceConfigIsRecoverable.

@Test
public void badServiceConfigIsRecoverable() throws Exception {
    final List<EquivalentAddressGroup> addresses = ImmutableList.of(new EquivalentAddressGroup(new SocketAddress() {
    }));
    final class FakeNameResolver extends NameResolver {

        Listener2 listener;

        @Override
        public String getServiceAuthority() {
            return "also fake";
        }

        @Override
        public void start(Listener2 listener) {
            this.listener = listener;
            listener.onResult(ResolutionResult.newBuilder().setAddresses(addresses).setServiceConfig(ConfigOrError.fromError(Status.INTERNAL.withDescription("kaboom is invalid"))).build());
        }

        @Override
        public void shutdown() {
        }
    }
    final class FakeNameResolverFactory2 extends NameResolver.Factory {

        FakeNameResolver resolver;

        @Nullable
        @Override
        public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
            return (resolver = new FakeNameResolver());
        }

        @Override
        public String getDefaultScheme() {
            return "fake";
        }
    }
    FakeNameResolverFactory2 factory = new FakeNameResolverFactory2();
    ManagedChannelImplBuilder customBuilder = new ManagedChannelImplBuilder(TARGET, new ClientTransportFactoryBuilder() {

        @Override
        public ClientTransportFactory buildClientTransportFactory() {
            return mockTransportFactory;
        }
    }, null);
    customBuilder.executorPool = executorPool;
    customBuilder.channelz = channelz;
    ManagedChannel mychannel = customBuilder.nameResolverFactory(factory).build();
    ClientCall<Void, Void> call1 = mychannel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT);
    ListenableFuture<Void> future1 = ClientCalls.futureUnaryCall(call1, null);
    executor.runDueTasks();
    try {
        future1.get(1, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        assertThat(Throwables.getStackTraceAsString(e.getCause())).contains("kaboom");
    }
    // ok the service config is bad, let's fix it.
    Map<String, Object> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {}}]}");
    Object fakeLbConfig = new Object();
    PolicySelection lbConfigs = new PolicySelection(mockLoadBalancerProvider, fakeLbConfig);
    mockLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawServiceConfig);
    ManagedChannelServiceConfig managedChannelServiceConfig = createManagedChannelServiceConfig(rawServiceConfig, lbConfigs);
    factory.resolver.listener.onResult(ResolutionResult.newBuilder().setAddresses(addresses).setServiceConfig(ConfigOrError.fromConfig(managedChannelServiceConfig)).build());
    ClientCall<Void, Void> call2 = mychannel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT.withDeadlineAfter(5, TimeUnit.SECONDS));
    ListenableFuture<Void> future2 = ClientCalls.futureUnaryCall(call2, null);
    timer.forwardTime(1234, TimeUnit.SECONDS);
    executor.runDueTasks();
    try {
        future2.get();
        Assert.fail();
    } catch (ExecutionException e) {
        assertThat(Throwables.getStackTraceAsString(e.getCause())).contains("deadline");
    }
    mychannel.shutdownNow();
}
Also used : PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) URI(java.net.URI) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) ExecutionException(java.util.concurrent.ExecutionException) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Aggregations

EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)1 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)1 ManagedChannel (io.grpc.ManagedChannel)1 NameResolver (io.grpc.NameResolver)1 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)1 ClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder)1 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)1 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)1 SocketAddress (java.net.SocketAddress)1 URI (java.net.URI)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1