add GrpcExceptionHandledServerCall to catch exceptions

Signed-off-by: Thomas McKernan <tmeaglei@gmail.com>
This commit is contained in:
Thomas McKernan
2025-05-14 19:06:10 -05:00
committed by Dave Syer
parent 3a7bcdf4e8
commit bd70fcd69f
7 changed files with 85 additions and 34 deletions

View File

@@ -209,17 +209,19 @@
<goal>compile-custom</goal>
</goals>
</execution>
<execution>
<id>grpc-kotlin</id>
<goals>
<goal>compile-custom</goal>
</goals>
<configuration>
<pluginId>grpc-kotlin</pluginId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<useModulePath>false</useModulePath>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -19,7 +19,7 @@ import org.springframework.grpc.test.AutoConfigureInProcessTransport
@SpringBootTest
@AutoConfigureInProcessTransport
internal class NoAutowiredClients {
class NoAutowiredClients {
@Autowired
private lateinit var context: ApplicationContext
@@ -36,7 +36,7 @@ internal class NoAutowiredClients {
@SpringBootTest(properties = ["spring.grpc.client.default-channel.address=0.0.0.0:9090"])
@AutoConfigureInProcessTransport
internal class DefaultAutowiredClients {
class DefaultAutowiredClients {
@Autowired
private lateinit var context: ApplicationContext
@@ -59,7 +59,7 @@ internal class DefaultAutowiredClients {
properties = ["spring.grpc.client.default-channel.address=0.0.0.0:9090"]
)
@AutoConfigureInProcessTransport
internal class SpecificAutowiredClients {
class SpecificAutowiredClients {
@Autowired
private lateinit var context: ApplicationContext

View File

@@ -50,7 +50,7 @@ import java.time.Duration
properties = ["spring.grpc.server.port=0", "spring.grpc.client.channels.health-test.address=static://0.0.0.0:\${local.grpc.port}", "spring.grpc.client.channels.health-test.health.enabled=true", "spring.grpc.client.channels.health-test.health.service-name=my-service"]
)
@DirtiesContext
internal class WithClientHealthEnabled {
class WithClientHealthEnabled {
@Test
fun loadBalancerRespectsServerHealth(
@Autowired channels: GrpcChannelFactory,
@@ -112,7 +112,7 @@ internal class WithClientHealthEnabled {
)
@AutoConfigureInProcessTransport
@DirtiesContext
internal class WithActuatorHealthAdapter {
class WithActuatorHealthAdapter {
@Test
fun healthIndicatorsAdaptedToGrpcHealthStatus(
@Autowired channels: GrpcChannelFactory,
@@ -158,7 +158,7 @@ internal class WithActuatorHealthAdapter {
}
}
internal class CustomHealthIndicator : HealthIndicator {
class CustomHealthIndicator : HealthIndicator {
override fun health(): Health? {
return if (SERVICE_IS_UP) Health.up().build() else Health.down().build()
}

View File

@@ -52,7 +52,7 @@ import java.util.concurrent.atomic.AtomicInteger
@SpringBootTest
@AutoConfigureInProcessTransport
internal class ServerWithInProcessChannel {
class ServerWithInProcessChannel {
@Test
fun servesResponseToClient(@Autowired channels: GrpcChannelFactory) {
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0"))
@@ -62,7 +62,7 @@ internal class ServerWithInProcessChannel {
@SpringBootTest
@AutoConfigureInProcessTransport
internal class ServerWithException {
class ServerWithException {
@Test
fun specificErrorResponse(@Autowired channels: GrpcChannelFactory) {
@@ -81,7 +81,7 @@ internal class ServerWithException {
@Test
fun defaultErrorResponseIsUnknown(@Autowired channels: GrpcChannelFactory) {
val client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"))
Assertions.assertThat<Status.Code?>(
Assertions.assertThat(
Assert.assertThrows(
StatusRuntimeException::class.java
) { client.sayHello(HelloRequest.newBuilder().setName("internal").build()) }
@@ -94,14 +94,14 @@ internal class ServerWithException {
@SpringBootTest
@AutoConfigureInProcessTransport
internal class ServerWithExceptionInInterceptorCall {
class ServerWithExceptionInInterceptorCall {
@Test
fun specificErrorResponse(@Autowired channels: GrpcChannelFactory) {
val client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"))
Assertions.assertThat(
Assert.assertThrows(
StatusRuntimeException::class.java
) { client.sayHello(HelloRequest.newBuilder().setName("foo").build()) }
) { client.sayHello(HelloRequest.newBuilder().setName("error").build()) }
.status
.code
).isEqualTo(Status.Code.INVALID_ARGUMENT)
@@ -129,7 +129,7 @@ internal class ServerWithExceptionInInterceptorCall {
@SpringBootTest
@AutoConfigureInProcessTransport
internal class ServerWithExceptionInInterceptorListener {
class ServerWithExceptionInInterceptorListener {
@Test
fun specificErrorResponse(
@Autowired channels: GrpcChannelFactory,
@@ -150,7 +150,7 @@ internal class ServerWithExceptionInInterceptorListener {
}
@TestConfiguration
internal open class TestConfig {
open class TestConfig {
companion object {
var callCount: AtomicInteger = AtomicInteger()
var messageCount: AtomicInteger = AtomicInteger()
@@ -206,7 +206,7 @@ internal class ServerWithExceptionInInterceptorListener {
@SpringBootTest("spring.grpc.server.exception-handler.enabled=false")
@AutoConfigureInProcessTransport
internal class ServerWithUnhandledException {
class ServerWithUnhandledException {
@Test
fun specificErrorResponse(@Autowired channels: GrpcChannelFactory) {
val client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"))
@@ -236,7 +236,7 @@ internal class ServerWithUnhandledException {
@SpringBootTest(properties = ["spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0"])
internal class ServerWithAnyIPv4AddressAndRandomPort {
class ServerWithAnyIPv4AddressAndRandomPort {
@Test
fun servesResponseToClientWithAnyIPv4AddressAndRandomPort(
@Autowired channels: GrpcChannelFactory,
@@ -248,7 +248,7 @@ internal class ServerWithAnyIPv4AddressAndRandomPort {
@SpringBootTest(properties = ["spring.grpc.server.host=::", "spring.grpc.server.port=0"])
internal class ServerWithAnyIPv6AddressAndRandomPort {
class ServerWithAnyIPv6AddressAndRandomPort {
@Test
fun servesResponseToClientWithAnyIPv4AddressAndRandomPort(
@Autowired channels: GrpcChannelFactory,
@@ -260,7 +260,7 @@ internal class ServerWithAnyIPv6AddressAndRandomPort {
@SpringBootTest(properties = ["spring.grpc.server.host=127.0.0.1", "spring.grpc.server.port=0"])
internal class ServerWithLocalhostAndRandomPort {
class ServerWithLocalhostAndRandomPort {
@Test
fun servesResponseToClientWithLocalhostAndRandomPort(
@Autowired channels: GrpcChannelFactory,
@@ -275,7 +275,7 @@ internal class ServerWithLocalhostAndRandomPort {
properties = ["spring.grpc.server.port=0", "spring.grpc.client.channels.test-channel.address=static://0.0.0.0:\${local.grpc.port}"]
)
@DirtiesContext
internal class ServerConfiguredWithStaticClientChannel {
class ServerConfiguredWithStaticClientChannel {
@Test
fun servesResponseToClientWithConfiguredChannel(@Autowired channels: GrpcChannelFactory) {
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"))
@@ -285,7 +285,7 @@ internal class ServerConfiguredWithStaticClientChannel {
@SpringBootTest(properties = ["spring.grpc.server.address=unix:unix-test-channel"])
@EnabledOnOs(OS.LINUX)
internal class ServerWithUnixDomain {
class ServerWithUnixDomain {
@Test
fun clientChannelWithUnixDomain(@Autowired channels: GrpcChannelFactory) {
assertThatResponseIsServedToChannel(
@@ -304,7 +304,7 @@ internal class ServerWithUnixDomain {
)
@ActiveProfiles("ssl")
@DirtiesContext
internal class ServerWithSsl {
class ServerWithSsl {
@Test
fun clientChannelWithSsl(@Autowired channels: GrpcChannelFactory) {
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"))
@@ -317,7 +317,7 @@ internal class ServerWithSsl {
)
@ActiveProfiles("ssl")
@DirtiesContext
internal class ServerWithClientAuth {
class ServerWithClientAuth {
@Test
fun clientChannelWithSsl(@Autowired channels: GrpcChannelFactory) {
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"))