MCPcopy Index your code
hub / github.com/nodejs/node / test

Function test

test/parallel/test-worker-message-port-message-before-close.js:19–36  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17// - Thread 2 closes the port, discarding message B in the process.
18
19async function test() {
20 const worker = new Worker(`
21 require('worker_threads').parentPort.on('message', ({ port }) => {
22 port.postMessage('firstMessage');
23 port.postMessage('lastMessage');
24 port.close();
25 });
26 `, { eval: true });
27
28 for (let i = 0; i < 10000; i++) {
29 const { port1, port2 } = new MessageChannel();
30 worker.postMessage({ port: port2 }, [ port2 ]);
31 assert.deepStrictEqual(await once(port1, 'message'), ['firstMessage']);
32 assert.deepStrictEqual(await once(port1, 'message'), ['lastMessage']);
33 }
34
35 await worker.terminate();
36}
37
38test().then(common.mustCall());

Calls 3

postMessageMethod · 0.95
terminateMethod · 0.95
onceFunction · 0.70

Tested by

no test coverage detected