mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
node tests
This commit is contained in:
40
test/js/node/test/parallel/test-worker-message-port-drain.js
Normal file
40
test/js/node/test/parallel/test-worker-message-port-drain.js
Normal file
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
|
||||
// This test ensures that the messages from the internal
|
||||
// message port are drained before the call to 'kDispose',
|
||||
// and so all the stdio messages from the worker are processed
|
||||
// in the parent and are pushed to their target streams.
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
Worker,
|
||||
isMainThread,
|
||||
parentPort,
|
||||
threadId,
|
||||
} = require('worker_threads');
|
||||
|
||||
if (isMainThread) {
|
||||
const workerIdsToOutput = new Map();
|
||||
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const worker = new Worker(__filename, { stdout: true });
|
||||
const workerOutput = [];
|
||||
workerIdsToOutput.set(worker.threadId, workerOutput);
|
||||
worker.on('message', console.log);
|
||||
worker.stdout.on('data', (chunk) => {
|
||||
workerOutput.push(chunk.toString().trim());
|
||||
});
|
||||
}
|
||||
|
||||
process.on('exit', () => {
|
||||
for (const [threadId, workerOutput] of workerIdsToOutput) {
|
||||
assert.ok(workerOutput.includes(`1 threadId: ${threadId}`));
|
||||
assert.ok(workerOutput.includes(`2 threadId: ${threadId}`));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log(`1 threadId: ${threadId}`);
|
||||
console.log(`2 threadId: ${threadId}`);
|
||||
parentPort.postMessage(Array(100).fill(1));
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { Worker, isMainThread } = require('worker_threads');
|
||||
|
||||
if (isMainThread) {
|
||||
const w = new Worker(__filename, { stdout: true });
|
||||
const expected = 'hello world';
|
||||
|
||||
let data = '';
|
||||
w.stdout.setEncoding('utf8');
|
||||
w.stdout.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
w.on('exit', common.mustCall(() => {
|
||||
assert.strictEqual(data, expected);
|
||||
}));
|
||||
} else {
|
||||
process.stdout.write('hello');
|
||||
process.stdout.write(' ');
|
||||
process.stdout.write('world');
|
||||
process.exit(0);
|
||||
}
|
||||
25
test/js/node/test/parallel/test-worker-stdio-flush.js
Normal file
25
test/js/node/test/parallel/test-worker-stdio-flush.js
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { Worker, isMainThread } = require('worker_threads');
|
||||
|
||||
if (isMainThread) {
|
||||
const w = new Worker(__filename, { stdout: true });
|
||||
const expected = 'hello world';
|
||||
|
||||
let data = '';
|
||||
w.stdout.setEncoding('utf8');
|
||||
w.stdout.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
w.on('exit', common.mustCall(() => {
|
||||
assert.strictEqual(data, expected);
|
||||
}));
|
||||
} else {
|
||||
process.on('exit', () => {
|
||||
process.stdout.write(' ');
|
||||
process.stdout.write('world');
|
||||
});
|
||||
process.stdout.write('hello');
|
||||
}
|
||||
Reference in New Issue
Block a user