Skip to content

Commit d15daed

Browse files
committed
interop-testing: Modernize stress test channel creation
OverrideAuthority() is the modern alternative to rewriting the InetAddress, and can also work if we change the channel creation to use target strings instead. We can use test CAs now without resorting to the Netty-specific APIs. After this change, only 4 classes in interop-testing depend on Netty.
1 parent 92174be commit d15daed

File tree

2 files changed

+30
-43
lines changed

2 files changed

+30
-43
lines changed

interop-testing/src/main/java/io/grpc/testing/integration/StressTestClient.java

+25-28
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,22 @@
3232
import com.google.common.util.concurrent.ListenableFuture;
3333
import com.google.common.util.concurrent.ListeningExecutorService;
3434
import com.google.common.util.concurrent.MoreExecutors;
35+
import io.grpc.ChannelCredentials;
36+
import io.grpc.Grpc;
37+
import io.grpc.InsecureChannelCredentials;
3538
import io.grpc.ManagedChannel;
3639
import io.grpc.ManagedChannelBuilder;
3740
import io.grpc.Server;
3841
import io.grpc.ServerBuilder;
3942
import io.grpc.Status;
4043
import io.grpc.StatusException;
41-
import io.grpc.netty.GrpcSslContexts;
42-
import io.grpc.netty.NegotiationType;
43-
import io.grpc.netty.NettyChannelBuilder;
44+
import io.grpc.TlsChannelCredentials;
4445
import io.grpc.stub.StreamObserver;
4546
import io.grpc.testing.TlsTesting;
46-
import io.netty.handler.ssl.SslContext;
4747
import java.io.IOException;
48-
import java.net.InetAddress;
4948
import java.net.InetSocketAddress;
5049
import java.net.URI;
5150
import java.net.URISyntaxException;
52-
import java.net.UnknownHostException;
5351
import java.util.ArrayList;
5452
import java.util.Collections;
5553
import java.util.Iterator;
@@ -296,19 +294,9 @@ private List<InetSocketAddress> parseServerAddresses(String addressesStr) {
296294
List<InetSocketAddress> addresses = new ArrayList<>();
297295

298296
for (List<String> namePort : parseCommaSeparatedTuples(addressesStr)) {
299-
InetAddress address;
300297
String name = namePort.get(0);
301298
int port = Integer.valueOf(namePort.get(1));
302-
try {
303-
address = InetAddress.getByName(name);
304-
if (serverHostOverride != null) {
305-
// Force the hostname to match the cert the server uses.
306-
address = InetAddress.getByAddress(serverHostOverride, address.getAddress());
307-
}
308-
} catch (UnknownHostException ex) {
309-
throw new RuntimeException(ex);
310-
}
311-
addresses.add(new InetSocketAddress(address, port));
299+
addresses.add(new InetSocketAddress(name, port));
312300
}
313301

314302
return addresses;
@@ -341,19 +329,28 @@ private static List<List<String>> parseCommaSeparatedTuples(String str) {
341329
}
342330

343331
private ManagedChannel createChannel(InetSocketAddress address) {
344-
SslContext sslContext = null;
345-
if (useTestCa) {
346-
try {
347-
sslContext = GrpcSslContexts.forClient().trustManager(
348-
TlsTesting.loadCert("ca.pem")).build();
349-
} catch (Exception ex) {
350-
throw new RuntimeException(ex);
332+
ChannelCredentials channelCredentials;
333+
if (useTls) {
334+
if (useTestCa) {
335+
try {
336+
channelCredentials = TlsChannelCredentials.newBuilder()
337+
.trustManager(TlsTesting.loadCert("ca.pem"))
338+
.build();
339+
} catch (Exception ex) {
340+
throw new RuntimeException(ex);
341+
}
342+
} else {
343+
channelCredentials = TlsChannelCredentials.create();
351344
}
345+
} else {
346+
channelCredentials = InsecureChannelCredentials.create();
347+
}
348+
ManagedChannelBuilder<?> builder = Grpc.newChannelBuilderForAddress(
349+
address.getHostString(), address.getPort(), channelCredentials);
350+
if (serverHostOverride != null) {
351+
builder.overrideAuthority(serverHostOverride);
352352
}
353-
return NettyChannelBuilder.forAddress(address)
354-
.negotiationType(useTls ? NegotiationType.TLS : NegotiationType.PLAINTEXT)
355-
.sslContext(sslContext)
356-
.build();
353+
return builder.build();
357354
}
358355

359356
private static String serverAddressesToString(List<InetSocketAddress> addresses) {

interop-testing/src/test/java/io/grpc/testing/integration/StressTestClientTest.java

+5-15
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import java.util.Arrays;
3333
import java.util.List;
3434
import java.util.Set;
35-
import java.util.concurrent.TimeUnit;
36-
import java.util.concurrent.locks.LockSupport;
3735
import org.junit.Rule;
3836
import org.junit.Test;
3937
import org.junit.rules.Timeout;
@@ -103,27 +101,19 @@ public void allCommandlineSwitchesAreSupported() {
103101
assertEquals(9090, client.metricsPort());
104102
}
105103

106-
@Test
107-
public void serverHostOverrideShouldBeApplied() {
108-
StressTestClient client = new StressTestClient();
109-
client.parseArgs(new String[] {
110-
"--server_addresses=localhost:8080",
111-
"--server_host_override=foo.test.google.fr",
112-
});
113-
114-
assertEquals("foo.test.google.fr", client.addresses().get(0).getHostName());
115-
}
116-
117104
@Test
118105
public void gaugesShouldBeExported() throws Exception {
119106

120107
TestServiceServer server = new TestServiceServer();
121-
server.parseArgs(new String[]{"--port=" + 0, "--use_tls=false"});
108+
server.parseArgs(new String[]{"--port=" + 0, "--use_tls=true"});
122109
server.start();
123110

124111
StressTestClient client = new StressTestClient();
125112
client.parseArgs(new String[] {"--test_cases=empty_unary:1",
126113
"--server_addresses=localhost:" + server.getPort(), "--metrics_port=" + 0,
114+
"--server_host_override=foo.test.google.fr",
115+
"--use_tls=true",
116+
"--use_test_ca=true",
127117
"--num_stubs_per_channel=2"});
128118
client.startMetricsService();
129119
client.runStressTest();
@@ -142,7 +132,7 @@ public void gaugesShouldBeExported() throws Exception {
142132
List<GaugeResponse> allGauges =
143133
ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
144134
while (allGauges.size() < gaugeNames.size()) {
145-
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100));
135+
Thread.sleep(100);
146136
allGauges = ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
147137
}
148138

0 commit comments

Comments
 (0)