diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java index 3c36bdfb..065a8b20 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java @@ -104,7 +104,10 @@ class GrpcP1BoundaryWireTest { "app.grpc.server.port=0", "app.grpc.server.bind-address=127.0.0.1", "app.grpc.server.allow-insecure-local=true", - "app.grpc.server.reflection-enabled=false"); + "app.grpc.server.reflection-enabled=false", + // No test here asserts the drain, so closing the context does not wait the 2 s + // production default. In-flight calls still get the shutdown grace. + "app.grpc.server.shutdown-drain-seconds=0"); @Test void missingAuthenticationMetadataIsRejectedWithAStableContract() { diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java index 83b97e32..8330d4e6 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java @@ -73,7 +73,10 @@ class GrpcRequiresPermissionWireTest { "app.grpc.server.port=0", "app.grpc.server.bind-address=127.0.0.1", "app.grpc.server.allow-insecure-local=true", - "app.grpc.server.reflection-enabled=false"); + "app.grpc.server.reflection-enabled=false", + // No test here asserts the drain, so closing the context does not wait the 2 s + // production default. In-flight calls still get the shutdown grace. + "app.grpc.server.shutdown-drain-seconds=0"); @Test @DisplayName( diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java index c260ad9c..4d64f118 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java @@ -183,6 +183,8 @@ class GrpcSafeActivationTest { .withPropertyValues( "app.grpc.server.port=0", "app.grpc.server.allow-insecure-local=true", + // The only test here that starts a listener: closing it does not wait the 2 s drain. + "app.grpc.server.shutdown-drain-seconds=0", "app.grpc.platform.enabled=true") .run(context -> assertThat(context).hasNotFailed()); } diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java index 28c3d28d..31aef859 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java @@ -6,6 +6,7 @@ import io.grpc.BindableService; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.ServerServiceDefinition; +import io.grpc.StatusRuntimeException; import io.grpc.health.v1.HealthCheckRequest; import io.grpc.health.v1.HealthCheckResponse; import io.grpc.health.v1.HealthCheckResponse.ServingStatus; @@ -36,6 +37,9 @@ import org.springframework.context.annotation.Configuration; */ class GrpcServerRunnerBootTest { + // At most half the 6 s drain: this hold plus one 2 s probe leaves 2 s of slack under load. + private static final Duration HOLD = Duration.ofSeconds(2); + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withUserConfiguration(GrpcServerConfig.class) @@ -44,7 +48,10 @@ class GrpcServerRunnerBootTest { "app.grpc.server.port=0", "app.grpc.server.bind-address=127.0.0.1", "app.grpc.server.allow-insecure-local=true", - "app.grpc.server.reflection-enabled=true"); + "app.grpc.server.reflection-enabled=true", + // Closing the context does not wait the 2 s production drain. The drain test below + // sets its own value, which overrides this one. + "app.grpc.server.shutdown-drain-seconds=0"); @Test void skeletonServerStartsAndServesHealthAndReflectionWithNoFeatures() { @@ -121,10 +128,12 @@ class GrpcServerRunnerBootTest { Thread stopping = Thread.ofVirtual().start(runner::stop); try { // when - ServingStatus duringDrain = firstStatusOtherThanServing(channel); + List duringDrain = answersFromTheFirstOtherThanServing(channel); // then - assertThat(duringDrain).isEqualTo(ServingStatus.NOT_SERVING); + assertThat(duringDrain) + .as("health answers from the first one other than SERVING, for %s more", HOLD) + .containsOnly(ServingStatus.NOT_SERVING.name()); } finally { stopping.join(Duration.ofSeconds(20)); channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); @@ -133,23 +142,41 @@ class GrpcServerRunnerBootTest { } /** - * Polls health until it stops saying SERVING. A server that shut its listener the moment shutdown - * began answers this with UNAVAILABLE instead, which is what a load balancer cannot act on. + * Polls health until it stops saying SERVING, then keeps asking for {@link #HOLD} and returns + * every answer from that first one on: the serving status while the listener is up, the gRPC + * status code once it is gone. A server that shut its listener the moment shutdown began answers + * UNAVAILABLE here, which is what a load balancer cannot act on. A health that still says SERVING + * after four seconds is the whole answer: shutdown never reported itself. */ - private static ServingStatus firstStatusOtherThanServing(ManagedChannel channel) + private static List answersFromTheFirstOtherThanServing(ManagedChannel channel) throws InterruptedException { long until = System.nanoTime() + TimeUnit.SECONDS.toNanos(4); - while (System.nanoTime() < until) { - ServingStatus status = health(channel); - if (status != ServingStatus.SERVING) { - return status; - } + String first = answer(channel); + while (first.equals(ServingStatus.SERVING.name()) && System.nanoTime() < until) { Thread.sleep(10); + first = answer(channel); } - return ServingStatus.SERVING; + List answers = new ArrayList<>(List.of(first)); + if (first.equals(ServingStatus.SERVING.name())) { + return answers; + } + long holdUntil = System.nanoTime() + HOLD.toNanos(); + while (System.nanoTime() < holdUntil) { + Thread.sleep(50); + answers.add(answer(channel)); + } + return answers; } - // Two seconds per probe: the four-second window plus one probe still ends inside the 6 s drain. + private static String answer(ManagedChannel channel) { + try { + return health(channel).name(); + } catch (StatusRuntimeException e) { + return e.getStatus().getCode().name(); + } + } + + // Two seconds per probe: the 4 s search or the hold, each plus one probe, ends inside the drain. private static ServingStatus health(ManagedChannel channel) { return HealthGrpc.newBlockingStub(channel) .withDeadlineAfter(2, TimeUnit.SECONDS)