mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 05:12:29 +00:00
Merge remote-tracking branch 'origin/main' into nektro-patch-44307
This commit is contained in:
@@ -30,7 +30,7 @@ if (cluster.isPrimary) {
|
||||
}
|
||||
`,
|
||||
});
|
||||
bunRun(joinP(dir, "index.ts"), bunEnv);
|
||||
bunRun(joinP(dir, "index.ts"), bunEnv, true);
|
||||
});
|
||||
|
||||
test("cloneable and non-transferable not-equals (BunFile)", () => {
|
||||
@@ -49,6 +49,11 @@ if (cluster.isPrimary) {
|
||||
worker.on("online", function () {
|
||||
worker.send({ file });
|
||||
});
|
||||
worker.on("exit", function (code, signal) {
|
||||
if (code !== 0) {
|
||||
process.exit(code);
|
||||
}
|
||||
});
|
||||
worker.on("message", function (data) {
|
||||
worker.kill();
|
||||
const { file } = data;
|
||||
@@ -63,10 +68,14 @@ if (cluster.isPrimary) {
|
||||
console.log("W", msg);
|
||||
process.send!(msg);
|
||||
});
|
||||
process.on("uncaughtExceptionMonitor", (error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
`,
|
||||
});
|
||||
bunRun(joinP(dir, "index.ts"), bunEnv);
|
||||
bunRun(joinP(dir, "index.ts"), bunEnv, true);
|
||||
});
|
||||
|
||||
test("cloneable and non-transferable not-equals (net.BlockList)", () => {
|
||||
@@ -84,6 +93,11 @@ if (cluster.isPrimary) {
|
||||
worker.on("online", function () {
|
||||
worker.send({ blocklist });
|
||||
});
|
||||
worker.on("exit", function (code, signal) {
|
||||
if (code !== 0) {
|
||||
process.exit(code);
|
||||
}
|
||||
});
|
||||
worker.on("message", function (data) {
|
||||
worker.kill();
|
||||
const { blocklist } = data;
|
||||
@@ -95,10 +109,14 @@ if (cluster.isPrimary) {
|
||||
} else {
|
||||
process.on("message", msg => {
|
||||
console.log("W", msg);
|
||||
process.send!(msg);
|
||||
process.send!(msg);
|
||||
});
|
||||
process.on("uncaughtExceptionMonitor", (error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
`,
|
||||
});
|
||||
bunRun(joinP(dir, "index.ts"), bunEnv);
|
||||
bunRun(joinP(dir, "index.ts"), bunEnv, true);
|
||||
});
|
||||
|
||||
@@ -2038,4 +2038,52 @@ describe("readline.createInterface()", () => {
|
||||
// rl.write("text");
|
||||
// rl.write(null, { ctrl: true, name: "c" });
|
||||
// });
|
||||
|
||||
it("should support Symbol.dispose for using statements", () => {
|
||||
const input = new PassThrough();
|
||||
const output = new PassThrough();
|
||||
let closed = false;
|
||||
|
||||
{
|
||||
using rl = readline.createInterface({
|
||||
input: input,
|
||||
output: output,
|
||||
});
|
||||
|
||||
rl.on("close", () => {
|
||||
closed = true;
|
||||
});
|
||||
|
||||
// Verify the interface has the Symbol.dispose method
|
||||
assert.strictEqual(typeof rl[Symbol.dispose], "function");
|
||||
assert.strictEqual(!closed, true);
|
||||
}
|
||||
|
||||
// After exiting the using block, the interface should be closed
|
||||
assert.strictEqual(closed, true);
|
||||
});
|
||||
|
||||
it("should support Symbol.dispose as alias for close()", () => {
|
||||
const input = new PassThrough();
|
||||
const output = new PassThrough();
|
||||
let closed = false;
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: input,
|
||||
output: output,
|
||||
});
|
||||
|
||||
rl.on("close", () => {
|
||||
closed = true;
|
||||
});
|
||||
|
||||
// Verify Symbol.dispose exists and works the same as close()
|
||||
assert.strictEqual(typeof rl[Symbol.dispose], "function");
|
||||
assert.strictEqual(!closed, true);
|
||||
|
||||
rl[Symbol.dispose]();
|
||||
|
||||
assert.strictEqual(closed, true);
|
||||
assert.strictEqual(rl.closed, true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,4 +46,50 @@ describe("readline/promises.createInterface()", () => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("should support Symbol.dispose for using statements", () => {
|
||||
const fi = new FakeInput();
|
||||
let closed = false;
|
||||
|
||||
{
|
||||
using rl = readlinePromises.createInterface({
|
||||
input: fi,
|
||||
output: fi,
|
||||
});
|
||||
|
||||
rl.on("close", () => {
|
||||
closed = true;
|
||||
});
|
||||
|
||||
// Verify the interface has the Symbol.dispose method
|
||||
assert.strictEqual(typeof rl[Symbol.dispose], "function");
|
||||
assert.strictEqual(!closed, true);
|
||||
}
|
||||
|
||||
// After exiting the using block, the interface should be closed
|
||||
assert.strictEqual(closed, true);
|
||||
});
|
||||
|
||||
it("should support Symbol.dispose as alias for close()", () => {
|
||||
const fi = new FakeInput();
|
||||
let closed = false;
|
||||
|
||||
const rl = readlinePromises.createInterface({
|
||||
input: fi,
|
||||
output: fi,
|
||||
});
|
||||
|
||||
rl.on("close", () => {
|
||||
closed = true;
|
||||
});
|
||||
|
||||
// Verify Symbol.dispose exists and works the same as close()
|
||||
assert.strictEqual(typeof rl[Symbol.dispose], "function");
|
||||
assert.strictEqual(!closed, true);
|
||||
|
||||
rl[Symbol.dispose]();
|
||||
|
||||
assert.strictEqual(closed, true);
|
||||
assert.strictEqual(rl.closed, true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
if (common.isWindows) return; // TODO: BUN
|
||||
const assert = require('assert');
|
||||
const cp = require('child_process');
|
||||
|
||||
|
||||
58
test/js/node/worker_threads/worker-async-dispose.test.ts
Normal file
58
test/js/node/worker_threads/worker-async-dispose.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { Worker } from "worker_threads";
|
||||
|
||||
test("Worker implements Symbol.asyncDispose", async () => {
|
||||
const worker = new Worker(
|
||||
`
|
||||
const { parentPort } = require("worker_threads");
|
||||
parentPort?.postMessage("ready");
|
||||
`,
|
||||
{ eval: true },
|
||||
);
|
||||
|
||||
// Wait for the worker to be ready
|
||||
await new Promise(resolve => {
|
||||
worker.on("message", msg => {
|
||||
if (msg === "ready") {
|
||||
resolve(msg);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Test that Symbol.asyncDispose exists and is a function
|
||||
expect(typeof worker[Symbol.asyncDispose]).toBe("function");
|
||||
|
||||
// Test that calling Symbol.asyncDispose terminates the worker
|
||||
const disposeResult = await worker[Symbol.asyncDispose]();
|
||||
expect(disposeResult).toBeUndefined();
|
||||
});
|
||||
|
||||
test("Worker can be used with await using", async () => {
|
||||
let workerTerminated = false;
|
||||
|
||||
{
|
||||
await using worker = new Worker(
|
||||
`
|
||||
const { parentPort } = require("worker_threads");
|
||||
parentPort?.postMessage("hello from worker");
|
||||
`,
|
||||
{ eval: true },
|
||||
);
|
||||
|
||||
// Listen for worker exit to confirm termination
|
||||
worker.on("exit", () => {
|
||||
workerTerminated = true;
|
||||
});
|
||||
|
||||
// Wait for the worker message to ensure it's running
|
||||
await new Promise(resolve => {
|
||||
worker.on("message", resolve);
|
||||
});
|
||||
|
||||
// Worker should automatically terminate when leaving this block via Symbol.asyncDispose
|
||||
}
|
||||
|
||||
// Give a moment for the exit event to be emitted
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
expect(workerTerminated).toBe(true);
|
||||
});
|
||||
Reference in New Issue
Block a user