fix(codeartifact): stop npm data plane test from missing a fast response body (#4430)

CodeArtifactNpmDataPlaneTest flaked intermittently in CI (seen in at
least #4369 and #4422, unrelated PRs): the proxied-request test
occasionally asserted an empty body against the fake upstream's real
{"name":"lodash"} response, even though the server side had genuinely
written and sent all 17 bytes.

The request() helper blocked on req.send()'s future to obtain the
HttpClientResponse, then separately called resp.body() afterward. For
a small response from a fast local connection, that gap between the
event loop delivering the response and this JUnit thread waking back
up to attach to its body left a real window: the whole body can finish
arriving and the stream can end before anything is listening for it,
so body() sees nothing left to replay.

Reproduced locally at roughly a 12% failure rate by running the test
in a loop; confirmed via temporary instrumentation on the production
proxy path that the server side succeeds every time (pipeTo always
completed with the full byte count), which ruled out the actual proxy
code and pointed at the test's own response handling instead.

Fixed by composing request, send, and body-read into one Vert.x future
chain, so body() attaches from inside the same event-loop callback
that receives the response rather than after a blocking gap. Verified
by running the test 100 consecutive times post-fix with zero failures.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-25 17:38:51 -05:00
1 parent 3b21bb7e7e
commit 8f5ba2b2a8
1 file changed
+30 -14
@@ -4,9 +4,9 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.AuthorizationTokenScope;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
@@ -230,25 +230,41 @@ class CodeArtifactNpmDataPlaneTest {
return request(HttpMethod.PUT, path, bearerToken, body);
}
/**
* Composes request, send and body-read into one Vert.x future chain rather than blocking with
* {@code .get()} between each step: blocking to obtain the response and only then calling
* {@link HttpClientResponse#body()} leaves a window, between the response arriving on the
* event loop and this JUnit thread waking back up and attaching to it, where a small, fast
* local response (this fake upstream's whole body arrives in one write) can finish delivering
* before anything is listening for it, so {@code body()} sees an already-ended stream with
* nothing left to replay. Chaining with {@code compose} attaches the body read from inside the
* same event-loop callback that receives the response, before that window can ever open, and
* leaves exactly one blocking {@code .get()} at the very end.
*/
private HttpResponse request(HttpMethod method, String path, String bearerToken, String body) throws Exception {
RequestOptions options = new RequestOptions()
.setHost("127.0.0.1")
.setPort(dataPlane.actualPort())
.setMethod(method)
.setURI(path);
HttpClientRequest req = client.request(options).toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
if (bearerToken != null) {
req.putHeader("Authorization", "Bearer " + bearerToken);
}
HttpClientResponse resp = (body != null
? req.send(body)
: req.send())
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
String responseBody = resp.body().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS)
.toString(StandardCharsets.UTF_8);
Map<String, String> headers = new HashMap<>();
resp.headers().forEach(h -> headers.put(h.getKey().toLowerCase(), h.getValue()));
return new HttpResponse(resp.statusCode(), responseBody, headers);
Future<HttpResponse> responseFuture = client.request(options)
.compose(req -> {
if (bearerToken != null) {
req.putHeader("Authorization", "Bearer " + bearerToken);
}
return body != null ? req.send(body) : req.send();
})
.compose(resp -> resp.body().map(buffer -> {
Map<String, String> headers = new HashMap<>();
resp.headers().forEach(h -> headers.put(h.getKey().toLowerCase(), h.getValue()));
return new HttpResponse(resp.statusCode(), buffer.toString(StandardCharsets.UTF_8), headers);
}));
// 6 seconds, not the 2 used elsewhere in this file: this single wait now covers every
// stage of the chain above (request creation, send, body read) rather than one stage each
// getting its own 2-second budget the way three separate blocking calls used to, so it
// needs the combined allowance to avoid trading the body-read flake this replaced for a
// tighter timeout on a busy runner.
return responseFuture.toCompletionStage().toCompletableFuture().get(6, TimeUnit.SECONDS);
}
private record HttpResponse(int statusCode, String body, Map<String, String> headers) {}