Skip to content

Commit 851065d

Browse files
authored
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 grpc#2490
1 parent a2a42e3 commit 851065d

8 files changed

Lines changed: 64 additions & 108 deletions

File tree

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ with a mock/fake service implementation.
6868

6969
For testing a gRPC server, create the server as an InProcessServer,
7070
and test it against a real client stub with an InProcessChannel.
71+
72+
The gRPC-java library also provides a JUnit rule,
73+
[GrpcServerRule](../testing/src/main/java/io/grpc/testing/GrpcServerRule.java), to do the starting
74+
up and shutting down boilerplate for you.

examples/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
compile "io.grpc:grpc-protobuf:${grpcVersion}"
3030
compile "io.grpc:grpc-stub:${grpcVersion}"
3131

32+
testCompile "io.grpc:grpc-testing:${grpcVersion}"
3233
testCompile "junit:junit:4.11"
3334
testCompile "org.mockito:mockito-core:1.9.5"
3435
}

examples/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<artifactId>grpc-stub</artifactId>
2929
<version>${grpc.version}</version>
3030
</dependency>
31+
<dependency>
32+
<groupId>io.grpc</groupId>
33+
<artifactId>grpc-testing</artifactId>
34+
<version>${grpc.version}</version>
35+
<scope>test</scope>
36+
</dependency>
3137
<dependency>
3238
<groupId>junit</groupId>
3339
<artifactId>junit</artifactId>

examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public HelloWorldClient(String host, int port) {
3737
this(ManagedChannelBuilder.forAddress(host, port)
3838
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
3939
// needing certificates.
40-
.usePlaintext(true));
40+
.usePlaintext(true)
41+
.build());
4142
}
4243

4344
/** Construct client for accessing RouteGuide server using the existing channel. */
44-
HelloWorldClient(ManagedChannelBuilder<?> channelBuilder) {
45-
channel = channelBuilder.build();
45+
HelloWorldClient(ManagedChannel channel) {
46+
this.channel = channel;
4647
blockingStub = GreeterGrpc.newBlockingStub(channel);
4748
}
4849

examples/src/test/java/io/grpc/examples/header/HeaderClientInterceptorTest.java

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
import static org.mockito.Mockito.spy;
2222
import static org.mockito.Mockito.verify;
2323

24-
import io.grpc.ManagedChannel;
24+
import io.grpc.ClientInterceptors;
2525
import io.grpc.Metadata;
26-
import io.grpc.Server;
2726
import io.grpc.ServerCall;
2827
import io.grpc.ServerCall.Listener;
2928
import io.grpc.ServerCallHandler;
@@ -35,10 +34,8 @@
3534
import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
3635
import io.grpc.examples.helloworld.HelloReply;
3736
import io.grpc.examples.helloworld.HelloRequest;
38-
import io.grpc.inprocess.InProcessChannelBuilder;
39-
import io.grpc.inprocess.InProcessServerBuilder;
40-
import org.junit.After;
41-
import org.junit.Before;
37+
import io.grpc.testing.GrpcServerRule;
38+
import org.junit.Rule;
4239
import org.junit.Test;
4340
import org.junit.runner.RunWith;
4441
import org.junit.runners.JUnit4;
@@ -55,6 +52,12 @@
5552
*/
5653
@RunWith(JUnit4.class)
5754
public class HeaderClientInterceptorTest {
55+
/**
56+
* This creates and starts an in-process server, and creates a client with an in-process channel.
57+
* When the test is done, it also shuts down the in-process client and server.
58+
*/
59+
@Rule
60+
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
5861

5962
private final ServerInterceptor mockServerInterceptor = spy(
6063
new ServerInterceptor() {
@@ -65,32 +68,12 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(
6568
}
6669
});
6770

68-
private Server fakeServer;
69-
private ManagedChannel inProcessChannel;
70-
71-
@Before
72-
public void setUp() throws Exception {
73-
String uniqueServerName = "fake server for " + getClass();
74-
fakeServer = InProcessServerBuilder.forName(uniqueServerName)
75-
.addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor))
76-
.directExecutor()
77-
.build()
78-
.start();
79-
inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName)
80-
.intercept(new HeaderClientInterceptor())
81-
.directExecutor()
82-
.build();
83-
}
84-
85-
@After
86-
public void tearDown() {
87-
inProcessChannel.shutdownNow();
88-
fakeServer.shutdownNow();
89-
}
90-
9171
@Test
9272
public void clientHeaderDeliveredToServer() {
93-
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(inProcessChannel);
73+
grpcServerRule.getServiceRegistry()
74+
.addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor));
75+
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(
76+
ClientInterceptors.intercept(grpcServerRule.getChannel(), new HeaderClientInterceptor()));
9477
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
9578

