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 <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-24 19:55:58 -04:00
1 parent 3442fe0fef
commit 615444a2aa
1 file changed
+20 -14
@@ -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<Integer> 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);