diff options
Diffstat (limited to 'test/bun.js')
-rw-r--r-- | test/bun.js/bash-echo.sh | 1 | ||||
-rw-r--r-- | test/bun.js/child_process-node.test.js | 32 | ||||
-rw-r--r-- | test/bun.js/child_process.test.ts | 62 | ||||
-rw-r--r-- | test/bun.js/fetch-gzip.test.ts | 167 | ||||
-rw-r--r-- | test/bun.js/filesink.test.ts | 4 | ||||
-rw-r--r-- | test/bun.js/hash.test.js | 11 | ||||
-rw-r--r-- | test/bun.js/node-test-helpers.js | 10 | ||||
-rw-r--r-- | test/bun.js/node-timers.test.ts | 10 | ||||
-rw-r--r-- | test/bun.js/spawn-streaming-stdout.test.ts | 50 | ||||
-rw-r--r-- | test/bun.js/spawn.test.ts | 274 | ||||
-rw-r--r-- | test/bun.js/streams.test.js | 36 |
11 files changed, 410 insertions, 247 deletions
diff --git a/test/bun.js/bash-echo.sh b/test/bun.js/bash-echo.sh index 7e6e64cb4..57bca4b01 100644 --- a/test/bun.js/bash-echo.sh +++ b/test/bun.js/bash-echo.sh @@ -1,4 +1,3 @@ #!/usr/bin/env bash - myvar=$(cat /dev/stdin) echo -e "$myvar" diff --git a/test/bun.js/child_process-node.test.js b/test/bun.js/child_process-node.test.js index 908dc2a36..699b0e58c 100644 --- a/test/bun.js/child_process-node.test.js +++ b/test/bun.js/child_process-node.test.js @@ -1,4 +1,4 @@ -import { beforeAll, describe, it } from "bun:test"; +import { beforeAll, describe, it as it_ } from "bun:test"; import { ChildProcess, spawn, exec } from "node:child_process"; import { strictEqual, @@ -9,6 +9,36 @@ import { createDoneDotAll, } from "node-test-helpers"; import { tmpdir } from "node:os"; +import { gcTick } from "gc"; + +const it = (label, fn) => { + const hasDone = fn.length === 1; + if (fn.constructor.name === "AsyncFunction" && hasDone) { + return it_(label, async (done) => { + gcTick(); + await fn(done); + gcTick(); + }); + } else if (hasDone) { + return it_(label, (done) => { + gcTick(); + fn(done); + gcTick(); + }); + } else if (fn.constructor.name === "AsyncFunction") { + return it_(label, async () => { + gcTick(); + await fn(); + gcTick(); + }); + } else { + return it_(label, () => { + gcTick(); + fn(); + gcTick(); + }); + } +}; const debug = process.env.DEBUG ? console.log : () => {}; diff --git a/test/bun.js/child_process.test.ts b/test/bun.js/child_process.test.ts index 95a589401..83b0954fc 100644 --- a/test/bun.js/child_process.test.ts +++ b/test/bun.js/child_process.test.ts @@ -1,4 +1,5 @@ -import { describe, it, expect } from "bun:test"; +import { describe, it as it_, expect as expect_ } from "bun:test"; +import { gcTick } from "gc"; import { ChildProcess, spawn, @@ -11,6 +12,42 @@ import { } from "node:child_process"; import { tmpdir } from "node:os"; +const expect: typeof expect_ = (actual: unknown) => { + gcTick(); + const ret = expect_(actual); + gcTick(); + return ret; +}; + +const it: typeof it_ = (label, fn) => { + const hasDone = fn.length === 1; + if (fn.constructor.name === "AsyncFunction" && hasDone) { + return it_(label, async (done) => { + gcTick(); + await fn(done); + gcTick(); + }); + } else if (hasDone) { + return it_(label, (done) => { + gcTick(); + fn(done); + gcTick(); + }); + } else if (fn.constructor.name === "AsyncFunction") { + return it_(label, async () => { + gcTick(); + await fn(); + gcTick(); + }); + } else { + return it_(label, () => { + gcTick(); + fn(); + gcTick(); + }); + } +}; + const debug = process.env.DEBUG ? console.log : () => {}; const platformTmpDir = require("fs").realpathSync(tmpdir()); @@ -166,17 +203,24 @@ describe("spawn()", () => { }); it("should allow explicit setting of argv0", async () => { + var resolve; + const promise = new Promise((resolve1) => { + resolve = resolve1; + }); + process.env.NO_COLOR = "1"; const child = spawn("node", ["--help"], { argv0: "bun" }); - const result: string = await new Promise((resolve) => { - let msg; - child.stdout.on("data", (data) => { - msg += data.toString(); - }); + delete process.env.NO_COLOR; + let msg = ""; - child.stdout.on("close", () => { - resolve(msg); - }); + child.stdout.on("data", (data) => { + msg += data.toString(); }); + + child.stdout.on("close", () => { + resolve(msg); + }); + + const result = await promise; expect(/Open bun's Discord server/.test(result)).toBe(true); }); diff --git a/test/bun.js/fetch-gzip.test.ts b/test/bun.js/fetch-gzip.test.ts index f5c2ed946..fdde76ae8 100644 --- a/test/bun.js/fetch-gzip.test.ts +++ b/test/bun.js/fetch-gzip.test.ts @@ -1,15 +1,16 @@ import { concatArrayBuffers } from "bun"; import { it, describe, expect } from "bun:test"; import fs from "fs"; -import { gc } from "./gc"; +import { gc, gcTick } from "./gc"; it("fetch() with a buffered gzip response works (one chunk)", async () => { var server = Bun.serve({ port: 6025, async fetch(req) { + gcTick(true); return new Response( - await Bun.file(import.meta.dir + "/fixture.html.gz").arrayBuffer(), + require("fs").readFileSync(import.meta.dir + "/fixture.html.gz"), { headers: { "Content-Encoding": "gzip", @@ -19,20 +20,25 @@ it("fetch() with a buffered gzip response works (one chunk)", async () => { ); }, }); + gcTick(true); const res = await fetch( `http://${server.hostname}:${server.port}`, {}, { verbose: true }, ); + gcTick(true); const arrayBuffer = await res.arrayBuffer(); - expect( - new Buffer(arrayBuffer).equals( - new Buffer( - await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), - ), - ), - ).toBe(true); + const clone = new Buffer(arrayBuffer); + gcTick(true); + await (async function () { + const second = new Buffer( + await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), + ); + gcTick(true); + expect(second.equals(clone)).toBe(true); + })(); + gcTick(true); server.stop(); }); @@ -116,27 +122,32 @@ it("fetch() with a gzip response works (one chunk)", async () => { var server = Bun.serve({ port: 6023, - fetch(req) { - return new Response(Bun.file(import.meta.dir + "/fixture.html.gz"), { - headers: { - "Content-Encoding": "gzip", - "Content-Type": "text/html; charset=utf-8", - }, - }); - }, - }); +// fetch(req) { +// return new Response(Bun.file(import.meta.dir + "/fixture.html.gz"), { +// headers: { +// "Content-Encoding": "gzip", +// "Content-Type": "text/html; charset=utf-8", +// }, +// }); +// }, +// }); +// gcTick(); +// const res = await fetch(`http://${server.hostname}:${server.port}`); +// const arrayBuffer = await res.arrayBuffer(); +// expect( +// new Buffer(arrayBuffer).equals( +// new Buffer( +// await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), +// ), +// ), +// ).toBe(true); +// gcTick(); +// server.stop(); +// }); - const res = await fetch(`http://${server.hostname}:${server.port}`); - const arrayBuffer = await res.arrayBuffer(); - expect( - new Buffer(arrayBuffer).equals( - new Buffer( - await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), - ), - ), - ).toBe(true); - server.stop(); -}); +// it("fetch() with a gzip response works (multiple chunks)", async () => { +// var server = Bun.serve({ +// port: 6024, it("fetch() with a gzip response works (one chunk, streamed, with a delay)", async () => { var server = Bun.serve({ @@ -183,55 +194,61 @@ it("fetch() with a gzip response works (multiple chunks)", async () => { var server = Bun.serve({ port: 6024, - fetch(req) { - return new Response( - new ReadableStream({ - type: "direct", - async pull(controller) { - var chunks: ArrayBuffer[] = []; - const buffer = await Bun.file( - import.meta.dir + "/fixture.html.gz", - ).arrayBuffer(); - var remaining = buffer; - for (var i = 100; i < buffer.byteLength; i += 100) { - var chunk = remaining.slice(0, i); - remaining = remaining.slice(i); - controller.write(chunk); - chunks.push(chunk); - await controller.flush(); - } +// await controller.flush(); +// // sanity check +// expect( +// new Buffer(concatArrayBuffers(chunks)).equals(new Buffer(buffer)), +// ).toBe(true); +// gcTick(); +// controller.end(); +// }, +// }), +// { +// headers: { +// "Content-Encoding": "gzip", +// "Content-Type": "text/html; charset=utf-8", +// "Content-Length": "1", +// }, +// }, +// ); +// }, +// }); - await controller.flush(); - // sanity check - expect( - new Buffer(concatArrayBuffers(chunks)).equals(new Buffer(buffer)), - ).toBe(true); +// const res = await fetch(`http://${server.hostname}:${server.port}`, {}); +// const arrayBuffer = await res.arrayBuffer(); +// expect( +// new Buffer(arrayBuffer).equals( +// new Buffer( +// await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), +// ), +// ), +// ).toBe(true); +// gcTick(); +// server.stop(); +// }); - controller.end(); - }, - }), - { - headers: { - "Content-Encoding": "gzip", - "Content-Type": "text/html; charset=utf-8", - "Content-Length": "1", - }, - }, - ); - }, - }); +// it("fetch() with a gzip response works (multiple chunks, TCP server)", async (done) => { +// const compressed = await Bun.file( +// import.meta.dir + "/fixture.html.gz", +// ).arrayBuffer(); +// const server = Bun.listen({ +// port: 4024, +// hostname: "0.0.0.0", +// socket: { +// async open(socket) { +// var corked: any[] = []; +// var cork = true; +// gcTick(); +// async function write(chunk) { +// await new Promise<void>((resolve, reject) => { +// if (cork) { +// corked.push(chunk); +// } - const res = await fetch(`http://${server.hostname}:${server.port}`, {}); - const arrayBuffer = await res.arrayBuffer(); - expect( - new Buffer(arrayBuffer).equals( - new Buffer( - await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), - ), - ), - ).toBe(true); - server.stop(); -}); +// if (!cork && corked.length) { +// socket.write(corked.join("")); +// corked.length = 0; +// } it("fetch() with a gzip response works (multiple chunks, TCP server)", async (done) => { const compressed = await Bun.file( diff --git a/test/bun.js/filesink.test.ts b/test/bun.js/filesink.test.ts index 038e8df19..a04144d0b 100644 --- a/test/bun.js/filesink.test.ts +++ b/test/bun.js/filesink.test.ts @@ -91,7 +91,7 @@ describe("FileSink", () => { } return Buffer.concat(chunks).toString(); // test it on a small chunk size - })(Bun.file(path).stream(4)); + })(Bun.file(path).stream(64)); return path; } @@ -106,6 +106,7 @@ describe("FileSink", () => { for (let i = 0; i < input.length; i++) { sink.write(input[i]); } + console.log("close", input[0].length); await sink.end(); if (!isPipe) { @@ -115,6 +116,7 @@ describe("FileSink", () => { } expect(output.byteLength).toBe(expected.byteLength); } else { + console.log("reading"); const output = await activeFIFO; expect(output).toBe(decoder.decode(expected)); } diff --git a/test/bun.js/hash.test.js b/test/bun.js/hash.test.js index 71ad5a229..0c1baaf14 100644 --- a/test/bun.js/hash.test.js +++ b/test/bun.js/hash.test.js @@ -1,36 +1,47 @@ import fs from "fs"; import { it, expect } from "bun:test"; import path from "path"; +import { gcTick } from "gc"; it(`Bun.hash()`, () => { + gcTick(); Bun.hash("hello world"); Bun.hash(new TextEncoder().encode("hello world")); }); it(`Bun.hash.wyhash()`, () => { Bun.hash.wyhash("hello world"); + gcTick(); Bun.hash.wyhash(new TextEncoder().encode("hello world")); }); it(`Bun.hash.adler32()`, () => { Bun.hash.adler32("hello world"); + gcTick(); Bun.hash.adler32(new TextEncoder().encode("hello world")); }); it(`Bun.hash.crc32()`, () => { Bun.hash.crc32("hello world"); + gcTick(); Bun.hash.crc32(new TextEncoder().encode("hello world")); }); it(`Bun.hash.cityHash32()`, () => { Bun.hash.cityHash32("hello world"); + gcTick(); Bun.hash.cityHash32(new TextEncoder().encode("hello world")); + gcTick(); }); it(`Bun.hash.cityHash64()`, () => { Bun.hash.cityHash64("hello world"); + gcTick(); Bun.hash.cityHash64(new TextEncoder().encode("hello world")); + gcTick(); }); it(`Bun.hash.murmur32v3()`, () => { Bun.hash.murmur32v3("hello world"); + gcTick(); Bun.hash.murmur32v3(new TextEncoder().encode("hello world")); }); it(`Bun.hash.murmur64v2()`, () => { Bun.hash.murmur64v2("hello world"); + gcTick(); Bun.hash.murmur64v2(new TextEncoder().encode("hello world")); }); diff --git a/test/bun.js/node-test-helpers.js b/test/bun.js/node-test-helpers.js index e7f6c74f6..0ebd6bc4f 100644 --- a/test/bun.js/node-test-helpers.js +++ b/test/bun.js/node-test-helpers.js @@ -1,6 +1,14 @@ -import { expect } from "bun:test"; +import { expect as expect_ } from "bun:test"; +import { gcTick } from "gc"; import assertNode from "node:assert"; +const expect = (actual) => { + gcTick(); + const ret = expect_(actual); + gcTick(); + return ret; +}; + export const strictEqual = (...args) => { let error = null; try { diff --git a/test/bun.js/node-timers.test.ts b/test/bun.js/node-timers.test.ts new file mode 100644 index 000000000..a1f2d08a4 --- /dev/null +++ b/test/bun.js/node-timers.test.ts @@ -0,0 +1,10 @@ +import { test } from "bun:test"; +import { setTimeout } from "node:timers"; + +test("unref is possible", () => { + const timer = setTimeout(() => { + throw new Error("should not be called"); + }, 1000); + timer.unref(); + clearTimeout(timer); +}); diff --git a/test/bun.js/spawn-streaming-stdout.test.ts b/test/bun.js/spawn-streaming-stdout.test.ts index 88c028db3..1f736533a 100644 --- a/test/bun.js/spawn-streaming-stdout.test.ts +++ b/test/bun.js/spawn-streaming-stdout.test.ts @@ -1,25 +1,41 @@ import { it, test, expect } from "bun:test"; import { spawn } from "bun"; import { bunExe } from "./bunExe"; +import { gcTick } from "gc"; test("spawn can read from stdout multiple chunks", async () => { - const proc = spawn({ - cmd: [bunExe(), import.meta.dir + "/spawn-streaming-stdout-repro.js"], - stdout: "pipe", - env: { - BUN_DEBUG_QUIET_LOGS: 1, - }, - }); + gcTick(true); + var exited; + await (async function () { + const proc = spawn({ + cmd: [bunExe(), import.meta.dir + "/spawn-streaming-stdout-repro.js"], + stdout: "pipe", + env: { + BUN_DEBUG_QUIET_LOGS: 1, + }, + }); + exited = proc.exited; + gcTick(true); + var counter = 0; + try { + for await (var chunk of proc.stdout) { + gcTick(true); + expect(new TextDecoder().decode(chunk)).toBe("Wrote to stdout\n"); + counter++; - var counter = 0; - for await (var chunk of proc.stdout) { - expect(new TextDecoder().decode(chunk)).toBe("Wrote to stdout\n"); - counter++; + if (counter > 3) break; + } + } catch (e) { + console.log(e.stack); + throw e; + } + gcTick(true); - if (counter > 3) break; - } - - expect(counter).toBe(4); - proc.kill(); - await proc.exited; + expect(counter).toBe(4); + gcTick(); + proc.kill(); + gcTick(); + })(); + await exited; + gcTick(true); }); diff --git a/test/bun.js/spawn.test.ts b/test/bun.js/spawn.test.ts index 418178e2b..0959034bb 100644 --- a/test/bun.js/spawn.test.ts +++ b/test/bun.js/spawn.test.ts @@ -5,54 +5,68 @@ import { rmdirSync, unlinkSync, rmSync, writeFileSync } from "node:fs"; for (let [gcTick, label] of [ [_gcTick, "gcTick"], - [() => {}, "no gc tick"], + // [() => {}, "no gc tick"], ] as const) { + Bun.gc(true); describe(label, () => { - describe("spawnSync", () => { - const hugeString = "hello".repeat(10000).slice(); - - it("as an array", () => { - const { stdout } = spawnSync(["echo", "hi"]); - - // stdout is a Buffer - const text = stdout!.toString(); - expect(text).toBe("hi\n"); - }); - - it("Uint8Array works as stdin", async () => { - const { stdout, stderr } = spawnSync({ - cmd: ["cat"], - stdin: new TextEncoder().encode(hugeString), - }); - - expect(stdout!.toString()).toBe(hugeString); - expect(stderr!.byteLength).toBe(0); - }); - - it("check exit code", async () => { - const { exitCode: exitCode1 } = spawnSync({ - cmd: ["ls"], - }); - const { exitCode: exitCode2 } = spawnSync({ - cmd: ["false"], - }); - expect(exitCode1).toBe(0); - expect(exitCode2).toBe(1); - }); - }); + // describe("spawnSync", () => { + // const hugeString = "hello".repeat(10000).slice(); + + // it("as an array", () => { + // const { stdout } = spawnSync(["echo", "hi"]); + // gcTick(); + // // stdout is a Buffer + // const text = stdout!.toString(); + // expect(text).toBe("hi\n"); + // gcTick(); + // }); + + // it("Uint8Array works as stdin", async () => { + // const { stdout, stderr } = spawnSync({ + // cmd: ["cat"], + // stdin: new TextEncoder().encode(hugeString), + // }); + // gcTick(); + // expect(stdout!.toString()).toBe(hugeString); + // expect(stderr!.byteLength).toBe(0); + // gcTick(); + // }); + + // it("check exit code", async () => { + // const { exitCode: exitCode1 } = spawnSync({ + // cmd: ["ls"], + // }); + // gcTick(); + // const { exitCode: exitCode2 } = spawnSync({ + // cmd: ["false"], + // }); + // gcTick(); + // expect(exitCode1).toBe(0); + // expect(exitCode2).toBe(1); + // gcTick(); + // }); + // }); describe("spawn", () => { const hugeString = "hello".repeat(10000).slice(); it("as an array", async () => { - const { stdout, exited } = spawn(["echo", "hello"], { - stdout: "pipe", - }); gcTick(); - expect(await new Response(stdout).text()).toBe("hello\n"); + await (async () => { + const { stdout } = spawn(["echo", "hello"], { + stdout: "pipe", + stderr: null, + stdin: null, + }); + gcTick(); + const text = await new Response(stdout).text(); + expect(text).toBe("hello\n"); + })(); + gcTick(); }); it("as an array with options object", async () => { + gcTick(); const { stdout } = spawn(["printenv", "FOO"], { cwd: "/tmp", env: { @@ -61,11 +75,12 @@ for (let [gcTick, label] of [ }, stdin: null, stdout: "pipe", - stderr: "inherit", + stderr: null, }); - + gcTick(); const text = await new Response(stdout).text(); expect(text).toBe("bar\n"); + gcTick(); }); it("Uint8Array works as stdin", async () => { @@ -76,30 +91,37 @@ for (let [gcTick, label] of [ stdin: new TextEncoder().encode(hugeString), stdout: Bun.file("/tmp/out.123.txt"), }); - + gcTick(); await exited; - expect(await Bun.file("/tmp/out.123.txt").text()).toBe(hugeString); + expect(require("fs").readFileSync("/tmp/out.123.txt", "utf8")).toBe( + hugeString, + ); + gcTick(); }); it("check exit code", async () => { const exitCode1 = await spawn({ cmd: ["ls"], }).exited; + gcTick(); const exitCode2 = await spawn({ cmd: ["false"], }).exited; + gcTick(); expect(exitCode1).toBe(0); expect(exitCode2).toBe(1); + gcTick(); }); it("nothing to stdout and sleeping doesn't keep process open 4ever", async () => { const proc = spawn({ cmd: ["sleep", "0.1"], }); - + gcTick(); for await (const _ of proc.stdout!) { throw new Error("should not happen"); } + gcTick(); }); it("check exit code from onExit", async () => { @@ -116,6 +138,7 @@ for (let [gcTick, label] of [ } }, }); + gcTick(); spawn({ cmd: ["false"], onExit(code) { @@ -126,9 +149,12 @@ for (let [gcTick, label] of [ } }, }); + gcTick(); }); + gcTick(); expect(exitCode1).toBe(0); expect(exitCode2).toBe(1); + gcTick(); }); it("Blob works as stdin", async () => { @@ -229,99 +255,111 @@ for (let [gcTick, label] of [ cmd: ["bash", import.meta.dir + "/bash-echo.sh"], stdout: "pipe", stdin: "pipe", + lazy: true, stderr: "inherit", }); - proc.stdin!.write(hugeString); + var stdout = proc.stdout!; + var reader = stdout.getReader(); + proc.stdin!.write("hey\n"); await proc.stdin!.end(); var text = ""; - var reader = proc.stdout!.getReader(); - var done = false; + + reader; + var done = false, + value; + while (!done) { - var { value, done } = await reader.read(); + ({ value, done } = await reader.read()); + console.log("i have ...read"); if (value) text += new TextDecoder().decode(value); if (done && text.length === 0) { reader.releaseLock(); - reader = proc.stdout!.getReader(); + reader = stdout.getReader(); done = false; } } - expect(text.trim().length).toBe(hugeString.length); - expect(text.trim()).toBe(hugeString); + expect(text.trim().length).toBe("hey".length); + expect(text.trim()).toBe("hey"); gcTick(); await proc.exited; }); - describe("pipe", () => { - function huge() { - return spawn({ - cmd: ["echo", hugeString], - stdout: "pipe", - stdin: "pipe", - stderr: "inherit", - }); - } - - function helloWorld() { - return spawn({ - cmd: ["echo", "hello"], - stdout: "pipe", - stdin: "pipe", - }); - } - - const fixtures = [ - [helloWorld, "hello"], - [huge, hugeString], - ] as const; - - for (const [callback, fixture] of fixtures) { - describe(fixture.slice(0, 12), () => { - describe("should should allow reading stdout", () => { - it("before exit", async () => { - const process = callback(); - const output = await readableStreamToText(process.stdout!); - const expected = fixture + "\n"; - expect(output.length).toBe(expected.length); - expect(output).toBe(expected); - - await process.exited; - }); - - it("before exit (chunked)", async () => { - const process = callback(); - var output = ""; - var reader = process.stdout!.getReader(); - var done = false; - while (!done) { - var { value, done } = await reader.read(); - if (value) output += new TextDecoder().decode(value); - } - - const expected = fixture + "\n"; - expect(output.length).toBe(expected.length); - expect(output).toBe(expected); - - await process.exited; - }); - - it("after exit", async () => { - const process = callback(); - await process.stdin!.end(); - - const output = await readableStreamToText(process.stdout!); - const expected = fixture + "\n"; - - expect(output.length).toBe(expected.length); - expect(output).toBe(expected); - - await process.exited; - }); - }); - }); - } - }); + // describe("pipe", () => { + // function huge() { + // return spawn({ + // cmd: ["echo", hugeString], + // stdout: "pipe", + // stdin: "pipe", + // stderr: "inherit", + // lazy: true, + // }); + // } + + // function helloWorld() { + // return spawn({ + // cmd: ["echo", "hello"], + // stdout: "pipe", + // stdin: "ignore", + // }); + // } + + // const fixtures = [ + // [helloWorld, "hello"], + // [huge, hugeString], + // ] as const; + + // for (const [callback, fixture] of fixtures) { + // describe(fixture.slice(0, 12), () => { + // describe("should should allow reading stdout", () => { + // it("before exit", async () => { + // const process = callback(); + // const output = readableStreamToText(process.stdout!); + // const expected = fixture + "\n"; + + // await Promise.all([ + // process.exited, + // output.then((output) => { + // expect(output.length).toBe(expected.length); + // expect(output).toBe(expected); + // }), + // ]); + // }); + + // it("before exit (chunked)", async () => { + // const process = callback(); + // var output = ""; + // const prom2 = (async function () { + // for await (const chunk of process.stdout) { + // output += new TextDecoder().decode(chunk); + // } + // })(); + + // const expected = fixture + "\n"; + + // await Promise.all([process.exited, prom2]); + // expect(output.length).toBe(expected.length); + // expect(output).toBe(expected); + // }); + + // it("after exit", async () => { + // const process = callback(); + + // const output = readableStreamToText(process.stdout!); + // const expected = fixture + "\n"; + // await Promise.all([ + // process.exited, + // output.then((output) => { + // expect(output.length).toBe(expected.length); + // expect(output).toBe(expected); + // }), + // ]); + // }); + // }); + // }); + // } + // }); }); }); } diff --git a/test/bun.js/streams.test.js b/test/bun.js/streams.test.js index bb676041b..a872b7701 100644 --- a/test/bun.js/streams.test.js +++ b/test/bun.js/streams.test.js @@ -218,6 +218,7 @@ it("Bun.file() read text from pipe", async () => { unlinkSync("/tmp/fifo"); } catch (e) {} + console.log("here"); mkfifo("/tmp/fifo", 0o666); // 65k so its less than the max on linux @@ -232,6 +233,8 @@ it("Bun.file() read text from pipe", async () => { "/tmp/fifo", ], stderr: "inherit", + stdout: null, + stdin: null, env: { FIFO_TEST: large, }, @@ -239,32 +242,17 @@ it("Bun.file() read text from pipe", async () => { const exited = proc.exited; proc.ref(); - var prom = new Promise((resolve, reject) => { - setTimeout(() => { - (async function () { - var reader = out.getReader(); - var total = 0; - - while (true) { - const chunk = await reader.read(); - total += chunk.value?.byteLength || 0; - if (chunk.value) chunks.push(chunk.value); - - if (chunk.done || total >= large.length) { - const output = new TextDecoder() - .decode(new Uint8Array(Buffer.concat(chunks))) - .trim(); - reader.releaseLock(); - resolve(output); - break; - } - } - })(); - }); - }); + const prom = (async function () { + while (chunks.length === 0) { + for await (const chunk of out) { + chunks.push(chunk); + } + console.log("done"); + } + })(); const [status, output] = await Promise.all([exited, prom]); - + console.log("here"); expect(output.length).toBe(large.length); expect(output).toBe(large); expect(status).toBe(0); |