use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.
the class ManagedChannelImplTest method channelTracing_serviceConfigChange.
@Test
public void channelTracing_serviceConfigChange() throws Exception {
timer.forwardNanos(1234);
channelBuilder.maxTraceEvents(10);
List<EquivalentAddressGroup> servers = new ArrayList<>();
servers.add(new EquivalentAddressGroup(socketAddress));
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(servers).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
createChannel();
int prevSize = getStats(channel).channelTrace.events.size();
ManagedChannelServiceConfig mcsc1 = createManagedChannelServiceConfig(ImmutableMap.<String, Object>of(), new PolicySelection(mockLoadBalancerProvider, null));
ResolutionResult resolutionResult1 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
}, new SocketAddress() {
})))).setServiceConfig(ConfigOrError.fromConfig(mcsc1)).build();
nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult1);
assertThat(getStats(channel).channelTrace.events).hasSize(prevSize + 1);
assertThat(getStats(channel).channelTrace.events.get(prevSize)).isEqualTo(new ChannelTrace.Event.Builder().setDescription("Service config changed").setSeverity(ChannelTrace.Event.Severity.CT_INFO).setTimestampNanos(timer.getTicker().read()).build());
prevSize = getStats(channel).channelTrace.events.size();
ResolutionResult resolutionResult2 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
}, new SocketAddress() {
})))).setServiceConfig(ConfigOrError.fromConfig(mcsc1)).build();
nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult2);
assertThat(getStats(channel).channelTrace.events).hasSize(prevSize);
prevSize = getStats(channel).channelTrace.events.size();
timer.forwardNanos(1234);
ResolutionResult resolutionResult3 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
}, new SocketAddress() {
})))).setServiceConfig(ConfigOrError.fromConfig(ManagedChannelServiceConfig.empty())).build();
nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult3);
assertThat(getStats(channel).channelTrace.events).hasSize(prevSize + 1);
assertThat(getStats(channel).channelTrace.events.get(prevSize)).isEqualTo(new ChannelTrace.Event.Builder().setDescription("Service config changed").setSeverity(ChannelTrace.Event.Severity.CT_INFO).setTimestampNanos(timer.getTicker().read()).build());
}
use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.
the class DnsNameResolverTest method resolve_presentResourceResolver.
@Test
public void resolve_presentResourceResolver() throws Exception {
DnsNameResolver.enableTxt = true;
InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
AddressResolver mockAddressResolver = mock(AddressResolver.class);
when(mockAddressResolver.resolveAddress(anyString())).thenReturn(Collections.singletonList(backendAddr));
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.singletonList("grpc_config=[{\"clientLanguage\": [\"java\"], \"serviceConfig\": {}}]"));
ServiceConfigParser serviceConfigParser = new ServiceConfigParser() {
@Override
public ConfigOrError parseServiceConfig(Map<String, ?> rawServiceConfig) {
return ConfigOrError.fromConfig(rawServiceConfig);
}
};
NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(DEFAULT_PORT).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(serviceConfigParser).build();
String name = "foo.googleapis.com";
DnsNameResolver resolver = newResolver(name, Stopwatch.createUnstarted(), false, args);
resolver.setAddressResolver(mockAddressResolver);
resolver.setResourceResolver(mockResourceResolver);
resolver.start(mockListener);
assertEquals(1, fakeExecutor.runDueTasks());
verify(mockListener).onResult(resultCaptor.capture());
ResolutionResult result = resultCaptor.getValue();
InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
assertThat(result.getServiceConfig().getConfig()).isNotNull();
verify(mockAddressResolver).resolveAddress(name);
verify(mockResourceResolver).resolveTxt("_grpc_config." + name);
}
use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.
the class DnsNameResolverTest method resolve_nullResourceResolver.
@Test
public void resolve_nullResourceResolver() throws Exception {
DnsNameResolver.enableTxt = true;
InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
AddressResolver mockAddressResolver = mock(AddressResolver.class);
when(mockAddressResolver.resolveAddress(anyString())).thenReturn(Collections.singletonList(backendAddr));
String name = "foo.googleapis.com";
DnsNameResolver resolver = newResolver(name, 81);
resolver.setAddressResolver(mockAddressResolver);
resolver.setResourceResolver(null);
resolver.start(mockListener);
assertEquals(1, fakeExecutor.runDueTasks());
verify(mockListener).onResult(resultCaptor.capture());
ResolutionResult result = resultCaptor.getValue();
InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
verify(mockAddressResolver).resolveAddress(name);
assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
assertThat(result.getServiceConfig()).isNull();
}
use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.
the class DnsNameResolverTest method resolve_serviceConfigMalformed_serviceConfigError.
@Test
public void resolve_serviceConfigMalformed_serviceConfigError() throws Exception {
DnsNameResolver.enableTxt = true;
InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
AddressResolver mockAddressResolver = mock(AddressResolver.class);
when(mockAddressResolver.resolveAddress(anyString())).thenReturn(Collections.singletonList(backendAddr));
String name = "foo.googleapis.com";
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.singletonList("grpc_config=something invalid"));
DnsNameResolver resolver = newResolver(name, 81);
resolver.setAddressResolver(mockAddressResolver);
resolver.setResourceResolver(mockResourceResolver);
resolver.start(mockListener);
assertEquals(1, fakeExecutor.runDueTasks());
verify(mockListener).onResult(resultCaptor.capture());
ResolutionResult result = resultCaptor.getValue();
InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
verify(mockAddressResolver).resolveAddress(name);
assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
assertThat(result.getServiceConfig()).isNotNull();
assertThat(result.getServiceConfig().getError()).isNotNull();
verify(mockResourceResolver).resolveTxt(anyString());
}
use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.
the class XdsNameResolverTest method resolved_noTimeout.
@Test
public void resolved_noTimeout() {
resolver.start(mockListener);
FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
Route route = Route.forAction(RouteMatch.withPathExactOnly(call1.getFullMethodNameForPath()), RouteAction.forCluster(cluster1, Collections.<HashPolicy>emptyList(), null, // per-route timeout unset
null), ImmutableMap.<String, FilterConfig>of());
VirtualHost virtualHost = VirtualHost.create("does not matter", Collections.singletonList(AUTHORITY), Collections.singletonList(route), ImmutableMap.<String, FilterConfig>of());
xdsClient.deliverLdsUpdate(0L, Collections.singletonList(virtualHost));
verify(mockListener).onResult(resolutionResultCaptor.capture());
ResolutionResult result = resolutionResultCaptor.getValue();
InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
assertCallSelectClusterResult(call1, configSelector, cluster1, null);
}
Aggregations