forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-worker-message-port-drain.js
40 lines (35 loc) · 1.14 KB
/
test-worker-message-port-drain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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));
}