From 851065dd0887ca669c3783b0a3379c88ba48cfe3 Mon Sep 17 00:00:00 2001 From: ZHANG Dapeng Date: Mon, 5 Jun 2017 16:32:18 -0700 Subject: [PATCH] examples: migrate unittest examples to GrpcServerRule migrated simple tests using `GrpcServerRule`. Kept the low level in-process channel setup and tear down code for the RouteGuide example to show how users can use in-process directly to set more custom channel builder options when needed. resolves #2490 --- examples/README.md | 4 ++ examples/build.gradle | 1 + examples/pom.xml | 6 +++ .../examples/helloworld/HelloWorldClient.java | 7 +-- .../header/HeaderClientInterceptorTest.java | 43 ++++++------------- .../header/HeaderServerInterceptorTest.java | 34 ++++++--------- .../helloworld/HelloWorldClientTest.java | 37 ++++++---------- .../helloworld/HelloWorldServerTest.java | 40 +++++------------ 8 files changed, 64 insertions(+), 108 deletions(-) diff --git a/examples/README.md b/examples/README.md index bc090cd782..90052997ef 100644 --- a/examples/README.md +++ b/examples/README.md @@ -68,3 +68,7 @@ with a mock/fake service implementation. For testing a gRPC server, create the server as an InProcessServer, and test it against a real client stub with an InProcessChannel. + +The gRPC-java library also provides a JUnit rule, +[GrpcServerRule](../testing/src/main/java/io/grpc/testing/GrpcServerRule.java), to do the starting +up and shutting down boilerplate for you. diff --git a/examples/build.gradle b/examples/build.gradle index f8a5c4cd86..1d4f7fb90f 100644 --- a/examples/build.gradle +++ b/examples/build.gradle @@ -29,6 +29,7 @@ dependencies { compile "io.grpc:grpc-protobuf:${grpcVersion}" compile "io.grpc:grpc-stub:${grpcVersion}" + testCompile "io.grpc:grpc-testing:${grpcVersion}" testCompile "junit:junit:4.11" testCompile "org.mockito:mockito-core:1.9.5" } diff --git a/examples/pom.xml b/examples/pom.xml index 9165f3413b..f2c61b8275 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -28,6 +28,12 @@ grpc-stub ${grpc.version} + + io.grpc + grpc-testing + ${grpc.version} + test + junit junit diff --git a/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java b/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java index 7dc878d893..37fff679c7 100644 --- a/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java +++ b/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java @@ -37,12 +37,13 @@ public class HelloWorldClient { this(ManagedChannelBuilder.forAddress(host, port) // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid // needing certificates. - .usePlaintext(true)); + .usePlaintext(true) + .build()); } /** Construct client for accessing RouteGuide server using the existing channel. */ - HelloWorldClient(ManagedChannelBuilder channelBuilder) { - channel = channelBuilder.build(); + HelloWorldClient(ManagedChannel channel) { + this.channel = channel; blockingStub = GreeterGrpc.newBlockingStub(channel); } diff --git a/examples/src/test/java/io/grpc/examples/header/HeaderClientInterceptorTest.java b/examples/src/test/java/io/grpc/examples/header/HeaderClientInterceptorTest.java index 88a7690a29..b5e2869554 100644 --- a/examples/src/test/java/io/grpc/examples/header/HeaderClientInterceptorTest.java +++ b/examples/src/test/java/io/grpc/examples/header/HeaderClientInterceptorTest.java @@ -21,9 +21,8 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; -import io.grpc.ManagedChannel; +import io.grpc.ClientInterceptors; import io.grpc.Metadata; -import io.grpc.Server; import io.grpc.ServerCall; import io.grpc.ServerCall.Listener; import io.grpc.ServerCallHandler; @@ -35,10 +34,8 @@ import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub; import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase; import io.grpc.examples.helloworld.HelloReply; import io.grpc.examples.helloworld.HelloRequest; -import io.grpc.inprocess.InProcessChannelBuilder; -import io.grpc.inprocess.InProcessServerBuilder; -import org.junit.After; -import org.junit.Before; +import io.grpc.testing.GrpcServerRule; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -55,6 +52,12 @@ import org.mockito.Matchers; */ @RunWith(JUnit4.class) public class HeaderClientInterceptorTest { + /** + * This creates and starts an in-process server, and creates a client with an in-process channel. + * When the test is done, it also shuts down the in-process client and server. + */ + @Rule + public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); private final ServerInterceptor mockServerInterceptor = spy( new ServerInterceptor() { @@ -65,32 +68,12 @@ public class HeaderClientInterceptorTest { } }); - private Server fakeServer; - private ManagedChannel inProcessChannel; - - @Before - public void setUp() throws Exception { - String uniqueServerName = "fake server for " + getClass(); - fakeServer = InProcessServerBuilder.forName(uniqueServerName) - .addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor)) - .directExecutor() - .build() - .start(); - inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName) - .intercept(new HeaderClientInterceptor()) - .directExecutor() - .build(); - } - - @After - public void tearDown() { - inProcessChannel.shutdownNow(); - fakeServer.shutdownNow(); - } - @Test public void clientHeaderDeliveredToServer() { - GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(inProcessChannel); + grpcServerRule.getServiceRegistry() + .addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor)); + GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub( + ClientInterceptors.intercept(grpcServerRule.getChannel(), new HeaderClientInterceptor())); ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(Metadata.class); try { diff --git a/examples/src/test/java/io/grpc/examples/header/HeaderServerInterceptorTest.java b/examples/src/test/java/io/grpc/examples/header/HeaderServerInterceptorTest.java index 3e3d843bda..6b128d320f 100644 --- a/examples/src/test/java/io/grpc/examples/header/HeaderServerInterceptorTest.java +++ b/examples/src/test/java/io/grpc/examples/header/HeaderServerInterceptorTest.java @@ -26,21 +26,18 @@ import io.grpc.Channel; import io.grpc.ClientCall; import io.grpc.ClientInterceptor; import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; -import io.grpc.ManagedChannel; import io.grpc.Metadata; import io.grpc.MethodDescriptor; -import io.grpc.Server; import io.grpc.ServerInterceptors; import io.grpc.examples.helloworld.GreeterGrpc; import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub; import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase; import io.grpc.examples.helloworld.HelloReply; import io.grpc.examples.helloworld.HelloRequest; -import io.grpc.inprocess.InProcessChannelBuilder; -import io.grpc.inprocess.InProcessServerBuilder; import io.grpc.stub.StreamObserver; -import org.junit.After; +import io.grpc.testing.GrpcServerRule; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -56,12 +53,15 @@ import org.mockito.ArgumentCaptor; */ @RunWith(JUnit4.class) public class HeaderServerInterceptorTest { - private Server fakeServer; - private ManagedChannel inProcessChannel; + /** + * This creates and starts an in-process server, and creates a client with an in-process channel. + * When the test is done, it also shuts down the in-process client and server. + */ + @Rule + public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); @Before public void setUp() throws Exception { - String uniqueServerName = "fake server for " + getClass(); GreeterImplBase greeterImplBase = new GreeterImplBase() { @Override @@ -70,18 +70,8 @@ public class HeaderServerInterceptorTest { responseObserver.onCompleted(); } }; - fakeServer = InProcessServerBuilder.forName(uniqueServerName) - .addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor())) - .directExecutor() - .build() - .start(); - inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build(); - } - - @After - public void tearDown() { - inProcessChannel.shutdownNow(); - fakeServer.shutdownNow(); + grpcServerRule.getServiceRegistry() + .addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor())); } @Test @@ -103,8 +93,8 @@ public class HeaderServerInterceptorTest { } SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor(); - GreeterBlockingStub blockingStub = - GreeterGrpc.newBlockingStub(inProcessChannel).withInterceptors(clientInterceptor); + GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(grpcServerRule.getChannel()) + .withInterceptors(clientInterceptor); ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(Metadata.class); blockingStub.sayHello(HelloRequest.getDefaultInstance()); diff --git a/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java b/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java index e373f559c7..b44b6f0e17 100644 --- a/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java +++ b/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java @@ -20,12 +20,10 @@ import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; -import io.grpc.Server; -import io.grpc.inprocess.InProcessChannelBuilder; -import io.grpc.inprocess.InProcessServerBuilder; import io.grpc.stub.StreamObserver; -import org.junit.After; +import io.grpc.testing.GrpcServerRule; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -42,31 +40,22 @@ import org.mockito.Matchers; */ @RunWith(JUnit4.class) public class HelloWorldClientTest { - private final GreeterGrpc.GreeterImplBase serviceImpl = spy(new GreeterGrpc.GreeterImplBase() {}); + /** + * This creates and starts an in-process server, and creates a client with an in-process channel. + * When the test is done, it also shuts down the in-process client and server. + */ + @Rule + public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); - private Server fakeServer; + private final GreeterGrpc.GreeterImplBase serviceImpl = spy(new GreeterGrpc.GreeterImplBase() {}); private HelloWorldClient client; - /** - * Creates and starts a fake in-process server, and creates a client with an in-process channel. - */ @Before public void setUp() throws Exception { - String uniqueServerName = "fake server for " + getClass(); - fakeServer = InProcessServerBuilder - .forName(uniqueServerName).directExecutor().addService(serviceImpl).build().start(); - InProcessChannelBuilder channelBuilder = - InProcessChannelBuilder.forName(uniqueServerName).directExecutor(); - client = new HelloWorldClient(channelBuilder); - } - - /** - * Shuts down the client and server. - */ - @After - public void tearDown() throws Exception { - client.shutdown(); - fakeServer.shutdownNow(); + // Add service. + grpcServerRule.getServiceRegistry().addService(serviceImpl); + // Create a HelloWorldClient using the in-process channel; + client = new HelloWorldClient(grpcServerRule.getChannel()); } /** diff --git a/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldServerTest.java b/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldServerTest.java index fe3a628a25..d0e5effdc8 100644 --- a/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldServerTest.java +++ b/examples/src/test/java/io/grpc/examples/helloworld/HelloWorldServerTest.java @@ -18,13 +18,9 @@ package io.grpc.examples.helloworld; import static org.junit.Assert.assertEquals; -import io.grpc.ManagedChannel; -import io.grpc.Server; import io.grpc.examples.helloworld.HelloWorldServer.GreeterImpl; -import io.grpc.inprocess.InProcessChannelBuilder; -import io.grpc.inprocess.InProcessServerBuilder; -import org.junit.After; -import org.junit.Before; +import io.grpc.testing.GrpcServerRule; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -39,30 +35,12 @@ import org.junit.runners.JUnit4; */ @RunWith(JUnit4.class) public class HelloWorldServerTest { - private static final String UNIQUE_SERVER_NAME = - "in-process server for " + HelloWorldServerTest.class; - private final Server inProcessServer = InProcessServerBuilder - .forName(UNIQUE_SERVER_NAME).addService(new GreeterImpl()).directExecutor().build(); - private final ManagedChannel inProcessChannel = - InProcessChannelBuilder.forName(UNIQUE_SERVER_NAME).directExecutor().build(); - /** - * Creates and starts the server with the {@link InProcessServerBuilder}, - * and creates an in-process channel with the {@link InProcessChannelBuilder}. + * This creates and starts an in-process server, and creates a client with an in-process channel. + * When the test is done, it also shuts down the in-process client and server. */ - @Before - public void setUp() throws Exception { - inProcessServer.start(); - } - - /** - * Shuts down the in-process channel and server. - */ - @After - public void tearDown() { - inProcessChannel.shutdownNow(); - inProcessServer.shutdownNow(); - } + @Rule + public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor(); /** * To test the server, make calls with a real stub using the in-process channel, and verify @@ -70,7 +48,11 @@ public class HelloWorldServerTest { */ @Test public void greeterImpl_replyMessage() throws Exception { - GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(inProcessChannel); + // Add the service to the in-process server. + grpcServerRule.getServiceRegistry().addService(new GreeterImpl()); + + GreeterGrpc.GreeterBlockingStub blockingStub = + GreeterGrpc.newBlockingStub(grpcServerRule.getChannel()); String testName = "test name"; HelloReply reply = blockingStub.sayHello(HelloRequest.newBuilder().setName(testName).build());