Skip to content

Commit 1ea1a53

Browse files
committed
grpclb: Implement newNameResolver(io.grpc.Uri).
1 parent a5a465c commit 1ea1a53

2 files changed

Lines changed: 61 additions & 21 deletions

File tree

grpclb/src/main/java/io/grpc/grpclb/SecretGrpclbNameResolverProvider.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
import com.google.common.base.Preconditions;
2020
import com.google.common.base.Stopwatch;
2121
import io.grpc.InternalServiceProviders;
22+
import io.grpc.NameResolver;
2223
import io.grpc.NameResolver.Args;
2324
import io.grpc.NameResolverProvider;
25+
import io.grpc.Uri;
2426
import io.grpc.internal.GrpcUtil;
2527
import java.net.InetSocketAddress;
2628
import java.net.SocketAddress;
2729
import java.net.URI;
2830
import java.util.Collection;
2931
import java.util.Collections;
32+
import java.util.List;
3033

3134
/**
3235
* A provider for {@code io.grpc.grpclb.GrpclbNameResolver}.
@@ -56,27 +59,47 @@ public static final class Provider extends NameResolverProvider {
5659
private static final boolean IS_ANDROID = InternalServiceProviders
5760
.isAndroid(SecretGrpclbNameResolverProvider.class.getClassLoader());
5861

62+
@Override
63+
public NameResolver newNameResolver(Uri targetUri, final NameResolver.Args args) {
64+
if (SCHEME.equals(targetUri.getScheme())) {
65+
List<String> pathSegments = targetUri.getPathSegments();
66+
Preconditions.checkArgument(
67+
!pathSegments.isEmpty(),
68+
"expected 1 path segment in target %s but found %s",
69+
targetUri,
70+
pathSegments);
71+
return newNameResolver(targetUri.getAuthority(), pathSegments.get(0), args);
72+
} else {
73+
return null;
74+
}
75+
}
76+
5977
@Override
6078
public GrpclbNameResolver newNameResolver(URI targetUri, Args args) {
79+
// TODO(jdcormie): Remove once RFC 3986 migration is complete.
6180
if (SCHEME.equals(targetUri.getScheme())) {
6281
String targetPath = Preconditions.checkNotNull(targetUri.getPath(), "targetPath");
6382
Preconditions.checkArgument(
6483
targetPath.startsWith("/"),
6584
"the path component (%s) of the target (%s) must start with '/'",
6685
targetPath, targetUri);
67-
String name = targetPath.substring(1);
68-
return new GrpclbNameResolver(
69-
targetUri.getAuthority(),
70-
name,
71-
args,
72-
GrpcUtil.SHARED_CHANNEL_EXECUTOR,
73-
Stopwatch.createUnstarted(),
74-
IS_ANDROID);
86+
return newNameResolver(targetUri.getAuthority(), targetPath.substring(1), args);
7587
} else {
7688
return null;
7789
}
7890
}
7991

92+
private GrpclbNameResolver newNameResolver(
93+
String authority, String domainNameToResolve, final NameResolver.Args args) {
94+
return new GrpclbNameResolver(
95+
authority,
96+
domainNameToResolve,
97+
args,
98+
GrpcUtil.SHARED_CHANNEL_EXECUTOR,
99+
Stopwatch.createUnstarted(),
100+
IS_ANDROID);
101+
}
102+
80103
@Override
81104
public String getDefaultScheme() {
82105
return SCHEME;

grpclb/src/test/java/io/grpc/grpclb/SecretGrpclbNameResolverProviderTest.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424
import io.grpc.NameResolver;
2525
import io.grpc.NameResolver.ServiceConfigParser;
2626
import io.grpc.SynchronizationContext;
27+
import io.grpc.Uri;
2728
import io.grpc.internal.DnsNameResolverProvider;
2829
import io.grpc.internal.GrpcUtil;
2930
import java.net.URI;
31+
import java.util.Arrays;
3032
import org.junit.Test;
3133
import org.junit.runner.RunWith;
32-
import org.junit.runners.JUnit4;
34+
import org.junit.runners.Parameterized;
35+
import org.junit.runners.Parameterized.Parameter;
36+
import org.junit.runners.Parameterized.Parameters;
3337

3438
/** Unit tests for {@link SecretGrpclbNameResolverProvider}. */
35-
@RunWith(JUnit4.class)
39+
@RunWith(Parameterized.class)
3640
public class SecretGrpclbNameResolverProviderTest {
3741

3842
private final SynchronizationContext syncContext = new SynchronizationContext(
@@ -53,6 +57,13 @@ public void uncaughtException(Thread t, Throwable e) {
5357
private SecretGrpclbNameResolverProvider.Provider provider =
5458
new SecretGrpclbNameResolverProvider.Provider();
5559

60+
@Parameters(name = "enableRfc3986UrisParam={0}")
61+
public static Iterable<Object[]> data() {
62+
return Arrays.asList(new Object[][] {{true}, {false}});
63+
}
64+
65+
@Parameter public boolean enableRfc3986UrisParam;
66+
5667
@Test
5768
public void isAvailable() {
5869
assertThat(provider.isAvailable()).isTrue();
@@ -66,43 +77,49 @@ public void priority_shouldBeHigherThanDefaultDnsNameResolver() {
6677
}
6778

6879
@Test
69-
public void newNameResolver() {
70-
assertThat(provider.newNameResolver(URI.create("dns:///localhost:443"), args))
80+
public void newNameResolverReturnsCorrectType() {
81+
assertThat(newNameResolver("dns:///localhost:443", args))
7182
.isInstanceOf(GrpclbNameResolver.class);
72-
assertThat(provider.newNameResolver(URI.create("notdns:///localhost:443"), args)).isNull();
83+
assertThat(newNameResolver("notdns:///localhost:443", args)).isNull();
7384
}
7485

7586
@Test
7687
public void invalidDnsName() throws Exception {
77-
testInvalidUri(URI.create("dns:/%5Binvalid%5D"));
88+
testInvalidUri("dns:/%5Binvalid%5D");
7889
}
7990

8091
@Test
8192
public void validIpv6() throws Exception {
82-
testValidUri(URI.create("dns:/%5B::1%5D"));
93+
testValidUri("dns:/%5B::1%5D");
8394
}
8495

8596
@Test
8697
public void validDnsNameWithoutPort() throws Exception {
87-
testValidUri(URI.create("dns:/foo.googleapis.com"));
98+
testValidUri("dns:/foo.googleapis.com");
8899
}
89100

90101
@Test
91102
public void validDnsNameWithPort() throws Exception {
92-
testValidUri(URI.create("dns:/foo.googleapis.com:456"));
103+
testValidUri("dns:/foo.googleapis.com:456");
93104
}
94105

95-
private void testInvalidUri(URI uri) {
106+
private void testInvalidUri(String uri) {
96107
try {
97-
provider.newNameResolver(uri, args);
108+
newNameResolver(uri, args);
98109
fail("Should have failed");
99110
} catch (IllegalArgumentException e) {
100111
// expected
101112
}
102113
}
103114

104-
private void testValidUri(URI uri) {
105-
GrpclbNameResolver resolver = provider.newNameResolver(uri, args);
115+
private void testValidUri(String uri) {
116+
NameResolver resolver = newNameResolver(uri, args);
106117
assertThat(resolver).isNotNull();
107118
}
119+
120+
private NameResolver newNameResolver(String uriString, NameResolver.Args args) {
121+
return enableRfc3986UrisParam
122+
? provider.newNameResolver(Uri.create(uriString), args)
123+
: provider.newNameResolver(URI.create(uriString), args);
124+
}
108125
}

0 commit comments

Comments
 (0)