diff options
author | 2022-11-12 18:30:12 -0800 | |
---|---|---|
committer | 2022-11-12 18:30:12 -0800 | |
commit | 21bf3ddaf23c842dc12a1d76dbd3b48daf08f349 (patch) | |
tree | 06706104877984e9f083fed7c3278c9d007193cc /test/bun.js/filesink.test.ts | |
parent | 514f2a8eddf1a1d35a33cc096ed7403a79afe36f (diff) | |
download | bun-21bf3ddaf23c842dc12a1d76dbd3b48daf08f349.tar.gz bun-21bf3ddaf23c842dc12a1d76dbd3b48daf08f349.tar.zst bun-21bf3ddaf23c842dc12a1d76dbd3b48daf08f349.zip |
Redo how we poll pipes (#1496)
* Fix pipe
* Handle unregistered
* Fix failing test
Diffstat (limited to 'test/bun.js/filesink.test.ts')
-rw-r--r-- | test/bun.js/filesink.test.ts | 133 |
1 files changed, 85 insertions, 48 deletions
diff --git a/test/bun.js/filesink.test.ts b/test/bun.js/filesink.test.ts index b3b3a7e74..b4a178613 100644 --- a/test/bun.js/filesink.test.ts +++ b/test/bun.js/filesink.test.ts @@ -1,5 +1,6 @@ import { ArrayBufferSink } from "bun"; import { describe, expect, it } from "bun:test"; +import { mkfifo } from "mkfifo"; describe("FileSink", () => { const fixtures = [ @@ -66,64 +67,100 @@ describe("FileSink", () => { ], ] as const; - for (const [input, expected, label] of fixtures) { - it(`${JSON.stringify(label)}`, async () => { - const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`; - try { - require("fs").unlinkSync(path); - } catch (e) {} + function getPath(label) { + const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`; + try { + require("fs").unlinkSync(path); + } catch (e) {} + return path; + } - const sink = Bun.file(path).writer(); - for (let i = 0; i < input.length; i++) { - sink.write(input[i]); - } - await sink.end(); + var activeFIFO: Promise<string>; + var decoder = new TextDecoder(); - const output = new Uint8Array(await Bun.file(path).arrayBuffer()); - for (let i = 0; i < expected.length; i++) { - expect(output[i]).toBe(expected[i]); + function getFd(label) { + const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`; + try { + require("fs").unlinkSync(path); + } catch (e) {} + mkfifo(path, 0o666); + activeFIFO = (async function (stream: ReadableStream<Uint8Array>) { + var chunks = []; + for await (const chunk of stream) { + chunks.push(chunk); } - expect(output.byteLength).toBe(expected.byteLength); - }); + return Buffer.concat(chunks).toString(); + // test it on a small chunk size + })(Bun.file(path).stream(4)); + return path; + } - it(`flushing -> ${JSON.stringify(label)}`, async () => { - const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`; - try { - require("fs").unlinkSync(path); - } catch (e) {} + for (let isPipe of [true, false] as const) { + describe(isPipe ? "pipe" : "file", () => { + for (const [input, expected, label] of fixtures) { + var getPathOrFd = () => (isPipe ? getFd(label) : getPath(label)); - const sink = Bun.file(path).writer(); - for (let i = 0; i < input.length; i++) { - sink.write(input[i]); - await sink.flush(); - } - await sink.end(); + it(`${JSON.stringify(label)}`, async () => { + const path = getPathOrFd(); + const sink = Bun.file(path).writer(); + for (let i = 0; i < input.length; i++) { + sink.write(input[i]); + } + await sink.end(); - const output = new Uint8Array(await Bun.file(path).arrayBuffer()); - for (let i = 0; i < expected.length; i++) { - expect(output[i]).toBe(expected[i]); - } - expect(output.byteLength).toBe(expected.byteLength); - }); + if (!isPipe) { + const output = new Uint8Array(await Bun.file(path).arrayBuffer()); + for (let i = 0; i < expected.length; i++) { + expect(output[i]).toBe(expected[i]); + } + expect(output.byteLength).toBe(expected.byteLength); + } else { + const output = await activeFIFO; + expect(output).toBe(decoder.decode(expected)); + } + }); - it(`highWaterMark -> ${JSON.stringify(label)}`, async () => { - const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`; - try { - require("fs").unlinkSync(path); - } catch (e) {} + it(`flushing -> ${JSON.stringify(label)}`, async () => { + const path = getPathOrFd(); + const sink = Bun.file(path).writer(); + for (let i = 0; i < input.length; i++) { + sink.write(input[i]); + await sink.flush(); + } + await sink.end(); + if (!isPipe) { + const output = new Uint8Array(await Bun.file(path).arrayBuffer()); + for (let i = 0; i < expected.length; i++) { + expect(output[i]).toBe(expected[i]); + } + expect(output.byteLength).toBe(expected.byteLength); + } else { + const output = await activeFIFO; + expect(output).toBe(decoder.decode(expected)); + } + }); - const sink = Bun.file(path).writer({ highWaterMark: 1 }); - for (let i = 0; i < input.length; i++) { - sink.write(input[i]); - await sink.flush(); - } - await sink.end(); + it(`highWaterMark -> ${JSON.stringify(label)}`, async () => { + const path = getPathOrFd(); + const sink = Bun.file(path).writer({ highWaterMark: 1 }); + for (let i = 0; i < input.length; i++) { + sink.write(input[i]); + await sink.flush(); + } + await sink.end(); - const output = new Uint8Array(await Bun.file(path).arrayBuffer()); - for (let i = 0; i < expected.length; i++) { - expect(output[i]).toBe(expected[i]); + if (!isPipe) { + const output = new Uint8Array(await Bun.file(path).arrayBuffer()); + for (let i = 0; i < expected.length; i++) { + expect(output[i]).toBe(expected[i]); + } + expect(output.byteLength).toBe(expected.byteLength); + } else { + const output = await activeFIFO; + expect(output).toBe(decoder.decode(expected)); + } + }); } - expect(output.byteLength).toBe(expected.byteLength); }); } }); |