Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed May 13, 2024
1 parent acc5ce8 commit ea09533
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
8 changes: 8 additions & 0 deletions test/js/web/fetch/fetch-leak-test-fixture-3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions test/js/web/fetch/fetch-leak-test-fixture-4.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions test/js/web/fetch/fetch-leak-test-fixture-5.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 63 additions & 2 deletions test/js/web/fetch/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { mkfifo } from "mkfifo";
import { tmpdir } from "os";
import { gzipSync } from "zlib";
import { join } from "path";
import { gc, withoutAggressiveGC, gcTick, isWindows } from "harness";
import { gc, withoutAggressiveGC, gcTick, isWindows, bunExe, bunEnv } from "harness";
import net from "net";

const tmp_dir = mkdtempSync(join(realpathSync(tmpdir()), "fetch.test"));

const fixture = readFileSync(join(import.meta.dir, "fetch.js.txt"), "utf8").replaceAll("\r\n", "\n");

const fetchFixture3 = join(import.meta.dir, "fetch-leak-test-fixture-3.js");
const fetchFixture4 = join(import.meta.dir, "fetch-leak-test-fixture-4.js");
const fetchFixture5 = join(import.meta.dir, "fetch-leak-test-fixture-5.js");
let server: Server;
function startServer({ fetch, ...options }: ServeOptions) {
server = serve({
Expand Down Expand Up @@ -1962,3 +1964,62 @@ describe("http/1.1 response body length", () => {
expect(response.arrayBuffer()).resolves.toHaveLength(0);
});
});
describe("fetch Response life cycle", () => {
it("should not keep Response alive if not consumed", async () => {
const serverProcess = Bun.spawn({
cmd: [bunExe(), fetchFixture3],
stderr: "inherit",
stdout: "pipe",
stdin: "ignore",
env: bunEnv,
});

async function getServerUrl() {
const reader = serverProcess.stdout.getReader();
const { done, value } = await reader.read();
return new TextDecoder().decode(value);
}
const serverUrl = await getServerUrl();
const clientProcess = Bun.spawn({
cmd: [bunExe(), fetchFixture4, serverUrl],
stderr: "inherit",
stdout: "pipe",
stdin: "ignore",
env: bunEnv,
});
try {
expect(await clientProcess.exited).toBe(0);
} finally {
serverProcess.kill();
}
});

it("should keep Response promise alive if consumed", async () => {
const serverProcess = Bun.spawn({
cmd: [bunExe(), fetchFixture3],
stderr: "inherit",
stdout: "pipe",
stdin: "ignore",
env: bunEnv,
});

async function getServerUrl() {
const reader = serverProcess.stdout.getReader();
const { done, value } = await reader.read();
return new TextDecoder().decode(value);
}
const serverUrl = await getServerUrl();
const clientProcess = Bun.spawn({
cmd: [bunExe(), fetchFixture5, serverUrl],
stderr: "inherit",
stdout: "pipe",
stdin: "ignore",
env: bunEnv,
});
try {
expect(await clientProcess.exited).toBe(0);
} finally {
serverProcess.kill();
}
});
});

0 comments on commit ea09533

Please sign in to comment.