mirror of https://github.com/grpc/grpc-java.git
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
This commit is contained in:
parent
a2a42e3396
commit
851065dd08
|
@ -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.
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -28,6 +28,12 @@
|
|||
<artifactId>grpc-stub</artifactId>
|
||||
<version>${grpc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-testing</artifactId>
|
||||
<version>${grpc.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
|
||||
|
||||
try {
|
||||
|
|
|
@ -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<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
|
||||
|
||||
blockingStub.sayHello(HelloRequest.getDefaultInstance());
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue