From 615444a2aaffa1a0c364b023e5afb599321cf111 Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:55:58 -0600 Subject: [PATCH] test(apigatewayv2): await WebSocket close callbacks (#9391) Replace seven fixed close sleeps with the existing listener close future, bounded completion and final socket abort. Preserve all integration cases and payload assertions. Co-authored-by: Andre Kurait Signed-off-by: Andre Kurait Signed-off-by: Jackson Price --- .../WebSocketAwsHttpIntegrationTest.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketAwsHttpIntegrationTest.java b/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketAwsHttpIntegrationTest.java index 3c703ac7f6..486b591ba7 100644 --- a/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketAwsHttpIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/apigatewayv2/WebSocketAwsHttpIntegrationTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.TestMethodOrder; import java.net.URI; import java.net.http.HttpClient; import java.net.http.WebSocket; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import static io.restassured.RestAssured.given; @@ -200,8 +201,7 @@ class WebSocketAwsHttpIntegrationTest { JsonNode responseNode = MAPPER.readTree(response); assertTrue(responseNode.has("result"), "Response should have 'result' field from response template"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } @Test @@ -215,8 +215,7 @@ class WebSocketAwsHttpIntegrationTest { String response = capture.getNextMessage(15, TimeUnit.SECONDS); assertNotNull(response, "Should receive response"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } // ──────────────────────────── HTTP_PROXY Integration Type Tests ──────────────────────────── @@ -299,8 +298,7 @@ class WebSocketAwsHttpIntegrationTest { assertTrue(responseNode.has("transformed") || responseNode.has("input"), "Response should contain Lambda output forwarded via HTTP_PROXY"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } @Test @@ -314,8 +312,7 @@ class WebSocketAwsHttpIntegrationTest { String response = capture.getNextMessage(15, TimeUnit.SECONDS); assertNotNull(response, "Should receive response without template transformation"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } // ──────────────────────────── HTTP Integration Type Tests ──────────────────────────── @@ -398,8 +395,7 @@ class WebSocketAwsHttpIntegrationTest { assertTrue(responseNode.has("httpResult"), "Response should have 'httpResult' field from response template"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } @Test @@ -413,8 +409,7 @@ class WebSocketAwsHttpIntegrationTest { String response = capture.getNextMessage(15, TimeUnit.SECONDS); assertNotNull(response, "Should receive response from HTTP endpoint"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } // ──────────────────────────── Stage Variable Substitution in HTTP URI ──────────────────────────── @@ -497,8 +492,7 @@ class WebSocketAwsHttpIntegrationTest { assertTrue(responseNode.has("transformed") || responseNode.has("input"), "Response should come from Lambda invoked via stage-variable-resolved HTTP URI"); - ws.sendClose(WebSocket.NORMAL_CLOSURE, "done").join(); - Thread.sleep(500); + closeWebSocket(ws, capture); } // ──────────────────────────── Cleanup ──────────────────────────── @@ -525,6 +519,18 @@ class WebSocketAwsHttpIntegrationTest { // ──────────────────────────── Helpers ──────────────────────────── + private static void closeWebSocket(WebSocket socket, + WebSocketTestSupport.MultiMessageCapture capture) throws Exception { + CompletableFuture closed = new CompletableFuture<>(); + capture.setCloseFuture(closed); + try { + socket.sendClose(WebSocket.NORMAL_CLOSURE, "done").get(5, TimeUnit.SECONDS); + assertEquals(WebSocket.NORMAL_CLOSURE, closed.get(5, TimeUnit.SECONDS)); + } finally { + socket.abort(); + } + } + private WebSocket connectWebSocket(String apiId, String stageName, WebSocketTestSupport.MultiMessageCapture capture) throws Exception { String wsUrl = WebSocketTestSupport.buildWsUrl(baseUri, apiId, stageName);