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,
|
For testing a gRPC server, create the server as an InProcessServer,
|
||||||
and test it against a real client stub with an InProcessChannel.
|
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-protobuf:${grpcVersion}"
|
||||||
compile "io.grpc:grpc-stub:${grpcVersion}"
|
compile "io.grpc:grpc-stub:${grpcVersion}"
|
||||||
|
|
||||||
|
testCompile "io.grpc:grpc-testing:${grpcVersion}"
|
||||||
testCompile "junit:junit:4.11"
|
testCompile "junit:junit:4.11"
|
||||||
testCompile "org.mockito:mockito-core:1.9.5"
|
testCompile "org.mockito:mockito-core:1.9.5"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,12 @@
|
||||||
<artifactId>grpc-stub</artifactId>
|
<artifactId>grpc-stub</artifactId>
|
||||||
<version>${grpc.version}</version>
|
<version>${grpc.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-testing</artifactId>
|
||||||
|
<version>${grpc.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -37,12 +37,13 @@ public class HelloWorldClient {
|
||||||
this(ManagedChannelBuilder.forAddress(host, port)
|
this(ManagedChannelBuilder.forAddress(host, port)
|
||||||
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
|
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
|
||||||
// needing certificates.
|
// needing certificates.
|
||||||
.usePlaintext(true));
|
.usePlaintext(true)
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Construct client for accessing RouteGuide server using the existing channel. */
|
/** Construct client for accessing RouteGuide server using the existing channel. */
|
||||||
HelloWorldClient(ManagedChannelBuilder<?> channelBuilder) {
|
HelloWorldClient(ManagedChannel channel) {
|
||||||
channel = channelBuilder.build();
|
this.channel = channel;
|
||||||
blockingStub = GreeterGrpc.newBlockingStub(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.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ClientInterceptors;
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.Server;
|
|
||||||
import io.grpc.ServerCall;
|
import io.grpc.ServerCall;
|
||||||
import io.grpc.ServerCall.Listener;
|
import io.grpc.ServerCall.Listener;
|
||||||
import io.grpc.ServerCallHandler;
|
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.GreeterGrpc.GreeterImplBase;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
import io.grpc.inprocess.InProcessChannelBuilder;
|
import io.grpc.testing.GrpcServerRule;
|
||||||
import io.grpc.inprocess.InProcessServerBuilder;
|
import org.junit.Rule;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
@ -55,6 +52,12 @@ import org.mockito.Matchers;
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class HeaderClientInterceptorTest {
|
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(
|
private final ServerInterceptor mockServerInterceptor = spy(
|
||||||
new ServerInterceptor() {
|
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
|
@Test
|
||||||
public void clientHeaderDeliveredToServer() {
|
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);
|
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -26,21 +26,18 @@ import io.grpc.Channel;
|
||||||
import io.grpc.ClientCall;
|
import io.grpc.ClientCall;
|
||||||
import io.grpc.ClientInterceptor;
|
import io.grpc.ClientInterceptor;
|
||||||
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
|
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
|
||||||
import io.grpc.ManagedChannel;
|
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.MethodDescriptor;
|
import io.grpc.MethodDescriptor;
|
||||||
import io.grpc.Server;
|
|
||||||
import io.grpc.ServerInterceptors;
|
import io.grpc.ServerInterceptors;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
|
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
|
import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
import io.grpc.inprocess.InProcessChannelBuilder;
|
|
||||||
import io.grpc.inprocess.InProcessServerBuilder;
|
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
import org.junit.After;
|
import io.grpc.testing.GrpcServerRule;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
@ -56,12 +53,15 @@ import org.mockito.ArgumentCaptor;
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class HeaderServerInterceptorTest {
|
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
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
String uniqueServerName = "fake server for " + getClass();
|
|
||||||
GreeterImplBase greeterImplBase =
|
GreeterImplBase greeterImplBase =
|
||||||
new GreeterImplBase() {
|
new GreeterImplBase() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -70,18 +70,8 @@ public class HeaderServerInterceptorTest {
|
||||||
responseObserver.onCompleted();
|
responseObserver.onCompleted();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fakeServer = InProcessServerBuilder.forName(uniqueServerName)
|
grpcServerRule.getServiceRegistry()
|
||||||
.addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor()))
|
.addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor()));
|
||||||
.directExecutor()
|
|
||||||
.build()
|
|
||||||
.start();
|
|
||||||
inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
inProcessChannel.shutdownNow();
|
|
||||||
fakeServer.shutdownNow();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -103,8 +93,8 @@ public class HeaderServerInterceptorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
|
SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
|
||||||
GreeterBlockingStub blockingStub =
|
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(grpcServerRule.getChannel())
|
||||||
GreeterGrpc.newBlockingStub(inProcessChannel).withInterceptors(clientInterceptor);
|
.withInterceptors(clientInterceptor);
|
||||||
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
|
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
|
||||||
|
|
||||||
blockingStub.sayHello(HelloRequest.getDefaultInstance());
|
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.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
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 io.grpc.stub.StreamObserver;
|
||||||
import org.junit.After;
|
import io.grpc.testing.GrpcServerRule;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
@ -42,31 +40,22 @@ import org.mockito.Matchers;
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class HelloWorldClientTest {
|
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;
|
private HelloWorldClient client;
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates and starts a fake in-process server, and creates a client with an in-process channel.
|
|
||||||
*/
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
String uniqueServerName = "fake server for " + getClass();
|
// Add service.
|
||||||
fakeServer = InProcessServerBuilder
|
grpcServerRule.getServiceRegistry().addService(serviceImpl);
|
||||||
.forName(uniqueServerName).directExecutor().addService(serviceImpl).build().start();
|
// Create a HelloWorldClient using the in-process channel;
|
||||||
InProcessChannelBuilder channelBuilder =
|
client = new HelloWorldClient(grpcServerRule.getChannel());
|
||||||
InProcessChannelBuilder.forName(uniqueServerName).directExecutor();
|
|
||||||
client = new HelloWorldClient(channelBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shuts down the client and server.
|
|
||||||
*/
|
|
||||||
@After
|
|
||||||
public void tearDown() throws Exception {
|
|
||||||
client.shutdown();
|
|
||||||
fakeServer.shutdownNow();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,9 @@ package io.grpc.examples.helloworld;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import io.grpc.ManagedChannel;
|
|
||||||
import io.grpc.Server;
|
|
||||||
import io.grpc.examples.helloworld.HelloWorldServer.GreeterImpl;
|
import io.grpc.examples.helloworld.HelloWorldServer.GreeterImpl;
|
||||||
import io.grpc.inprocess.InProcessChannelBuilder;
|
import io.grpc.testing.GrpcServerRule;
|
||||||
import io.grpc.inprocess.InProcessServerBuilder;
|
import org.junit.Rule;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
@ -39,30 +35,12 @@ import org.junit.runners.JUnit4;
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class HelloWorldServerTest {
|
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},
|
* This creates and starts an in-process server, and creates a client with an in-process channel.
|
||||||
* and creates an in-process channel with the {@link InProcessChannelBuilder}.
|
* When the test is done, it also shuts down the in-process client and server.
|
||||||
*/
|
*/
|
||||||
@Before
|
@Rule
|
||||||
public void setUp() throws Exception {
|
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
|
||||||
inProcessServer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shuts down the in-process channel and server.
|
|
||||||
*/
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
inProcessChannel.shutdownNow();
|
|
||||||
inProcessServer.shutdownNow();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To test the server, make calls with a real stub using the in-process channel, and verify
|
* 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
|
@Test
|
||||||
public void greeterImpl_replyMessage() throws Exception {
|
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";
|
String testName = "test name";
|
||||||
|
|
||||||
HelloReply reply = blockingStub.sayHello(HelloRequest.newBuilder().setName(testName).build());
|
HelloReply reply = blockingStub.sayHello(HelloRequest.newBuilder().setName(testName).build());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue