Skip to content

Commit

Permalink
Improve Http1xTest#testHttpServerWithIdleTimeoutSendChunkedFile that …
Browse files Browse the repository at this point in the history
…needs to probe the actual time to send a chunked file to pass reliably independantly of the test env.
  • Loading branch information
vietj committed Sep 22, 2024
1 parent f9ab03b commit 4ab5f09
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/test/java/io/vertx/core/http/Http1xTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4879,21 +4879,36 @@ public void start(Promise<Void> startFuture) {
public void testHttpServerWithIdleTimeoutSendChunkedFile() throws Exception {
// Does not pass reliably in CI (timeout)
Assume.assumeTrue(!vertx.isNativeTransportEnabled() && !Utils.isWindows());
int expected = 64 * 1024 * 1024; // We estimate this will take more than 200ms to transfer with a 1ms pause in chunks
File sent = TestUtils.tmpFile(".dat", expected);
server.close();
int expected = 32 * 1024 * 1024;
File file = TestUtils.tmpFile(".dat", expected);
// Estimate the delay to transfer a file with a 1ms pause in chunks
int delay = retrieveFileFromServer(file, createBaseServerOptions());
// Now test with timeout relative to this delay
int timeout = delay / 2;
delay = retrieveFileFromServer(file, createBaseServerOptions().setIdleTimeout(timeout).setIdleTimeoutUnit(TimeUnit.MILLISECONDS));
assertTrue(delay > timeout);
}

private int retrieveFileFromServer(File file, HttpServerOptions options) throws Exception {
server.close().toCompletionStage().toCompletableFuture().get(20, TimeUnit.SECONDS);
server = vertx
.createHttpServer(createBaseServerOptions().setIdleTimeout(1000).setIdleTimeoutUnit(TimeUnit.MILLISECONDS))
.createHttpServer(options)
.requestHandler(
req -> {
req.response().sendFile(sent.getAbsolutePath());
req.response().sendFile(file.getAbsolutePath());
});
startServer(testAddress);
client.request(requestOptions)
.onComplete(onSuccess(req -> {
req.send(onSuccess(resp -> {
long now = System.currentTimeMillis();
int[] length = {0};
long now = System.currentTimeMillis();
Integer len = getFile().toCompletionStage().toCompletableFuture().get(20, TimeUnit.SECONDS);
assertEquals((int)len, file.length());
return (int) (System.currentTimeMillis() - now);
}

private Future<Integer> getFile() {
int[] length = {0};
return client.request(requestOptions)
.compose(req -> req.send()
.compose(resp -> {
resp.handler(buff -> {
length[0] += buff.length();
resp.pause();
Expand All @@ -4902,14 +4917,9 @@ public void testHttpServerWithIdleTimeoutSendChunkedFile() throws Exception {
});
});
resp.exceptionHandler(this::fail);
resp.endHandler(v -> {
assertEquals(expected, length[0]);
assertTrue(System.currentTimeMillis() - now > 1000);
testComplete();
});
}));
}));
await();
return resp.end();
}))
.map(v -> length[0]);
}

@Test
Expand Down

0 comments on commit 4ab5f09

Please sign in to comment.