testing: move out inner class tests in GrpcServerRuleTest

Resolves #2808
This commit is contained in:
ZHANG Dapeng 2017-06-06 12:30:58 -07:00 committed by GitHub
parent c48610b890
commit 9057bc723c
1 changed files with 139 additions and 156 deletions

View File

@ -34,60 +34,58 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.model.Statement;
/** Unit tests for {@link GrpcServerRule}. */
@RunWith(JUnit4.class)
public class GrpcServerRuleTest {
@RunWith(JUnit4.class)
public static class WithoutDirectExecutor {
@Rule
public final GrpcServerRule grpcServerRule = new GrpcServerRule();
@Rule public final GrpcServerRule grpcServerRule1 = new GrpcServerRule();
@Rule public final GrpcServerRule grpcServerRule2 = new GrpcServerRule().directExecutor();
@Test
public void serverAndChannelAreStarted() {
assertThat(grpcServerRule.getServer().isShutdown()).isFalse();
assertThat(grpcServerRule.getServer().isTerminated()).isFalse();
public void serverAndChannelAreStarted_withoutDirectExecutor() {
assertThat(grpcServerRule1.getServer().isShutdown()).isFalse();
assertThat(grpcServerRule1.getServer().isTerminated()).isFalse();
assertThat(grpcServerRule.getChannel().isShutdown()).isFalse();
assertThat(grpcServerRule.getChannel().isTerminated()).isFalse();
assertThat(grpcServerRule1.getChannel().isShutdown()).isFalse();
assertThat(grpcServerRule1.getChannel().isTerminated()).isFalse();
assertThat(grpcServerRule.getServerName()).isNotNull();
assertThat(grpcServerRule.getServiceRegistry()).isNotNull();
assertThat(grpcServerRule1.getServerName()).isNotNull();
assertThat(grpcServerRule1.getServiceRegistry()).isNotNull();
}
@Test
public void serverAllowsServicesToBeAddedViaServiceRegistry() {
public void serverAllowsServicesToBeAddedViaServiceRegistry_withoutDirectExecutor() {
TestServiceImpl testService = new TestServiceImpl();
grpcServerRule.getServiceRegistry().addService(testService);
grpcServerRule1.getServiceRegistry().addService(testService);
TestServiceGrpc.TestServiceBlockingStub stub =
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
TestServiceGrpc.newBlockingStub(grpcServerRule1.getChannel());
Messages.SimpleRequest request1 = Messages.SimpleRequest.newBuilder()
.setPayload(Messages.Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.setPayload(Messages.Payload.newBuilder().setBody(
ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.build();
Messages.SimpleRequest request2 = Messages.SimpleRequest.newBuilder()
.setPayload(Messages.Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.setPayload(Messages.Payload.newBuilder().setBody(
ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.build();
stub.unaryCall(request1);
stub.unaryCall(request2);
assertThat(testService.unaryCallRequests)
.containsExactly(request1, request2);
assertThat(testService.unaryCallRequests).containsExactly(request1, request2);
}
@Test
public void serviceIsNotRunOnSameThreadAsTest() {
public void serviceIsNotRunOnSameThreadAsTest_withoutDirectExecutor() {
TestServiceImpl testService = new TestServiceImpl();
grpcServerRule.getServiceRegistry().addService(testService);
grpcServerRule1.getServiceRegistry().addService(testService);
TestServiceGrpc.TestServiceBlockingStub stub =
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
TestServiceGrpc.newBlockingStub(grpcServerRule1.getChannel());
// Make a garbage request first due to https://github.com/grpc/grpc-java/issues/2444.
stub.emptyCall(EmptyProtos.Empty.newBuilder().build());
@ -97,63 +95,55 @@ public class GrpcServerRuleTest {
}
@Test(expected = IllegalStateException.class)
public void callDirectExecutorNotAtRuleInstantiation() {
grpcServerRule.directExecutor();
}
}
@RunWith(JUnit4.class)
public static class WithDirectExecutor {
@Rule
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
@Test
public void serverAndChannelAreStarted() {
assertThat(grpcServerRule.getServer().isShutdown()).isFalse();
assertThat(grpcServerRule.getServer().isTerminated()).isFalse();
assertThat(grpcServerRule.getChannel().isShutdown()).isFalse();
assertThat(grpcServerRule.getChannel().isTerminated()).isFalse();
assertThat(grpcServerRule.getServerName()).isNotNull();
assertThat(grpcServerRule.getServiceRegistry()).isNotNull();
public void callDirectExecutorNotAtRuleInstantiation_withoutDirectExecutor() {
grpcServerRule1.directExecutor();
}
@Test
public void serverAllowsServicesToBeAddedViaServiceRegistry() {
public void serverAndChannelAreStarted_withDirectExecutor() {
assertThat(grpcServerRule2.getServer().isShutdown()).isFalse();
assertThat(grpcServerRule2.getServer().isTerminated()).isFalse();
assertThat(grpcServerRule2.getChannel().isShutdown()).isFalse();
assertThat(grpcServerRule2.getChannel().isTerminated()).isFalse();
assertThat(grpcServerRule2.getServerName()).isNotNull();
assertThat(grpcServerRule2.getServiceRegistry()).isNotNull();
}
@Test
public void serverAllowsServicesToBeAddedViaServiceRegistry_withDirectExecutor() {
TestServiceImpl testService = new TestServiceImpl();
grpcServerRule.getServiceRegistry().addService(testService);
grpcServerRule2.getServiceRegistry().addService(testService);
TestServiceGrpc.TestServiceBlockingStub stub =
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
TestServiceGrpc.newBlockingStub(grpcServerRule2.getChannel());
Messages.SimpleRequest request1 = Messages.SimpleRequest.newBuilder()
.setPayload(Messages.Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.setPayload(Messages.Payload.newBuilder().setBody(
ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.build();
Messages.SimpleRequest request2 = Messages.SimpleRequest.newBuilder()
.setPayload(Messages.Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.setPayload(Messages.Payload.newBuilder().setBody(
ByteString.copyFromUtf8(UUID.randomUUID().toString())))
.build();
stub.unaryCall(request1);
stub.unaryCall(request2);
assertThat(testService.unaryCallRequests)
.containsExactly(request1, request2);
assertThat(testService.unaryCallRequests).containsExactly(request1, request2);
}
@Test
public void serviceIsRunOnSameThreadAsTest() {
public void serviceIsRunOnSameThreadAsTest_withDirectExecutor() {
TestServiceImpl testService = new TestServiceImpl();
grpcServerRule.getServiceRegistry().addService(testService);
grpcServerRule2.getServiceRegistry().addService(testService);
TestServiceGrpc.TestServiceBlockingStub stub =
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
TestServiceGrpc.newBlockingStub(grpcServerRule2.getChannel());
// Make a garbage request first due to https://github.com/grpc/grpc-java/issues/2444.
stub.emptyCall(EmptyProtos.Empty.newBuilder().build());
@ -161,10 +151,6 @@ public class GrpcServerRuleTest {
assertThat(testService.lastEmptyCallRequestThread).isEqualTo(Thread.currentThread());
}
}
@RunWith(JUnit4.class)
public static class ResourceCleanup {
@Test
public void serverAndChannelAreShutdownAfterRule() throws Throwable {
@ -210,7 +196,6 @@ public class GrpcServerRuleTest {
server = grpcServerRule.getServer();
}
}
}
private static class TestServiceImpl extends TestServiceGrpc.TestServiceImplBase {
@ -221,8 +206,7 @@ public class GrpcServerRuleTest {
@Override
public void emptyCall(
EmptyProtos.Empty request,
StreamObserver<EmptyProtos.Empty> responseObserver) {
EmptyProtos.Empty request, StreamObserver<EmptyProtos.Empty> responseObserver) {
lastEmptyCallRequestThread = Thread.currentThread();
@ -233,8 +217,7 @@ public class GrpcServerRuleTest {
@Override
public void unaryCall(
Messages.SimpleRequest request,
StreamObserver<Messages.SimpleResponse> responseObserver) {
Messages.SimpleRequest request, StreamObserver<Messages.SimpleResponse> responseObserver) {
unaryCallRequests.add(request);