0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-worker-message-port-close.js
Rich Trott 44131ad638 worker: do not send message if port is closing
Fixes: https://github.com/nodejs/node/issues/42296

PR-URL: https://github.com/nodejs/node/pull/42357
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2022-03-18 06:32:22 -07:00

50 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel, moveMessagePortToContext } = require('worker_threads');
// Make sure that .start() and .stop() do not throw on closing/closed
// MessagePorts.
// Refs: https://github.com/nodejs/node/issues/26463
function dummy() {}
{
const { port1, port2 } = new MessageChannel();
port1.close(common.mustCall(() => {
port1.on('message', dummy);
port1.off('message', dummy);
port2.on('message', dummy);
port2.off('message', dummy);
}));
port1.on('message', dummy);
port1.off('message', dummy);
port2.on('message', dummy);
port2.off('message', dummy);
}
{
const { port1 } = new MessageChannel();
port1.on('message', dummy);
port1.close(common.mustCall(() => {
port1.off('message', dummy);
}));
}
{
const { port2 } = new MessageChannel();
port2.close();
assert.throws(() => moveMessagePortToContext(port2, {}), {
code: 'ERR_CLOSED_MESSAGE_PORT',
message: 'Cannot send data on closed MessagePort'
});
}
// Refs: https://github.com/nodejs/node/issues/42296
{
const ch = new MessageChannel();
ch.port1.onmessage = common.mustNotCall();
ch.port2.close();
ch.port2.postMessage('fhqwhgads');
}