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,181 +34,166 @@ 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
|
@Test
|
||||||
public final GrpcServerRule grpcServerRule = new GrpcServerRule();
|
public void serverAndChannelAreStarted_withoutDirectExecutor() {
|
||||||
|
assertThat(grpcServerRule1.getServer().isShutdown()).isFalse();
|
||||||
|
assertThat(grpcServerRule1.getServer().isTerminated()).isFalse();
|
||||||
|
|
||||||
@Test
|
assertThat(grpcServerRule1.getChannel().isShutdown()).isFalse();
|
||||||
public void serverAndChannelAreStarted() {
|
assertThat(grpcServerRule1.getChannel().isTerminated()).isFalse();
|
||||||
assertThat(grpcServerRule.getServer().isShutdown()).isFalse();
|
|
||||||
assertThat(grpcServerRule.getServer().isTerminated()).isFalse();
|
|
||||||
|
|
||||||
assertThat(grpcServerRule.getChannel().isShutdown()).isFalse();
|
assertThat(grpcServerRule1.getServerName()).isNotNull();
|
||||||
assertThat(grpcServerRule.getChannel().isTerminated()).isFalse();
|
assertThat(grpcServerRule1.getServiceRegistry()).isNotNull();
|
||||||
|
|
||||||
assertThat(grpcServerRule.getServerName()).isNotNull();
|
|
||||||
assertThat(grpcServerRule.getServiceRegistry()).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void serverAllowsServicesToBeAddedViaServiceRegistry() {
|
|
||||||
TestServiceImpl testService = new TestServiceImpl();
|
|
||||||
|
|
||||||
grpcServerRule.getServiceRegistry().addService(testService);
|
|
||||||
|
|
||||||
TestServiceGrpc.TestServiceBlockingStub stub =
|
|
||||||
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
|
|
||||||
|
|
||||||
Messages.SimpleRequest request1 = Messages.SimpleRequest.newBuilder()
|
|
||||||
.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())))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
stub.unaryCall(request1);
|
|
||||||
stub.unaryCall(request2);
|
|
||||||
|
|
||||||
assertThat(testService.unaryCallRequests)
|
|
||||||
.containsExactly(request1, request2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void serviceIsNotRunOnSameThreadAsTest() {
|
|
||||||
TestServiceImpl testService = new TestServiceImpl();
|
|
||||||
|
|
||||||
grpcServerRule.getServiceRegistry().addService(testService);
|
|
||||||
|
|
||||||
TestServiceGrpc.TestServiceBlockingStub stub =
|
|
||||||
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
|
|
||||||
assertThat(testService.lastEmptyCallRequestThread).isNotEqualTo(Thread.currentThread());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
|
||||||
public void callDirectExecutorNotAtRuleInstantiation() {
|
|
||||||
grpcServerRule.directExecutor();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RunWith(JUnit4.class)
|
@Test
|
||||||
public static class WithDirectExecutor {
|
public void serverAllowsServicesToBeAddedViaServiceRegistry_withoutDirectExecutor() {
|
||||||
|
TestServiceImpl testService = new TestServiceImpl();
|
||||||
|
|
||||||
@Rule
|
grpcServerRule1.getServiceRegistry().addService(testService);
|
||||||
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
|
|
||||||
|
|
||||||
@Test
|
TestServiceGrpc.TestServiceBlockingStub stub =
|
||||||
public void serverAndChannelAreStarted() {
|
TestServiceGrpc.newBlockingStub(grpcServerRule1.getChannel());
|
||||||
assertThat(grpcServerRule.getServer().isShutdown()).isFalse();
|
|
||||||
assertThat(grpcServerRule.getServer().isTerminated()).isFalse();
|
|
||||||
|
|
||||||
assertThat(grpcServerRule.getChannel().isShutdown()).isFalse();
|
Messages.SimpleRequest request1 = Messages.SimpleRequest.newBuilder()
|
||||||
assertThat(grpcServerRule.getChannel().isTerminated()).isFalse();
|
.setPayload(Messages.Payload.newBuilder().setBody(
|
||||||
|
ByteString.copyFromUtf8(UUID.randomUUID().toString())))
|
||||||
|
.build();
|
||||||
|
|
||||||
assertThat(grpcServerRule.getServerName()).isNotNull();
|
Messages.SimpleRequest request2 = Messages.SimpleRequest.newBuilder()
|
||||||
assertThat(grpcServerRule.getServiceRegistry()).isNotNull();
|
.setPayload(Messages.Payload.newBuilder().setBody(
|
||||||
}
|
ByteString.copyFromUtf8(UUID.randomUUID().toString())))
|
||||||
|
.build();
|
||||||
|
|
||||||
@Test
|
stub.unaryCall(request1);
|
||||||
public void serverAllowsServicesToBeAddedViaServiceRegistry() {
|
stub.unaryCall(request2);
|
||||||
TestServiceImpl testService = new TestServiceImpl();
|
|
||||||
|
|
||||||
grpcServerRule.getServiceRegistry().addService(testService);
|
assertThat(testService.unaryCallRequests).containsExactly(request1, request2);
|
||||||
|
|
||||||
TestServiceGrpc.TestServiceBlockingStub stub =
|
|
||||||
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
|
|
||||||
|
|
||||||
Messages.SimpleRequest request1 = Messages.SimpleRequest.newBuilder()
|
|
||||||
.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())))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
stub.unaryCall(request1);
|
|
||||||
stub.unaryCall(request2);
|
|
||||||
|
|
||||||
assertThat(testService.unaryCallRequests)
|
|
||||||
.containsExactly(request1, request2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void serviceIsRunOnSameThreadAsTest() {
|
|
||||||
TestServiceImpl testService = new TestServiceImpl();
|
|
||||||
|
|
||||||
grpcServerRule.getServiceRegistry().addService(testService);
|
|
||||||
|
|
||||||
TestServiceGrpc.TestServiceBlockingStub stub =
|
|
||||||
TestServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
|
|
||||||
assertThat(testService.lastEmptyCallRequestThread).isEqualTo(Thread.currentThread());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RunWith(JUnit4.class)
|
@Test
|
||||||
public static class ResourceCleanup {
|
public void serviceIsNotRunOnSameThreadAsTest_withoutDirectExecutor() {
|
||||||
|
TestServiceImpl testService = new TestServiceImpl();
|
||||||
|
|
||||||
@Test
|
grpcServerRule1.getServiceRegistry().addService(testService);
|
||||||
public void serverAndChannelAreShutdownAfterRule() throws Throwable {
|
|
||||||
GrpcServerRule grpcServerRule = new GrpcServerRule();
|
|
||||||
|
|
||||||
// Before the rule has been executed, all of its resources should be null.
|
TestServiceGrpc.TestServiceBlockingStub stub =
|
||||||
assertThat(grpcServerRule.getChannel()).isNull();
|
TestServiceGrpc.newBlockingStub(grpcServerRule1.getChannel());
|
||||||
assertThat(grpcServerRule.getServer()).isNull();
|
|
||||||
assertThat(grpcServerRule.getServerName()).isNull();
|
|
||||||
assertThat(grpcServerRule.getServiceRegistry()).isNull();
|
|
||||||
|
|
||||||
// The TestStatement stores the channel and server instances so that we can inspect them after
|
// Make a garbage request first due to https://github.com/grpc/grpc-java/issues/2444.
|
||||||
// the rule cleans up.
|
stub.emptyCall(EmptyProtos.Empty.newBuilder().build());
|
||||||
TestStatement statement = new TestStatement(grpcServerRule);
|
stub.emptyCall(EmptyProtos.Empty.newBuilder().build());
|
||||||
|
|
||||||
grpcServerRule.apply(statement, null).evaluate();
|
assertThat(testService.lastEmptyCallRequestThread).isNotEqualTo(Thread.currentThread());
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure that the stored channel and server instances were shut down.
|
@Test(expected = IllegalStateException.class)
|
||||||
assertThat(statement.channel.isShutdown()).isTrue();
|
public void callDirectExecutorNotAtRuleInstantiation_withoutDirectExecutor() {
|
||||||
assertThat(statement.server.isShutdown()).isTrue();
|
grpcServerRule1.directExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
// All references to the resources that we created should be set to null.
|
@Test
|
||||||
assertThat(grpcServerRule.getChannel()).isNull();
|
public void serverAndChannelAreStarted_withDirectExecutor() {
|
||||||
assertThat(grpcServerRule.getServer()).isNull();
|
assertThat(grpcServerRule2.getServer().isShutdown()).isFalse();
|
||||||
assertThat(grpcServerRule.getServerName()).isNull();
|
assertThat(grpcServerRule2.getServer().isTerminated()).isFalse();
|
||||||
assertThat(grpcServerRule.getServiceRegistry()).isNull();
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
grpcServerRule2.getServiceRegistry().addService(testService);
|
||||||
|
|
||||||
|
TestServiceGrpc.TestServiceBlockingStub stub =
|
||||||
|
TestServiceGrpc.newBlockingStub(grpcServerRule2.getChannel());
|
||||||
|
|
||||||
|
Messages.SimpleRequest request1 = Messages.SimpleRequest.newBuilder()
|
||||||
|
.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())))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
stub.unaryCall(request1);
|
||||||
|
stub.unaryCall(request2);
|
||||||
|
|
||||||
|
assertThat(testService.unaryCallRequests).containsExactly(request1, request2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void serviceIsRunOnSameThreadAsTest_withDirectExecutor() {
|
||||||
|
TestServiceImpl testService = new TestServiceImpl();
|
||||||
|
|
||||||
|
grpcServerRule2.getServiceRegistry().addService(testService);
|
||||||
|
|
||||||
|
TestServiceGrpc.TestServiceBlockingStub stub =
|
||||||
|
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());
|
||||||
|
stub.emptyCall(EmptyProtos.Empty.newBuilder().build());
|
||||||
|
|
||||||
|
assertThat(testService.lastEmptyCallRequestThread).isEqualTo(Thread.currentThread());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void serverAndChannelAreShutdownAfterRule() throws Throwable {
|
||||||
|
GrpcServerRule grpcServerRule = new GrpcServerRule();
|
||||||
|
|
||||||
|
// Before the rule has been executed, all of its resources should be null.
|
||||||
|
assertThat(grpcServerRule.getChannel()).isNull();
|
||||||
|
assertThat(grpcServerRule.getServer()).isNull();
|
||||||
|
assertThat(grpcServerRule.getServerName()).isNull();
|
||||||
|
assertThat(grpcServerRule.getServiceRegistry()).isNull();
|
||||||
|
|
||||||
|
// The TestStatement stores the channel and server instances so that we can inspect them after
|
||||||
|
// the rule cleans up.
|
||||||
|
TestStatement statement = new TestStatement(grpcServerRule);
|
||||||
|
|
||||||
|
grpcServerRule.apply(statement, null).evaluate();
|
||||||
|
|
||||||
|
// Ensure that the stored channel and server instances were shut down.
|
||||||
|
assertThat(statement.channel.isShutdown()).isTrue();
|
||||||
|
assertThat(statement.server.isShutdown()).isTrue();
|
||||||
|
|
||||||
|
// All references to the resources that we created should be set to null.
|
||||||
|
assertThat(grpcServerRule.getChannel()).isNull();
|
||||||
|
assertThat(grpcServerRule.getServer()).isNull();
|
||||||
|
assertThat(grpcServerRule.getServerName()).isNull();
|
||||||
|
assertThat(grpcServerRule.getServiceRegistry()).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TestStatement extends Statement {
|
||||||
|
|
||||||
|
private final GrpcServerRule grpcServerRule;
|
||||||
|
|
||||||
|
private ManagedChannel channel;
|
||||||
|
private Server server;
|
||||||
|
|
||||||
|
private TestStatement(GrpcServerRule grpcServerRule) {
|
||||||
|
this.grpcServerRule = grpcServerRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TestStatement extends Statement {
|
@Override
|
||||||
|
public void evaluate() throws Throwable {
|
||||||
private final GrpcServerRule grpcServerRule;
|
channel = grpcServerRule.getChannel();
|
||||||
|
server = grpcServerRule.getServer();
|
||||||
private ManagedChannel channel;
|
|
||||||
private Server server;
|
|
||||||
|
|
||||||
private TestStatement(GrpcServerRule grpcServerRule) {
|
|
||||||
this.grpcServerRule = grpcServerRule;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void evaluate() throws Throwable {
|
|
||||||
channel = grpcServerRule.getChannel();
|
|
||||||
server = grpcServerRule.getServer();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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