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