Add sample and integration test

This commit is contained in:
Dave Syer
2024-11-21 13:36:36 +00:00
parent f97487c768
commit c239cfe970
7 changed files with 261 additions and 15 deletions

View File

@@ -2,6 +2,12 @@ package org.springframework.grpc.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.grpc.server.GlobalServerInterceptor;
import org.springframework.grpc.server.exception.GrpcExceptionHandlerInterceptor;
import io.grpc.ServerInterceptor;
import io.grpc.Status;
@SpringBootApplication
public class GrpcServerApplication {
@@ -10,4 +16,15 @@ public class GrpcServerApplication {
SpringApplication.run(GrpcServerApplication.class, args);
}
@GlobalServerInterceptor
@Bean
public ServerInterceptor globalInterceptor() {
return new GrpcExceptionHandlerInterceptor(exception -> {
if (exception instanceof IllegalArgumentException) {
return Status.INVALID_ARGUMENT.withDescription(exception.getMessage());
}
return null;
});
}
}

View File

@@ -18,6 +18,12 @@ public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
log.info("Hello " + req.getName());
if (req.getName().startsWith("error")) {
throw new IllegalArgumentException("Bad name: " + req.getName());
}
if (req.getName().startsWith("internal")) {
throw new RuntimeException();
}
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();

View File

@@ -17,6 +17,7 @@
package org.springframework.grpc.sample;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@@ -35,6 +36,8 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import io.grpc.ManagedChannel;
import io.grpc.StatusRuntimeException;
import io.grpc.Status.Code;
/**
* More detailed integration tests for {@link GrpcServerFactory gRPC server factories} and
@@ -53,6 +56,32 @@ class GrpcServerIntegrationTests {
}
@Nested
@SpringBootTest
class ServerWithException {
@Test
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:0").build());
assertThat(assertThrows(StatusRuntimeException.class,
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
.getStatus()
.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
}
@Test
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:0").build());
assertThat(assertThrows(StatusRuntimeException.class,
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
.getStatus()
.getCode()).isEqualTo(Code.UNKNOWN);
}
}
@Nested
@SpringBootTest(properties = { "spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0",
"spring.grpc.inprocess.enabled=false" })