test: add failing test for #13696

It seems that the http module does not stream the initial few chunks of
a streaming http response in real-time. This has caused issues when
using bun with testcontainers.

While I am not qualified to locate and resolve the root cause of this
issue, I can provide a breaking test, in the hopes it will help somebody
else locate and fix the issue.
This commit is contained in:
Felix Zedén Yverås
2024-09-03 19:48:19 +02:00
committed by Ciro Spaciari
parent 528ad62d1e
commit bc503ce8ec

View File

@@ -133,6 +133,51 @@ describe("node:http", () => {
}
});
it("response body streaming is immediate (#13696)", async () => {
const totalChunks = 10;
const spacing = 50;
const acceptableDelay = 20;
try {
var server = createServer(async (req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
for (let i = 0; i < totalChunks; i++) {
res.write(new Date().getTime().toString() + "\n");
if (i + 1 < totalChunks) await Bun.sleep(spacing);
}
res.end();
});
const url = await listen(server);
const res = await fetch(url);
const reader = res.body.getReader();
const decoder = new TextDecoder();
let receivedChunks = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
receivedChunks++;
// Verify that chunks are not held up longer than necessary
// at the receiver. This is likely to be in the single digits.
//
// #13696: Bun would delay the initial chunks and then send multiple
// chunks before real-time streaming started working.
expect(
new Date().getTime() - parseInt(decoder.decode(value).trimEnd())
).toBeLessThan(acceptableDelay);
}
// Verify that the correct number of chunks were sent (in case server
// decides to send no chunks at all).
expect(receivedChunks).toEqual(totalChunks);
} finally {
server.close();
}
});
it("listen should return server", async () => {
const server = createServer();
const listenResponse = server.listen(0);