diff options
author | 2022-11-25 00:08:36 -0800 | |
---|---|---|
committer | 2022-11-25 00:08:36 -0800 | |
commit | d1a4f4fd6981a06920adb632dde2562b76ddc4d0 (patch) | |
tree | 6a26cd3ebd8afdbad653a800d54967c4e84b55c2 /test/bun.js | |
parent | 0b915fed034c38ae9a2e15caee94530910dc864b (diff) | |
download | bun-d1a4f4fd6981a06920adb632dde2562b76ddc4d0.tar.gz bun-d1a4f4fd6981a06920adb632dde2562b76ddc4d0.tar.zst bun-d1a4f4fd6981a06920adb632dde2562b76ddc4d0.zip |
Introduce `FileSink.ref()` and `FileSink.unref()`
Diffstat (limited to 'test/bun.js')
-rw-r--r-- | test/bun.js/spawn-streaming-stdin.test.ts | 32 | ||||
-rw-r--r-- | test/bun.js/stdin-repro.js | 10 | ||||
-rw-r--r-- | test/bun.js/streams.test.js | 12 |
3 files changed, 35 insertions, 19 deletions
diff --git a/test/bun.js/spawn-streaming-stdin.test.ts b/test/bun.js/spawn-streaming-stdin.test.ts index 953548071..6424eadfc 100644 --- a/test/bun.js/spawn-streaming-stdin.test.ts +++ b/test/bun.js/spawn-streaming-stdin.test.ts @@ -12,30 +12,40 @@ test("spawn can write to stdin multiple chunks", async () => { cmd: [bunExe(), import.meta.dir + "/stdin-repro.js"], stdout: "pipe", stdin: "pipe", - stderr: "inherit", + stderr: Bun.file("/tmp/out.log"), env: { BUN_DEBUG_QUIET_LOGS: 1, }, }); + // (async function () { + // for await (var chunk of proc.stderr) { + // console.error("[stderr]", new TextDecoder().decode(chunk)); + // } + // })(); exited = proc.exited; var counter = 0; var inCounter = 0; const prom2 = (async function () { - while (inCounter++ < 4) { + while (true) { await new Promise((resolve, reject) => setTimeout(resolve, 8)); proc.stdin.write("Wrote to stdin!"); - await proc.stdin.flush(); + inCounter++; + + if (inCounter === 4) break; } - await proc.stdin.end(); + await new Promise((resolve) => + Promise.resolve(proc.stdin.end()).then(resolve), + ); })(); + var chunks = []; const prom = (async function () { try { for await (var chunk of proc.stdout) { - expect(new TextDecoder().decode(chunk)).toBe("Wrote to stdin!\n"); + chunks.push(chunk); counter++; - if (counter > 3) break; + if (counter === 4) break; } } catch (e) { console.log(e.stack); @@ -43,11 +53,15 @@ test("spawn can write to stdin multiple chunks", async () => { } })(); await Promise.all([prom, prom2]); + const code = await exited; + console.log(code); expect(counter).toBe(4); + expect(Buffer.concat(chunks).toString().trim()).toBe( + "Wrote to stdin!\n".repeat(4).trim(), + ); // proc.kill(); + + gcTick(true); })(); - await exited; } - - gcTick(true); }); diff --git a/test/bun.js/stdin-repro.js b/test/bun.js/stdin-repro.js index 05daf0637..5f945be7c 100644 --- a/test/bun.js/stdin-repro.js +++ b/test/bun.js/stdin-repro.js @@ -1,5 +1,7 @@ -while (true) { - for await (let chunk of Bun.stdin.stream()) { - console.log(new Buffer(chunk).toString()); - } +var count = 5; +for await (let chunk of Bun.stdin.stream()) { + const str = new Buffer(chunk).toString(); + console.error("how many?", count, chunk.byteLength); + count -= str.split("\n").length; + console.log(str); } diff --git a/test/bun.js/streams.test.js b/test/bun.js/streams.test.js index a872b7701..406c80852 100644 --- a/test/bun.js/streams.test.js +++ b/test/bun.js/streams.test.js @@ -225,7 +225,7 @@ it("Bun.file() read text from pipe", async () => { const large = "HELLO!".repeat((((1024 * 65) / "HELLO!".length) | 0) + 1); const chunks = []; - var out = Bun.file("/tmp/fifo").stream(); + const proc = Bun.spawn({ cmd: [ "bash", @@ -244,17 +244,17 @@ it("Bun.file() read text from pipe", async () => { const prom = (async function () { while (chunks.length === 0) { + var out = Bun.file("/tmp/fifo").stream(); for await (const chunk of out) { chunks.push(chunk); } - console.log("done"); } + return Buffer.concat(chunks).toString(); })(); const [status, output] = await Promise.all([exited, prom]); - console.log("here"); - expect(output.length).toBe(large.length); - expect(output).toBe(large); + expect(output.length).toBe(large.length + 1); + expect(output).toBe(large + "\n"); expect(status).toBe(0); }); @@ -452,7 +452,7 @@ it("ReadableStream for Blob", async () => { it("ReadableStream for File", async () => { var blob = file(import.meta.dir + "/fetch.js.txt"); - var stream = blob.stream(24); + var stream = blob.stream(); const chunks = []; var reader = stream.getReader(); stream = undefined; |