9679
try {

examples/src/test/java/io/grpc/examples/header/HeaderServerInterceptorTest.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,18 @@
2626
import io.grpc.ClientCall;
2727
import io.grpc.ClientInterceptor;
2828
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
29-
import io.grpc.ManagedChannel;
3029
import io.grpc.Metadata;
3130
import io.grpc.MethodDescriptor;
32-
import io.grpc.Server;
3331
import io.grpc.ServerInterceptors;
3432
import io.grpc.examples.helloworld.GreeterGrpc;
3533
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
3634
import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
3735
import io.grpc.examples.helloworld.HelloReply;
3836
import io.grpc.examples.helloworld.HelloRequest;
39-
import io.grpc.inprocess.InProcessChannelBuilder;
40-
import io.grpc.inprocess.InProcessServerBuilder;
4137
import io.grpc.stub.StreamObserver;
42-
import org.junit.After;
38+
import io.grpc.testing.GrpcServerRule;
4339
import org.junit.Before;
40+
import org.junit.Rule;
4441
import org.junit.Test;
4542
import org.junit.runner.RunWith;
4643
import org.junit.runners.JUnit4;
@@ -56,12 +53,15 @@
5653
*/
5754
@RunWith(JUnit4.class)
5855
public class HeaderServerInterceptorTest {
59-
private Server fakeServer;
60-
private ManagedChannel inProcessChannel;
56+
/**
57+
* This creates and starts an in-process server, and creates a client with an in-process channel.
58+
* When the test is done, it also shuts down the in-process client and server.
59+
*/
60+
@Rule
61+
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
6162

6263
@Before
6364
public void setUp() throws Exception {
64-
String uniqueServerName = "fake server for " + getClass();
6565
GreeterImplBase greeterImplBase =
6666
new GreeterImplBase() {
6767
@Override
@@ -70,18 +70,8 @@ public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseOb
7070
responseObserver.onCompleted();
7171
}
7272
};
73-
fakeServer = InProcessServerBuilder.forName(uniqueServerName)
74-
.addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor()))
75-
.directExecutor()
76-
.build()
77-
.start();
78-
inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build();
79-
}
80-
81-
@After
82-
public void tearDown() {
83-
inProcessChannel.shutdownNow();
84-
fakeServer.shutdownNow();
73+
grpcServerRule.getServiceRegistry()
74+
.addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor()));
8575
}
8676

8777
@Test
@@ -103,8 +93,8 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
10393
}
10494

10595
SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
106-
GreeterBlockingStub blockingStub =
107-
GreeterGrpc.newBlockingStub(inProcessChannel).withInterceptors(clientInterceptor);
96+
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(grpcServerRule.getChannel())
97+
.withInterceptors(clientInterceptor);
10898
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
10999

110100
blockingStub.sayHello(HelloRequest.getDefaultInstance());

examples/src/test/java/io/grpc/examples/helloworld/HelloWorldClientTest.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
import static org.mockito.Mockito.spy;
2121
import static org.mockito.Mockito.verify;
2222

