test(lambda): await runtime recovery and bound client cleanup (#7380)

Replace incidental dispatch waits with observable completion and preserve failure-path cleanup of each shutdown-race client and server. Retain existing race assertions and coverage.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-03 07:57:07 -04:00
1 parent 4933126314
commit 548f972d01
1 file changed
+55 -46
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.lambda.runtime;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.lambda.model.PendingInvocation;
import io.vertx.core.Context;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
@@ -36,6 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -806,13 +808,17 @@ class RuntimeApiServerTest {
// interleaving by ending the ctx from afterEnqueueDispatchLockReleased.
AtomicInteger sendInvocationCalls =
new AtomicInteger();
CompletableFuture<Context> dispatchObserved = new CompletableFuture<>();
installGatedServerWithDispatchObservation(
waitingCtx -> {
if (!waitingCtx.response().ended()) {
waitingCtx.response().setStatusCode(500).end();
}
},
requestId -> sendInvocationCalls.incrementAndGet());
requestId -> {
sendInvocationCalls.incrementAndGet();
dispatchObserved.complete(Vertx.currentContext());
});
HttpRequest parkedRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))
@@ -826,8 +832,12 @@ class RuntimeApiServerTest {
new CompletableFuture<>());
server.enqueue(invocation);
// Wait for the runOnContext-scheduled dispatch to have fired.
Thread.sleep(500);
Context dispatchContext = dispatchObserved.get(5, TimeUnit.SECONDS);
await().atMost(Duration.ofSeconds(5)).pollInterval(Duration.ofMillis(10))
.until(() -> server.pendingQueueSize() == 1 && server.inFlightSize() == 0);
CompletableFuture<Void> dispatchReturned = new CompletableFuture<>();
dispatchContext.runOnContext(ignored -> dispatchReturned.complete(null));
dispatchReturned.get(5, TimeUnit.SECONDS);
assertEquals(1, sendInvocationCalls.get(),
"sendInvocation must run exactly once — a zero count means the "
@@ -870,8 +880,8 @@ class RuntimeApiServerTest {
new CompletableFuture<>());
server.enqueue(invocation);
// Wait for the runOnContext-scheduled dispatch (and its failed write) to fire.
Thread.sleep(500);
await().atMost(Duration.ofSeconds(5)).pollInterval(Duration.ofMillis(10))
.until(() -> server.pendingQueueSize() == 1 && server.inFlightSize() == 0);
assertEquals(1, server.pendingQueueSize(),
"invocation must be requeued after write failure — zero means stranded.");
@@ -2040,54 +2050,53 @@ class RuntimeApiServerTest {
// a shared client's connection pool could otherwise hand back a stale pooled
// connection to that iteration's now-closed server.
HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();
int freshPort = findFreePort();
RuntimeApiServer freshServer = new RuntimeApiServer(vertx, freshPort);
freshServer.start().get(5, TimeUnit.SECONDS);
try {
String extensionId = registerExtensionOn(client, freshPort, "lambda-adapter", "SHUTDOWN");
try (AutoCloseable clientCleanup = () -> {
client.shutdownNow();
assertTrue(client.awaitTermination(Duration.ofSeconds(5)), "HTTP client did not terminate");
}) {
int freshPort = findFreePort();
RuntimeApiServer freshServer = new RuntimeApiServer(vertx, freshPort);
try (AutoCloseable serverCleanup = () -> freshServer.stop().get(5, TimeUnit.SECONDS)) {
freshServer.start().get(5, TimeUnit.SECONDS);
String extensionId = registerExtensionOn(client, freshPort, "lambda-adapter", "SHUTDOWN");
CompletableFuture<HttpResponse<String>> asyncNext = client.sendAsync(
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + freshPort + "/2020-01-01/extension/event/next"))
.header("Lambda-Extension-Identifier", extensionId)
.GET().build(),
HttpResponse.BodyHandlers.ofString());
// Wait for the poll to actually park before racing stop() against it. A fixed
// sleep only *assumes* it parked: on a loaded machine the sleep expires first,
// stop() closes the listening socket while the request is still in flight, and
// the client sees the connection die (EOFException) rather than the SHUTDOWN
// this test is about. Same reasoning as
// extensionRegister_racedAgainstStop_eventuallyDeliversShutdown.
assertTrue(awaitExtensionParked(freshServer, extensionId, 5000),
"extension never parked on /event/next; the stop() race was never set up");
freshServer.stop();
HttpResponse<String> response = asyncNext.get(5, TimeUnit.SECONDS);
boolean deliveredHere = response.statusCode() == 200
&& "SHUTDOWN".equals(new JsonObject(response.body()).getString("eventType"));
if (deliveredHere) {
totalDelivered.incrementAndGet();
} else {
// Didn't land in this specific request — a subsequent poll with the same
// identifier must still find the SHUTDOWN queued (never orphaned).
HttpResponse<String> retry = client.send(HttpRequest.newBuilder()
.uri(URI.create(
"http://localhost:" + freshPort + "/2020-01-01/extension/event/next"))
CompletableFuture<HttpResponse<String>> asyncNext = client.sendAsync(
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + freshPort + "/2020-01-01/extension/event/next"))
.header("Lambda-Extension-Identifier", extensionId)
.GET().build(),
HttpResponse.BodyHandlers.ofString());
if (retry.statusCode() == 200
&& "SHUTDOWN".equals(new JsonObject(retry.body()).getString("eventType"))) {
// Wait for the poll to actually park before racing stop() against it. A fixed
// sleep only *assumes* it parked: on a loaded machine the sleep expires first,
// stop() closes the listening socket while the request is still in flight, and
// the client sees the connection die (EOFException) rather than the SHUTDOWN
// this test is about. Same reasoning as
// extensionRegister_racedAgainstStop_eventuallyDeliversShutdown.
assertTrue(awaitExtensionParked(freshServer, extensionId, 5000),
"extension never parked on /event/next; the stop() race was never set up");
freshServer.stop();
HttpResponse<String> response = asyncNext.get(5, TimeUnit.SECONDS);
boolean deliveredHere = response.statusCode() == 200
&& "SHUTDOWN".equals(new JsonObject(response.body()).getString("eventType"));
if (deliveredHere) {
totalDelivered.incrementAndGet();
} else {
// Didn't land in this specific request — a subsequent poll with the same
// identifier must still find the SHUTDOWN queued (never orphaned).
HttpResponse<String> retry = client.send(HttpRequest.newBuilder()
.uri(URI.create(
"http://localhost:" + freshPort + "/2020-01-01/extension/event/next"))
.header("Lambda-Extension-Identifier", extensionId)
.GET().build(),
HttpResponse.BodyHandlers.ofString());
if (retry.statusCode() == 200
&& "SHUTDOWN".equals(new JsonObject(retry.body()).getString("eventType"))) {
totalDelivered.incrementAndGet();
}
}
}
} finally {
freshServer.stop().get(5, TimeUnit.SECONDS);
// Each HttpClient owns a selector thread and connection pool; leaking one per
// iteration across hundreds of iterations starves the JVM and makes even an
// unrelated server's bind time out.
client.close();
}
}