11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test ensures that the messages from the internal
51cb0ef41Sopenharmony_ci// message port are drained before the call to 'kDispose',
61cb0ef41Sopenharmony_ci// and so all the stdio messages from the worker are processed
71cb0ef41Sopenharmony_ci// in the parent and are pushed to their target streams.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst {
111cb0ef41Sopenharmony_ci  Worker,
121cb0ef41Sopenharmony_ci  isMainThread,
131cb0ef41Sopenharmony_ci  parentPort,
141cb0ef41Sopenharmony_ci  threadId,
151cb0ef41Sopenharmony_ci} = require('worker_threads');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciif (isMainThread) {
181cb0ef41Sopenharmony_ci  const workerIdsToOutput = new Map();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  for (let i = 0; i < 2; i++) {
211cb0ef41Sopenharmony_ci    const worker = new Worker(__filename, { stdout: true });
221cb0ef41Sopenharmony_ci    const workerOutput = [];
231cb0ef41Sopenharmony_ci    workerIdsToOutput.set(worker.threadId, workerOutput);
241cb0ef41Sopenharmony_ci    worker.on('message', console.log);
251cb0ef41Sopenharmony_ci    worker.stdout.on('data', (chunk) => {
261cb0ef41Sopenharmony_ci      workerOutput.push(chunk.toString().trim());
271cb0ef41Sopenharmony_ci    });
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  process.on('exit', () => {
311cb0ef41Sopenharmony_ci    for (const [threadId, workerOutput] of workerIdsToOutput) {
321cb0ef41Sopenharmony_ci      assert.ok(workerOutput.includes(`1 threadId: ${threadId}`));
331cb0ef41Sopenharmony_ci      assert.ok(workerOutput.includes(`2 threadId: ${threadId}`));
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci} else {
371cb0ef41Sopenharmony_ci  console.log(`1 threadId: ${threadId}`);
381cb0ef41Sopenharmony_ci  console.log(`2 threadId: ${threadId}`);
391cb0ef41Sopenharmony_ci  parentPort.postMessage(Array(100).fill(1));
401cb0ef41Sopenharmony_ci}
41