Search in sources :

Example 6 with AutoConfiguredLoadBalancer

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

the class AutoConfiguredLoadBalancerFactoryTest method defaultIsPickFirst.

@Test
public void defaultIsPickFirst() {
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(new TestHelper());
    assertThat(lb.getDelegateProvider()).isInstanceOf(PickFirstLoadBalancerProvider.class);
    assertThat(lb.getDelegate().getClass().getName()).contains("PickFirst");
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Test(org.junit.Test)

Example 7 with AutoConfiguredLoadBalancer

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_noLbPolicySelected_defaultToPickFirst.

@Test
public void handleResolvedAddressGroups_noLbPolicySelected_defaultToPickFirst() {
    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);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(null).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isInstanceOf(PickFirstLoadBalancer.class);
}
Also used : ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 8 with AutoConfiguredLoadBalancer

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_noLbPolicySelected_defaultToCustomDefault.

@Test
public void handleResolvedAddressGroups_noLbPolicySelected_defaultToCustomDefault() {
    AutoConfiguredLoadBalancer lb = new AutoConfiguredLoadBalancerFactory("test_lb").newLoadBalancer(new TestHelper());
    List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(null).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 9 with AutoConfiguredLoadBalancer

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_propagateLbConfigToDelegate.

@Test
@SuppressWarnings("unchecked")
public void handleResolvedAddressGroups_propagateLbConfigToDelegate() throws Exception {
    Map<String, ?> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"high\" } } ] }");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    assertThat(lbConfigs.getConfig()).isNotNull();
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper();
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    verify(testLbBalancerProvider).newLoadBalancer(same(helper));
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
    ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(testLbBalancer).handleResolvedAddresses(resultCaptor.capture());
    assertThat(resultCaptor.getValue().getAddresses()).containsExactlyElementsIn(servers).inOrder();
    verify(testLbBalancer, atLeast(0)).canHandleEmptyAddressListFromNameResolution();
    ArgumentCaptor<Map<String, ?>> lbConfigCaptor = ArgumentCaptor.forClass(Map.class);
    verify(testLbBalancerProvider).parseLoadBalancingPolicyConfig(lbConfigCaptor.capture());
    assertThat(lbConfigCaptor.getValue()).containsExactly("setting1", "high");
    verifyNoMoreInteractions(testLbBalancer);
    rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"low\" } } ] }");
    lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(testLbBalancer, times(2)).handleResolvedAddresses(resultCaptor.capture());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(resultCaptor.getValue().getAddresses()).containsExactlyElementsIn(servers).inOrder();
    verify(testLbBalancerProvider, times(2)).parseLoadBalancingPolicyConfig(lbConfigCaptor.capture());
    assertThat(lbConfigCaptor.getValue()).containsExactly("setting1", "low");
    // Service config didn't change policy, thus the delegateLb is not swapped
    verifyNoMoreInteractions(testLbBalancer);
    verify(testLbBalancerProvider).newLoadBalancer(any(Helper.class));
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ConfigOrError(io.grpc.NameResolver.ConfigOrError) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Map(java.util.Map) Test(org.junit.Test)

Example 10 with AutoConfiguredLoadBalancer

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

the class AutoConfiguredLoadBalancerFactoryTest method newLoadBalancer_isAuto.

@Test
public void newLoadBalancer_isAuto() {
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(new TestHelper());
    assertThat(lb).isInstanceOf(AutoConfiguredLoadBalancer.class);
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) 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