1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const { Worker, parentPort } = require('worker_threads'); 5 6// Do not use isMainThread so that this test itself can be run inside a Worker. 7if (!process.env.HAS_STARTED_WORKER) { 8 process.env.HAS_STARTED_WORKER = 1; 9 const w = new Worker(__filename); 10 const expectation = [ 4, undefined, null ]; 11 const actual = []; 12 w.on('message', common.mustCall((message) => { 13 actual.push(message); 14 if (actual.length === expectation.length) { 15 assert.deepStrictEqual(expectation, actual); 16 w.terminate(); 17 } 18 }, expectation.length)); 19 w.postMessage(2); 20} else { 21 parentPort.onmessage = common.mustCall((message) => { 22 parentPort.postMessage(message.data * 2); 23 parentPort.postMessage(undefined); 24 parentPort.postMessage(null); 25 }); 26} 27