Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions test/js/bun/shell/shell-blocking-pipe.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { $, generateHeapSnapshot } from "bun";
import { $ } from "bun";

import { test } from "bun:test";
import { isWindows } from "harness";
import { expect, test } from "bun:test";
import { bunEnv, bunExe, isWindows } from "harness";
import { join } from "node:path";

// We skip this test on Windows becasue:
// We skip these tests on Windows because:
// 1. Windows didn't have this problem to begin with
// 2. We need system cat.
test.skipIf(isWindows)("writing > send buffer size doesn't block the main thread", async () => {
const expected = Buffer.alloc(1024 * 1024, "bun!").toString();
const massiveComamnd = "echo " + expected + " | " + Bun.which("cat");
const pendingResult = $`${{
const result = await $`${{
raw: massiveComamnd,
}}`.text();

// Ensure that heap snapshot works, to excercise the memoryCost & estimated fields.
generateHeapSnapshot("v8");

const result = await pendingResult;

if (result !== expected + "\n") {
throw new Error("Expected " + expected + "\n but got " + result);
}
Expand All @@ -31,3 +27,25 @@ test.skipIf(isWindows)("writing > send buffer size (with a variable) doesn't blo
throw new Error("Expected " + expected + "\n but got " + result);
}
});

// The snapshots are taken in a separate process; the fixture explains why.
test.skipIf(isWindows)("heap snapshots report the script held by a pending shell command", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), join(import.meta.dir, "shell-heap-snapshot-fixture.ts")],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
const { scriptLength, parsedScriptBeforeRun, interpreterWhileRunning, interpreterAfterExit, stdoutMatches } =
JSON.parse(stdout);
expect(parsedScriptBeforeRun).toBeGreaterThanOrEqual(scriptLength);
expect(interpreterWhileRunning).toBeGreaterThanOrEqual(scriptLength);
expect(interpreterAfterExit).toBeLessThan(scriptLength);
// The second snapshot ran a full GC underneath the running command; its
// output must still come through intact.
expect(stdoutMatches).toBe(true);
expect(exitCode).toBe(0);
});
56 changes: 56 additions & 0 deletions test/js/bun/shell/shell-heap-snapshot-fixture.ts

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