diff options
author | 2022-10-10 20:57:38 -0700 | |
---|---|---|
committer | 2022-10-10 20:57:38 -0700 | |
commit | 3cc61f8e30f494c10de08de8afcf7bef3dd67fec (patch) | |
tree | 11066938a4014f211aa286e93e9d5cfa66b8b567 /test/bun.js/streams.test.js | |
parent | ef2c9c330c1cf6a79a27d9ce06af1bdc7b5b18b6 (diff) | |
download | bun-3cc61f8e30f494c10de08de8afcf7bef3dd67fec.tar.gz bun-3cc61f8e30f494c10de08de8afcf7bef3dd67fec.tar.zst bun-3cc61f8e30f494c10de08de8afcf7bef3dd67fec.zip |
Fix issue with exit callback in Bun.spawn() never firing
Diffstat (limited to '')
-rw-r--r-- | test/bun.js/streams.test.js | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/test/bun.js/streams.test.js b/test/bun.js/streams.test.js index 1268a04c0..20aa5c270 100644 --- a/test/bun.js/streams.test.js +++ b/test/bun.js/streams.test.js @@ -1,11 +1,72 @@ -import { file, readableStreamToArrayBuffer, readableStreamToArray } from "bun"; +import { + file, + readableStreamToArrayBuffer, + readableStreamToArray, + readableStreamToText, +} from "bun"; import { expect, it, beforeEach, afterEach } from "bun:test"; -import { writeFileSync } from "node:fs"; +import { mkfifo } from "mkfifo"; +import { unlinkSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; import { gc } from "./gc"; new Uint8Array(); beforeEach(() => gc()); afterEach(() => gc()); +it("Bun.file() read text from pipe", async () => { + try { + unlinkSync("/tmp/fifo"); + } catch (e) {} + + mkfifo("/tmp/fifo", 0o666); + + const large = "HELLO!".repeat((((1024 * 512) / "HELLO!".length) | 0) + 1); + + const chunks = []; + var out = Bun.file("/tmp/fifo").stream(); + const proc = Bun.spawn({ + cmd: [ + "bash", + join(import.meta.dir + "/", "bun-streams-test-fifo.sh"), + "/tmp/fifo", + ], + stderr: "inherit", + env: { + FIFO_TEST: large, + }, + }); + const exited = proc.exited; + proc.ref(); + var prom = new Promise((resolve, reject) => { + setTimeout(() => { + (async function () { + var reader = out.getReader(); + while (true) { + const chunk = await reader.read(); + if (chunk.done) { + if (chunks.length == 0) { + out = Bun.file("/tmp/fifo").stream(); + reader = out.getReader(); + continue; + } + const output = new TextDecoder() + .decode(new Uint8Array(Buffer.concat(chunks))) + .trim(); + resolve(output); + break; + } + chunks.push(chunk.value); + } + })(); + }); + }); + + const [status, output] = await Promise.all([exited, prom]); + + expect(output.length).toBe(large.length); + expect(output).toBe(large); + expect(status).toBe(0); +}); it("exists globally", () => { expect(typeof ReadableStream).toBe("function"); @@ -191,7 +252,6 @@ it("ReadableStream for Blob", async () => { console.error(e); console.error(e.stack); } - if (chunk.done) break; chunks.push(new TextDecoder().decode(chunk.value)); } |