• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3
4// This test ensures that the messages from the internal
5// message port are drained before the call to 'kDispose',
6// and so all the stdio messages from the worker are processed
7// in the parent and are pushed to their target streams.
8
9const assert = require('assert');
10const {
11  Worker,
12  isMainThread,
13  parentPort,
14  threadId,
15} = require('worker_threads');
16
17if (isMainThread) {
18  const workerIdsToOutput = new Map();
19
20  for (let i = 0; i < 2; i++) {
21    const worker = new Worker(__filename, { stdout: true });
22    const workerOutput = [];
23    workerIdsToOutput.set(worker.threadId, workerOutput);
24    worker.on('message', console.log);
25    worker.stdout.on('data', (chunk) => {
26      workerOutput.push(chunk.toString().trim());
27    });
28  }
29
30  process.on('exit', () => {
31    for (const [threadId, workerOutput] of workerIdsToOutput) {
32      assert.ok(workerOutput.includes(`1 threadId: ${threadId}`));
33      assert.ok(workerOutput.includes(`2 threadId: ${threadId}`));
34    }
35  });
36} else {
37  console.log(`1 threadId: ${threadId}`);
38  console.log(`2 threadId: ${threadId}`);
39  parentPort.postMessage(Array(100).fill(1));
40}
41