mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 13:40:37 +00:00

Having an experimental feature behind a flag makes change if we are expecting significant breaking changes to its API. Since the Worker API has been essentially stable since its initial introduction, and no noticeable doubt about possibly not keeping the feature around has been voiced, removing the flag and thereby reducing the barrier to experimentation, and consequently receiving feedback on the implementation, seems like a good idea. PR-URL: https://github.com/nodejs/node/pull/25361 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { MessageChannel } = require('worker_threads');
|
|
|
|
// This tests various behaviors around transferring MessagePorts with closing
|
|
// or closed handles.
|
|
|
|
const { port1, port2 } = new MessageChannel();
|
|
|
|
const arrayBuf = new ArrayBuffer(10);
|
|
port1.onmessage = common.mustNotCall();
|
|
port2.onmessage = common.mustNotCall();
|
|
|
|
function testSingle(closedPort, potentiallyOpenPort) {
|
|
assert.throws(common.mustCall(() => {
|
|
potentiallyOpenPort.postMessage(null, [arrayBuf, closedPort]);
|
|
}), common.mustCall((err) => {
|
|
assert.strictEqual(err.name, 'DataCloneError');
|
|
assert.strictEqual(err.message,
|
|
'MessagePort in transfer list is already detached');
|
|
assert.strictEqual(err.code, 25);
|
|
assert.ok(err instanceof Error);
|
|
|
|
const DOMException = err.constructor;
|
|
assert.ok(err instanceof DOMException);
|
|
assert.strictEqual(DOMException.name, 'DOMException');
|
|
|
|
return true;
|
|
}));
|
|
|
|
// arrayBuf must not be transferred, even though it is present earlier in the
|
|
// transfer list than the closedPort.
|
|
assert.strictEqual(arrayBuf.byteLength, 10);
|
|
}
|
|
|
|
function testBothClosed() {
|
|
testSingle(port1, port2);
|
|
testSingle(port2, port1);
|
|
}
|
|
|
|
// Even though the port handles may not be completely closed in C++ land, the
|
|
// observable behavior must be that the closing/detachment is synchronous and
|
|
// instant.
|
|
|
|
port1.close(common.mustCall(testBothClosed));
|
|
testSingle(port1, port2);
|
|
port2.close(common.mustCall(testBothClosed));
|
|
testBothClosed();
|
|
|
|
function tickUnref(n, fn) {
|
|
if (n === 0) return fn();
|
|
setImmediate(tickUnref, n - 1, fn).unref();
|
|
}
|
|
|
|
tickUnref(10, common.mustNotCall('The communication channel is still open'));
|