Files
bun.sh/test/js/bun/glob/leak.test.ts
2024-09-03 21:32:52 -07:00

66 lines
1.9 KiB
TypeScript

import { describe, expect, test } from "bun:test";
describe("leaks", () => {
const bun = process.argv[0];
const cwd = import.meta.dir;
const iters = 100;
const hundredMb = (1 << 20) * 100;
test("scanSync", () => {
const code = /* ts */ `
let prev: number | undefined = undefined;
for (let i = 0; i < ${iters}; i++) {
Bun.gc(true);
(function () {
const glob = new Bun.Glob("**/*");
Array.from(glob.scanSync({ cwd: '${escapeCwd(cwd)}' }));
})();
Bun.gc(true);
const val = process.memoryUsage.rss();
if (prev === undefined) {
prev = val;
} else {
if (Math.abs(prev - val) >= ${hundredMb}) {
throw new Error('uh oh: ' + Math.abs(prev - val))
}
}
}
`;
const { stdout, stderr, exitCode } = Bun.spawnSync([bun, "--smol", "-e", code]);
console.log(stdout.toString(), stderr.toString());
expect(exitCode).toBe(0);
});
test("scan", async () => {
const code = /* ts */ `
let prev: number | undefined = undefined;
for (let i = 0; i < ${iters}; i++) {
Bun.gc(true);
await (async function () {
const glob = new Bun.Glob("**/*");
await Array.fromAsync(glob.scan({ cwd: '${escapeCwd(cwd)}' }));
})();
Bun.gc(true);
const val = process.memoryUsage.rss();
if (prev === undefined) {
prev = val;
} else {
if (Math.abs(prev - val) >= ${hundredMb}) {
throw new Error('uh oh: ' + Math.abs(prev - val))
}
}
}
`;
const { stdout, stderr, exitCode } = Bun.spawnSync([bun, "--smol", "-e", code]);
console.log(stdout.toString(), stderr.toString());
expect(exitCode).toBe(0);
});
});
function escapeCwd(cwd: string): string {
if (process.platform == "win32") return cwd.replaceAll("\\", "\\\\");
return cwd;
}