test(grpc): drain을 단언하지 않는 시험이 종료 drain 2초를 기다리지 않는다 #63

Merged
donghyeon.kang merged 2 commits from test/grpc-no-drain-wait into main 2026-11-20 11:25:35 +00:00
4 changed files with 50 additions and 15 deletions
@@ -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() {
@@ -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(
@@ -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());
}
@@ -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<String> 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<String> 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<String> 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)