Search in sources :

Example 1 with AutoConfiguredLoadBalancer

use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_delegateDoNotAcceptEmptyAddressList_nothing.

@Test
public void handleResolvedAddressGroups_delegateDoNotAcceptEmptyAddressList_nothing() throws Exception {
    Helper helper = new TestHelper();
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"high\" } } ] }");
    ConfigOrError lbConfig = lbf.parseLoadBalancerPolicy(serviceConfig);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(Collections.<EquivalentAddressGroup>emptyList()).setLoadBalancingPolicyConfig(lbConfig.getConfig()).build());
    assertThat(testLbBalancer.canHandleEmptyAddressListFromNameResolution()).isFalse();
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.UNAVAILABLE);
    assertThat(handleResult.getDescription()).startsWith("NameResolver returned no usable address");
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
}
Also used : ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ConfigOrError(io.grpc.NameResolver.ConfigOrError) Test(org.junit.Test)

Example 2 with AutoConfiguredLoadBalancer

use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method forwardsCalls.

@SuppressWarnings("deprecation")
@Test
public void forwardsCalls() {
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(new TestHelper());
    final AtomicInteger calls = new AtomicInteger();
    TestLoadBalancer testlb = new TestLoadBalancer() {

        @Override
        public void handleNameResolutionError(Status error) {
            calls.getAndSet(1);
        }

        @Override
        public void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
            calls.getAndSet(2);
        }

        @Override
        public void shutdown() {
            calls.getAndSet(3);
        }
    };
    lb.setDelegate(testlb);
    lb.handleNameResolutionError(Status.RESOURCE_EXHAUSTED);
    assertThat(calls.getAndSet(0)).isEqualTo(1);
    lb.handleSubchannelState(null, null);
    assertThat(calls.getAndSet(0)).isEqualTo(2);
    lb.shutdown();
    assertThat(calls.getAndSet(0)).isEqualTo(3);
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) ConnectivityStateInfo(io.grpc.ConnectivityStateInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subchannel(io.grpc.LoadBalancer.Subchannel) Test(org.junit.Test)

Example 3 with AutoConfiguredLoadBalancer

use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method channelTracing_lbPolicyChanged.

@Test
public void channelTracing_lbPolicyChanged() throws Exception {
    List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper() {

        @Override
        public Subchannel createSubchannel(CreateSubchannelArgs args) {
            return new TestSubchannel(args);
        }
    };
    AutoConfiguredLoadBalancer lb = new AutoConfiguredLoadBalancerFactory(GrpcUtil.DEFAULT_LB_POLICY).newLoadBalancer(helper);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    verifyNoMoreInteractions(channelLogger);
    ConfigOrError testLbParsedConfig = ConfigOrError.fromConfig("foo");
    nextParsedConfigOrError.set(testLbParsedConfig);
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { } } ] }");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(serviceConfig);
    handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    verify(channelLogger).log(eq(ChannelLogLevel.INFO), eq("Load balancer changed from {0} to {1}"), eq("PickFirstLoadBalancer"), eq(testLbBalancer.getClass().getSimpleName()));
    verify(channelLogger).log(eq(ChannelLogLevel.DEBUG), eq("Load-balancing config: {0}"), eq(testLbParsedConfig.getConfig()));
    verifyNoMoreInteractions(channelLogger);
    testLbParsedConfig = ConfigOrError.fromConfig("bar");
    nextParsedConfigOrError.set(testLbParsedConfig);
    serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { } } ] }");
    lbConfigs = lbf.parseLoadBalancerPolicy(serviceConfig);
    handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    verify(channelLogger).log(eq(ChannelLogLevel.DEBUG), eq("Load-balancing config: {0}"), eq(testLbParsedConfig.getConfig()));
    verifyNoMoreInteractions(channelLogger);
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ConfigOrError(io.grpc.NameResolver.ConfigOrError) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 4 with AutoConfiguredLoadBalancer

use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method defaultIsConfigurable.

@Test
public void defaultIsConfigurable() {
    AutoConfiguredLoadBalancer lb = new AutoConfiguredLoadBalancerFactory("test_lb").newLoadBalancer(new TestHelper());
    assertThat(lb.getDelegateProvider()).isSameInstanceAs(testLbBalancerProvider);
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Test(org.junit.Test)

Example 5 with AutoConfiguredLoadBalancer

use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_shutsDownOldBalancer.

@Test
public void handleResolvedAddressGroups_shutsDownOldBalancer() throws Exception {
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"round_robin\": { } } ] }");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(serviceConfig);
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper() {

        @Override
        public Subchannel createSubchannel(CreateSubchannelArgs args) {
            assertThat(args.getAddresses()).isEqualTo(servers);
            return new TestSubchannel(args);
        }
    };
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    final AtomicBoolean shutdown = new AtomicBoolean();
    TestLoadBalancer testlb = new TestLoadBalancer() {

        @Override
        public void handleNameResolutionError(Status error) {
        // noop
        }

        @Override
        public void shutdown() {
            shutdown.set(true);
        }
    };
    lb.setDelegate(testlb);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegateProvider().getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
    assertTrue(shutdown.get());
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ConfigOrError(io.grpc.NameResolver.ConfigOrError) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)14 Test (org.junit.Test)14 Status (io.grpc.Status)11 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)10 Helper (io.grpc.LoadBalancer.Helper)9 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)9 InetSocketAddress (java.net.InetSocketAddress)8 ConfigOrError (io.grpc.NameResolver.ConfigOrError)7 SocketAddress (java.net.SocketAddress)7 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)5 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)3 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)1 LoadBalancer (io.grpc.LoadBalancer)1 Subchannel (io.grpc.LoadBalancer.Subchannel)1 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1