use of io.grpc.internal.ServiceConfigUtil.PolicySelection 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);
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerConfig_policyShouldBeIgnoredIfConfigExists.
@Test
public void parseLoadBalancerConfig_policyShouldBeIgnoredIfConfigExists() throws Exception {
Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {} } ]," + "\"loadBalancingPolicy\": \"pick_first\" }");
ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
assertThat(parsed).isNotNull();
assertThat(parsed.getError()).isNull();
assertThat(parsed.getConfig()).isInstanceOf(PolicySelection.class);
assertThat(((PolicySelection) parsed.getConfig()).provider.getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerConfig_multipleValidPolicies.
@Test
public void parseLoadBalancerConfig_multipleValidPolicies() throws Exception {
Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [" + "{\"round_robin\": {}}," + "{\"test_lb\": {} } ] }");
ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
assertThat(parsed).isNotNull();
assertThat(parsed.getError()).isNull();
assertThat(parsed.getConfig()).isInstanceOf(PolicySelection.class);
assertThat(((PolicySelection) parsed.getConfig()).provider.getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_useSelectedLbPolicy.
@Test
public void handleResolvedAddressGroups_useSelectedLbPolicy() throws Exception {
Map<String, ?> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {}}]}");
ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
assertThat(lbConfigs.getConfig()).isNotNull();
assertThat(((PolicySelection) lbConfigs.getConfig()).provider.getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
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(lbConfigs.getConfig()).build());
assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
assertThat(lb.getDelegate().getClass().getName()).isEqualTo("io.grpc.util.RoundRobinLoadBalancer");
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerConfig_firstValidSecondInvalidPolicy.
@Test
@SuppressWarnings("unchecked")
public void parseLoadBalancerConfig_firstValidSecondInvalidPolicy() throws Exception {
when(testLbBalancerProvider.parseLoadBalancingPolicyConfig(any(Map.class))).thenReturn(ConfigOrError.fromError(Status.UNKNOWN));
Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [" + "{\"round_robin\": {}}," + "{\"test_lb\": {} } ] }");
ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
assertThat(parsed).isNotNull();
assertThat(parsed.getConfig()).isNotNull();
assertThat(((PolicySelection) parsed.getConfig()).config).isNotNull();
}
Aggregations