From 0f5016bd7c0eff44bb2255f9389ad8faf2bb9163 Mon Sep 17 00:00:00 2001 From: cirospaciari Date: Sun, 14 Apr 2024 23:40:38 -0300 Subject: [PATCH] echo test --- test/js/bun/http/body-leak-test-fixture.ts | 6 ++++ test/js/bun/http/serve-body-leak.test.ts | 35 +++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/test/js/bun/http/body-leak-test-fixture.ts b/test/js/bun/http/body-leak-test-fixture.ts index c3f51f4a6c2ad5..542882d69b976e 100644 --- a/test/js/bun/http/body-leak-test-fixture.ts +++ b/test/js/bun/http/body-leak-test-fixture.ts @@ -25,6 +25,12 @@ const server = Bun.serve({ // we will receive ABORT_ERR here so we just catch it and ignores it reader?.read().catch(() => {}); } + } else if (req.url.endsWith("/streaming-echo")) { + return new Response(req.body, { + headers: { + "Content-Type": "application/octet-stream", + }, + }); } return new Response("Ok"); }, diff --git a/test/js/bun/http/serve-body-leak.test.ts b/test/js/bun/http/serve-body-leak.test.ts index 17e2a764f1a954..0a4cc538fc69dd 100644 --- a/test/js/bun/http/serve-body-leak.test.ts +++ b/test/js/bun/http/serve-body-leak.test.ts @@ -25,11 +25,7 @@ afterAll(() => { }); async function getMemoryUsage(): Promise { - return await fetch(`${url.origin}/report`).then(res => res.json()); -} -async function garbageCollect() { - await Bun.sleep(100); - await fetch(`${url.origin}/gc`); + return (await fetch(`${url.origin}/report`).then(res => res.json())) as number; } async function warmup() { @@ -48,22 +44,31 @@ async function callBuffering() { }); } async function callStreaming() { - await fetch(`${url.origin}/streaming`, { + const result = await fetch(`${url.origin}/streaming`, { method: "POST", body: payload, - }); + }).then(res => res.text()); + expect(result).toBe("Ok"); } async function callIncompleteStreaming() { - await fetch(`${url.origin}/incomplete-streaming`, { + const result = await fetch(`${url.origin}/incomplete-streaming`, { method: "POST", body: payload, - }); + }).then(res => res.text()); + expect(result).toBe("Ok"); +} +async function callStreamingEcho() { + const result = await fetch(`${url.origin}/streaming-echo`, { + method: "POST", + body: payload, + }).then(res => res.text()); + expect(result).toBe(payload); } async function callIgnore() { await fetch(url, { method: "POST", body: payload, - }); + }).then(res => res.text()); } async function calculateMemoryLeak(fn: () => Promise) { @@ -130,3 +135,13 @@ it("should not leak memory when streaming the body incompletely", async () => { // acceptable memory leak expect(report.leak).toBeLessThanOrEqual(ACCEPTABLE_MEMORY_LEAK); }); + +it("should not leak memory when streaming the body and echoing it back", async () => { + const report = await calculateMemoryLeak(callStreamingEcho); + console.log(report); + + // peak memory is too high + expect(report.peak_memory > report.start_memory * 2).toBe(false); + // acceptable memory leak + expect(report.leak).toBeLessThanOrEqual(ACCEPTABLE_MEMORY_LEAK); +});