node tests

This commit is contained in:
Ben Grant
2025-05-28 14:53:23 -07:00
parent 5bcc2a22cf
commit 19bb1600f9
3 changed files with 89 additions and 0 deletions

View 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));
}

View File

@@ -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);
}

View 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');
}