mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(elbv2): make backend HTTP connection reuse configurable (#7557)
Expose backend keep-alive and idle-pool timeout controls while retaining the existing defaults. Preserve target failures without replaying uncertain requests, and document target timeout hints and local transport limits. Validate negative idle timeouts with a Floci-specific error before creating clients, and attempt every fixture cleanup step while preserving aggregated failures. Cover negative, zero and positive configuration values and real-listener connection behavior. Closes #7524 test(elbv2): replace timer-dependent backend checks
This commit is contained in:
1 parent
2df2a1e149
commit
983fdecc79
10 files changed
+313
No files matched your search
@@ -0,0 +1,95 @@
|
||||
package io.floci.aws.services.elbv2;
|
||||
|
||||
import io.floci.aws.testutil.FreePorts;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static io.floci.aws.testing.AlbFixtures.createListener;
|
||||
import static io.floci.aws.testing.AlbFixtures.createLoadBalancer;
|
||||
import static io.floci.aws.testing.AlbFixtures.createTargetGroup;
|
||||
import static io.floci.aws.testing.AlbFixtures.deleteListener;
|
||||
import static io.floci.aws.testing.AlbFixtures.deleteLoadBalancer;
|
||||
import static io.floci.aws.testing.AlbFixtures.deleteTargetGroup;
|
||||
import static io.floci.aws.testing.AlbFixtures.registerTarget;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
abstract class ElbV2BackendConnectionTestSupport {
|
||||
|
||||
@Inject
|
||||
Vertx vertx;
|
||||
|
||||
int listenerPort;
|
||||
HttpServer backend;
|
||||
private String loadBalancerArn;
|
||||
private String targetGroupArn;
|
||||
private String listenerArn;
|
||||
|
||||
void startBackend(Handler<HttpServerRequest> handler) throws Exception {
|
||||
backend = vertx.createHttpServer().requestHandler(handler)
|
||||
.listen(0, "127.0.0.1").toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
listenerPort = FreePorts.anyInterfacePort();
|
||||
loadBalancerArn = createLoadBalancer("backend-connection-lb");
|
||||
targetGroupArn = createTargetGroup("backend-connection-tg", "ip", backend.actualPort());
|
||||
registerTarget(targetGroupArn, "127.0.0.1", backend.actualPort());
|
||||
listenerArn = createListener(loadBalancerArn, targetGroupArn, listenerPort);
|
||||
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
|
||||
try (Socket socket = new Socket("127.0.0.1", listenerPort)) {
|
||||
// Probe only the listener socket, without sending a request to the target.
|
||||
socket.setSoTimeout(1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsBadGatewayWhenTargetClosesOnItsFirstRequest() throws Exception {
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
startBackend(request -> {
|
||||
requests.incrementAndGet();
|
||||
request.connection().close();
|
||||
});
|
||||
|
||||
assertEquals(502, given().baseUri("http://127.0.0.1").port(listenerPort).get("/close").statusCode());
|
||||
assertEquals(1, requests.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void neverReplaysPostAfterTheTargetConsumesItsBodyAndCloses() throws Exception {
|
||||
List<String> consumed = new CopyOnWriteArrayList<>();
|
||||
startBackend(request -> request.bodyHandler(body -> {
|
||||
consumed.add(body.toString());
|
||||
request.connection().close();
|
||||
}));
|
||||
|
||||
assertEquals(502, given().baseUri("http://127.0.0.1").port(listenerPort)
|
||||
.body("charge-once").post("/charge").statusCode());
|
||||
assertEquals(List.of("charge-once"), consumed);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopBackend() {
|
||||
assertAll("Clean up every backend connection fixture resource after any failure",
|
||||
() -> deleteListener(listenerArn),
|
||||
() -> deleteLoadBalancer(loadBalancerArn),
|
||||
() -> deleteTargetGroup(targetGroupArn),
|
||||
() -> {
|
||||
if (backend != null) {
|
||||
backend.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.floci.aws.services.elbv2;
|
||||
|
||||
import io.floci.aws.testing.RealElbV2DataPlaneProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(RealElbV2DataPlaneProfile.class)
|
||||
class ElbV2BackendKeepAliveIntegrationTest extends ElbV2BackendConnectionTestSupport {
|
||||
|
||||
@Test
|
||||
void reusesBackendConnectionsByDefaultAndRetainsTargetCloseErrors() throws Exception {
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
startBackend(request -> {
|
||||
if (requests.incrementAndGet() == 3) {
|
||||
request.connection().close();
|
||||
} else {
|
||||
request.response().putHeader("X-Backend-Connection",
|
||||
Integer.toString(request.remoteAddress().port())).end("ok");
|
||||
}
|
||||
});
|
||||
|
||||
Response first = given().baseUri("http://127.0.0.1").port(listenerPort).get("/first");
|
||||
assertEquals(200, first.statusCode());
|
||||
Response second = given().baseUri("http://127.0.0.1").port(listenerPort).get("/second");
|
||||
assertEquals(200, second.statusCode());
|
||||
assertEquals(first.header("X-Backend-Connection"), second.header("X-Backend-Connection"));
|
||||
assertEquals(502, given().baseUri("http://127.0.0.1").port(listenerPort).get("/third").statusCode());
|
||||
assertEquals(3, requests.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package io.floci.aws.services.elbv2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpConnection;
|
||||
import io.vertx.core.http.WebSocket;
|
||||
import io.vertx.core.http.WebSocketConnectOptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static io.floci.aws.testing.AlbFixtures.exchange;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(ElbV2BackendNoKeepAliveIntegrationTest.NoKeepAliveProfile.class)
|
||||
class ElbV2BackendNoKeepAliveIntegrationTest extends ElbV2BackendConnectionTestSupport {
|
||||
|
||||
public static class NoKeepAliveProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.elbv2.mock", "false",
|
||||
"floci.services.elbv2.backend-keep-alive", "false");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void disablingKeepAliveUsesFreshConnectionsForSequentialRequests() throws Exception {
|
||||
Map<HttpConnection, AtomicInteger> requests = new ConcurrentHashMap<>();
|
||||
startBackend(request -> {
|
||||
int count = requests.computeIfAbsent(request.connection(), ignored -> new AtomicInteger())
|
||||
.incrementAndGet();
|
||||
if (count > 1) {
|
||||
request.connection().close();
|
||||
} else {
|
||||
request.response().putHeader("X-Backend-Connection",
|
||||
Integer.toString(request.remoteAddress().port())).end("ok");
|
||||
}
|
||||
});
|
||||
|
||||
Response first = given().baseUri("http://127.0.0.1").port(listenerPort).get("/first");
|
||||
assertEquals(200, first.statusCode());
|
||||
Response second = given().baseUri("http://127.0.0.1").port(listenerPort).get("/second");
|
||||
assertEquals(200, second.statusCode());
|
||||
assertEquals("ok", second.asString());
|
||||
assertNotEquals(first.header("X-Backend-Connection"), second.header("X-Backend-Connection"));
|
||||
assertEquals(2, requests.size());
|
||||
assertEquals(2, requests.values().stream().mapToInt(AtomicInteger::get).sum());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disablingKeepAlivePreservesWebSocketUpgrades() throws Exception {
|
||||
startBackend(request -> request.toWebSocket().onSuccess(socket ->
|
||||
socket.textMessageHandler(socket::writeTextMessage)));
|
||||
|
||||
HttpClient client = vertx.createHttpClient();
|
||||
try {
|
||||
WebSocket socket = client.webSocket(new WebSocketConnectOptions()
|
||||
.setHost("127.0.0.1").setPort(listenerPort).setURI("/socket"))
|
||||
.toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
assertEquals("first", exchange(socket, "first"));
|
||||
assertEquals("second", exchange(socket, "second"));
|
||||
socket.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
client.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.floci.aws.services.elbv2;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpClientOptions;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ElbV2DataPlaneConfigTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, Integer.MIN_VALUE})
|
||||
void negativeBackendKeepAliveTimeoutNamesTheInvalidSetting(int seconds) {
|
||||
ElbV2DataPlane plane = plane(seconds);
|
||||
plane.vertx = mock(Vertx.class);
|
||||
|
||||
IllegalArgumentException error = assertThrows(IllegalArgumentException.class, plane::init);
|
||||
|
||||
assertEquals("floci.services.elbv2.backend-keep-alive-timeout-seconds must be non-negative",
|
||||
error.getMessage());
|
||||
verifyNoInteractions(plane.vertx);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 1, 60})
|
||||
void backendClientUsesConfiguredKeepAliveTimeout(int seconds) {
|
||||
ElbV2DataPlane plane = plane(seconds);
|
||||
plane.vertx = mock(Vertx.class);
|
||||
|
||||
plane.init();
|
||||
|
||||
ArgumentCaptor<HttpClientOptions> options = ArgumentCaptor.forClass(HttpClientOptions.class);
|
||||
verify(plane.vertx).createHttpClient(options.capture());
|
||||
assertEquals(seconds, options.getValue().getKeepAliveTimeout());
|
||||
}
|
||||
|
||||
private static ElbV2DataPlane plane(int seconds) {
|
||||
ElbV2DataPlane plane = new ElbV2DataPlane();
|
||||
plane.config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(plane.config.services().elbv2().backendKeepAliveTimeoutSeconds()).thenReturn(seconds);
|
||||
return plane;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user