23-
import io.grpc.Server;
24-
import io.grpc.inprocess.InProcessChannelBuilder;
25-
import io.grpc.inprocess.InProcessServerBuilder;
2623
import io.grpc.stub.StreamObserver;
27-
import org.junit.After;
24+
import io.grpc.testing.GrpcServerRule;
2825
import org.junit.Before;
26+
import org.junit.Rule;
2927
import org.junit.Test;
3028
import org.junit.runner.RunWith;
3129
import org.junit.runners.JUnit4;
@@ -42,31 +40,22 @@
4240
*/
4341
@RunWith(JUnit4.class)
4442
public class HelloWorldClientTest {
45-
private final GreeterGrpc.GreeterImplBase serviceImpl = spy(new GreeterGrpc.GreeterImplBase() {});
43+
/**
44+
* This creates and starts an in-process server, and creates a client with an in-process channel.
45+
* When the test is done, it also shuts down the in-process client and server.
46+
*/
47+
@Rule
48+
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
4649

47-
private Server fakeServer;
50+
private final GreeterGrpc.GreeterImplBase serviceImpl = spy(new GreeterGrpc.GreeterImplBase() {});
4851
private HelloWorldClient client;
4952

50-
/**
51-
* Creates and starts a fake in-process server, and creates a client with an in-process channel.
52-
*/
5353
@Before
5454
public void setUp() throws Exception {
55-
String uniqueServerName = "fake server for " + getClass();
56-
fakeServer = InProcessServerBuilder
57-
.forName(uniqueServerName).directExecutor().addService(serviceImpl).build().start();
58-
InProcessChannelBuilder channelBuilder =
59-
InProcessChannelBuilder.forName(uniqueServerName).directExecutor();
60-
client = new HelloWorldClient(channelBuilder);
61-
}
62-
63-
/**
64-
* Shuts down the client and server.
65-
*/
66-
@After
67-
public void tearDown() throws Exception {
68-
client.shutdown();
69-
fakeServer.shutdownNow();
55+
// Add service.
56+
grpcServerRule.getServiceRegistry().addService(serviceImpl);
57+
// Create a HelloWorldClient using the in-process channel;
58+
client = new HelloWorldClient(grpcServerRule.getChannel());
7059
}
7160

7261
/**

examples/src/test/java/io/grpc/examples/helloworld/HelloWorldServerTest.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020

21-
import io.grpc.ManagedChannel;
22-
import io.grpc.Server;
2321
import io.grpc.examples.helloworld.HelloWorldServer.GreeterImpl;
24-
import io.grpc.inprocess.InProcessChannelBuilder;
25-
import io.grpc.inprocess.InProcessServerBuilder;
26-
import org.junit.After;
27-
import org.junit.Before;
22+
import io.grpc.testing.GrpcServerRule;
23+
import org.junit.Rule;
2824
import org.junit.Test;
2925
import org.junit.runner.RunWith;
3026
import org.junit.runners.JUnit4;
@@ -39,38 +35,24 @@
3935
*/
4036
@RunWith(JUnit4.class)
4137
public class HelloWorldServerTest {
42-
private static final String UNIQUE_SERVER_NAME =
43-
"in-process server for " + HelloWorldServerTest.class;
44-
private final Server inProcessServer = InProcessServerBuilder
45-
.forName(UNIQUE_SERVER_NAME).addService(new GreeterImpl()).directExecutor().build();
46-
private final ManagedChannel inProcessChannel =
47-
InProcessChannelBuilder.forName(UNIQUE_SERVER_NAME).directExecutor().build();
48-
4938
/**
50-
* Creates and starts the server with the {@link InProcessServerBuilder},
51-
* and creates an in-process channel with the {@link InProcessChannelBuilder}.
39+
* This creates and starts an in-process server, and creates a client with an in-process channel.
40+
* When the test is done, it also shuts down the in-process client and server.
5241
*/
53-
@Before
54-
public void setUp() throws Exception {
55-
inProcessServer.start();
56-
}
57-
58-
/**
59-
* Shuts down the in-process channel and server.
60-
*/
61-
@After
62-
public void tearDown() {
63-
inProcessChannel.shutdownNow();
64-
inProcessServer.shutdownNow();
65-
}
42+
@Rule
43+
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
6644

6745
/**
6846
* To test the server, make calls with a real stub using the in-process channel, and verify
6947
* behaviors or state changes from the client side.
7048
*/
7149
@Test
7250
public void greeterImpl_replyMessage() throws Exception {
73-
GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(inProcessChannel);
51+
// Add the service to the in-process server.
52+
grpcServerRule.getServiceRegistry().addService(new GreeterImpl());
53+
54+
GreeterGrpc.GreeterBlockingStub blockingStub =
55+
GreeterGrpc.newBlockingStub(grpcServerRule.getChannel());
7456
String testName = "test name";
7557

7658
HelloReply reply = blockingStub.sayHello(HelloRequest.newBuilder().setName(testName).build());

0 commit comments

Comments
 (0)