Search in sources :

Example 41 with Helper

use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.

the class ClusterResolverLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new AssertionError(e);
        }
    });
    FakeClock fakeClock = new FakeClock();
    NameResolverRegistry nsRegistry = new NameResolverRegistry();
    NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(8080).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(mock(ServiceConfigParser.class)).setChannelLogger(mock(ChannelLogger.class)).build();
    when(helper.getNameResolverRegistry()).thenReturn(nsRegistry);
    when(helper.getNameResolverArgs()).thenReturn(args);
    when(helper.getSynchronizationContext()).thenReturn(syncContext);
    when(helper.getScheduledExecutorService()).thenReturn(fakeClock.getScheduledExecutorService());
    when(helper.getAuthority()).thenReturn("api.google.com");
    LoadBalancerProvider provider = new ClusterResolverLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(ClusterResolverLoadBalancer.class);
}
Also used : FakeClock(io.grpc.internal.FakeClock) LoadBalancer(io.grpc.LoadBalancer) NameResolverRegistry(io.grpc.NameResolverRegistry) Helper(io.grpc.LoadBalancer.Helper) SynchronizationContext(io.grpc.SynchronizationContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ServiceConfigParser(io.grpc.NameResolver.ServiceConfigParser) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 42 with Helper

use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.

the class CdsLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new AssertionError(e);
        }
    });
    when(helper.getSynchronizationContext()).thenReturn(syncContext);
    LoadBalancerProvider provider = new CdsLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(CdsLoadBalancer2.class);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SynchronizationContext(io.grpc.SynchronizationContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 43 with Helper

use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.

the class PriorityLoadBalancerTest method readyToConnectDoesNotFailOverButUpdatesPicker.

@Test
public void readyToConnectDoesNotFailOverButUpdatesPicker() {
    PriorityChildConfig priorityChildConfig0 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityChildConfig priorityChildConfig1 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityLbConfig priorityLbConfig = new PriorityLbConfig(ImmutableMap.of("p0", priorityChildConfig0, "p1", priorityChildConfig1), ImmutableList.of("p0", "p1"));
    priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(priorityLbConfig).build());
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
    Helper helper0 = Iterables.getOnlyElement(fooHelpers);
    // p0 gets READY.
    final Subchannel subchannel0 = mock(Subchannel.class);
    helper0.updateBalancingState(READY, new SubchannelPicker() {

        @Override
        public PickResult pickSubchannel(PickSubchannelArgs args) {
            return PickResult.withSubchannel(subchannel0);
        }
    });
    assertCurrentPickerPicksSubchannel(subchannel0);
    // p0 goes to CONNECTING
    helper0.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // no failover happened
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
    // resolution update without priority change does not trigger failover
    Attributes.Key<String> fooKey = Attributes.Key.create("fooKey");
    priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(priorityLbConfig).setAttributes(Attributes.newBuilder().set(fooKey, "barVal").build()).build());
    assertCurrentPickerIsBufferPicker();
    // no failover happened
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) Attributes(io.grpc.Attributes) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) PickResult(io.grpc.LoadBalancer.PickResult) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 44 with Helper

use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.

the class RingHashLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    when(helper.getSynchronizationContext()).thenReturn(syncContext);
    when(helper.getAuthority()).thenReturn(AUTHORITY);
    assertThat(provider.newLoadBalancer(helper)).isInstanceOf(RingHashLoadBalancer.class);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) Test(org.junit.Test)

Example 45 with Helper

use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.

the class WeightedTargetLoadBalancerProviderTest method parseWeightedTargetConfig.

@Test
public void parseWeightedTargetConfig() throws Exception {
    LoadBalancerRegistry lbRegistry = new LoadBalancerRegistry();
    WeightedTargetLoadBalancerProvider weightedTargetLoadBalancerProvider = new WeightedTargetLoadBalancerProvider(lbRegistry);
    final Object fooConfig = new Object();
    LoadBalancerProvider lbProviderFoo = new LoadBalancerProvider() {

        @Override
        public boolean isAvailable() {
            return true;
        }

        @Override
        public int getPriority() {
            return 5;
        }

        @Override
        public String getPolicyName() {
            return "foo_policy";
        }

        @Override
        public LoadBalancer newLoadBalancer(Helper helper) {
            return mock(LoadBalancer.class);
        }

        @Override
        public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
            return ConfigOrError.fromConfig(fooConfig);
        }
    };
    final Object barConfig = new Object();
    LoadBalancerProvider lbProviderBar = new LoadBalancerProvider() {

        @Override
        public boolean isAvailable() {
            return true;
        }

        @Override
        public int getPriority() {
            return 5;
        }

        @Override
        public String getPolicyName() {
            return "bar_policy";
        }

        @Override
        public LoadBalancer newLoadBalancer(Helper helper) {
            return mock(LoadBalancer.class);
        }

        @Override
        public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
            return ConfigOrError.fromConfig(barConfig);
        }
    };
    lbRegistry.register(lbProviderFoo);
    lbRegistry.register(lbProviderBar);
    String weightedTargetConfigJson = ("{" + "  'targets' : {" + "    'target_1' : {" + "      'weight' : 10," + "      'childPolicy' : [" + "        {'unsupported_policy' : {}}," + "        {'foo_policy' : {}}" + "      ]" + "    }," + "    'target_2' : {" + "      'weight' : 20," + "      'childPolicy' : [" + "        {'unsupported_policy' : {}}," + "        {'bar_policy' : {}}" + "      ]" + "    }" + "  }" + "}").replace('\'', '"');
    @SuppressWarnings("unchecked") Map<String, ?> rawLbConfigMap = (Map<String, ?>) JsonParser.parse(weightedTargetConfigJson);
    ConfigOrError parsedConfig = weightedTargetLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawLbConfigMap);
    ConfigOrError expectedConfig = ConfigOrError.fromConfig(new WeightedTargetConfig(ImmutableMap.of("target_1", new WeightedPolicySelection(10, new PolicySelection(lbProviderFoo, fooConfig)), "target_2", new WeightedPolicySelection(20, new PolicySelection(lbProviderBar, barConfig)))));
    assertThat(parsedConfig).isEqualTo(expectedConfig);
}
Also used : WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Helper(io.grpc.LoadBalancer.Helper) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ConfigOrError(io.grpc.NameResolver.ConfigOrError) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Helper (io.grpc.LoadBalancer.Helper)59 Test (org.junit.Test)57 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)28 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)21 LoadBalancer (io.grpc.LoadBalancer)20 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)20 Subchannel (io.grpc.LoadBalancer.Subchannel)18 Metadata (io.grpc.Metadata)16 Status (io.grpc.Status)14 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)13 SocketAddress (java.net.SocketAddress)13 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)12 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)10 ConfigOrError (io.grpc.NameResolver.ConfigOrError)9 AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)9 ClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder)9 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)9 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)9 ClientStreamTracer (io.grpc.ClientStreamTracer)7 ManagedChannel (io.grpc.ManagedChannel)7