mirror of https://github.com/grpc/grpc-java.git
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.
This commit is contained in:
parent
92174be3df
commit
d15daed5e4
|
|
@ -32,24 +32,22 @@ import com.google.common.util.concurrent.Futures;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import com.google.common.util.concurrent.ListeningExecutorService;
|
import com.google.common.util.concurrent.ListeningExecutorService;
|
||||||
import com.google.common.util.concurrent.MoreExecutors;
|
import com.google.common.util.concurrent.MoreExecutors;
|
||||||
|
import io.grpc.ChannelCredentials;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
import io.grpc.ManagedChannelBuilder;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.StatusException;
|
import io.grpc.StatusException;
|
||||||
import io.grpc.netty.GrpcSslContexts;
|
import io.grpc.TlsChannelCredentials;
|
||||||
import io.grpc.netty.NegotiationType;
|
|
||||||
import io.grpc.netty.NettyChannelBuilder;
|
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
import io.grpc.testing.TlsTesting;
|
import io.grpc.testing.TlsTesting;
|
||||||
import io.netty.handler.ssl.SslContext;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
@ -296,19 +294,9 @@ public class StressTestClient {
|
||||||
List<InetSocketAddress> addresses = new ArrayList<>();
|
List<InetSocketAddress> addresses = new ArrayList<>();
|
||||||
|
|
||||||
for (List<String> namePort : parseCommaSeparatedTuples(addressesStr)) {
|
for (List<String> namePort : parseCommaSeparatedTuples(addressesStr)) {
|
||||||
InetAddress address;
|
|
||||||
String name = namePort.get(0);
|
String name = namePort.get(0);
|
||||||
int port = Integer.valueOf(namePort.get(1));
|
int port = Integer.valueOf(namePort.get(1));
|
||||||
try {
|
addresses.add(new InetSocketAddress(name, port));
|
||||||
address = InetAddress.getByName(name);
|
|
||||||
if (serverHostOverride != null) {
|
|
||||||
// Force the hostname to match the cert the server uses.
|
|
||||||
address = InetAddress.getByAddress(serverHostOverride, address.getAddress());
|
|
||||||
}
|
|
||||||
} catch (UnknownHostException ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
addresses.add(new InetSocketAddress(address, port));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return addresses;
|
return addresses;
|
||||||
|
|
@ -341,19 +329,28 @@ public class StressTestClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ManagedChannel createChannel(InetSocketAddress address) {
|
private ManagedChannel createChannel(InetSocketAddress address) {
|
||||||
SslContext sslContext = null;
|
ChannelCredentials channelCredentials;
|
||||||
if (useTestCa) {
|
if (useTls) {
|
||||||
try {
|
if (useTestCa) {
|
||||||
sslContext = GrpcSslContexts.forClient().trustManager(
|
try {
|
||||||
TlsTesting.loadCert("ca.pem")).build();
|
channelCredentials = TlsChannelCredentials.newBuilder()
|
||||||
} catch (Exception ex) {
|
.trustManager(TlsTesting.loadCert("ca.pem"))
|
||||||
throw new RuntimeException(ex);
|
.build();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
channelCredentials = TlsChannelCredentials.create();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
channelCredentials = InsecureChannelCredentials.create();
|
||||||
}
|
}
|
||||||
return NettyChannelBuilder.forAddress(address)
|
ManagedChannelBuilder<?> builder = Grpc.newChannelBuilderForAddress(
|
||||||
.negotiationType(useTls ? NegotiationType.TLS : NegotiationType.PLAINTEXT)
|
address.getHostString(), address.getPort(), channelCredentials);
|
||||||
.sslContext(sslContext)
|
if (serverHostOverride != null) {
|
||||||
.build();
|
builder.overrideAuthority(serverHostOverride);
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String serverAddressesToString(List<InetSocketAddress> addresses) {
|
private static String serverAddressesToString(List<InetSocketAddress> addresses) {
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,6 @@ import java.net.InetSocketAddress;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.locks.LockSupport;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.Timeout;
|
import org.junit.rules.Timeout;
|
||||||
|
|
@ -103,27 +101,19 @@ public class StressTestClientTest {
|
||||||
assertEquals(9090, client.metricsPort());
|
assertEquals(9090, client.metricsPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void serverHostOverrideShouldBeApplied() {
|
|
||||||
StressTestClient client = new StressTestClient();
|
|
||||||
client.parseArgs(new String[] {
|
|
||||||
"--server_addresses=localhost:8080",
|
|
||||||
"--server_host_override=foo.test.google.fr",
|
|
||||||
});
|
|
||||||
|
|
||||||
assertEquals("foo.test.google.fr", client.addresses().get(0).getHostName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void gaugesShouldBeExported() throws Exception {
|
public void gaugesShouldBeExported() throws Exception {
|
||||||
|
|
||||||
TestServiceServer server = new TestServiceServer();
|
TestServiceServer server = new TestServiceServer();
|
||||||
server.parseArgs(new String[]{"--port=" + 0, "--use_tls=false"});
|
server.parseArgs(new String[]{"--port=" + 0, "--use_tls=true"});
|
||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
StressTestClient client = new StressTestClient();
|
StressTestClient client = new StressTestClient();
|
||||||
client.parseArgs(new String[] {"--test_cases=empty_unary:1",
|
client.parseArgs(new String[] {"--test_cases=empty_unary:1",
|
||||||
"--server_addresses=localhost:" + server.getPort(), "--metrics_port=" + 0,
|
"--server_addresses=localhost:" + server.getPort(), "--metrics_port=" + 0,
|
||||||
|
"--server_host_override=foo.test.google.fr",
|
||||||
|
"--use_tls=true",
|
||||||
|
"--use_test_ca=true",
|
||||||
"--num_stubs_per_channel=2"});
|
"--num_stubs_per_channel=2"});
|
||||||
client.startMetricsService();
|
client.startMetricsService();
|
||||||
client.runStressTest();
|
client.runStressTest();
|
||||||
|
|
@ -142,7 +132,7 @@ public class StressTestClientTest {
|
||||||
List<GaugeResponse> allGauges =
|
List<GaugeResponse> allGauges =
|
||||||
ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
|
ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
|
||||||
while (allGauges.size() < gaugeNames.size()) {
|
while (allGauges.size() < gaugeNames.size()) {
|
||||||
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100));
|
Thread.sleep(100);
|
||||||
allGauges = ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
|
allGauges = ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue