node/test/parallel/test-worker-message-port-transfer-target.js
Timothy Gu f374d6aaf9
messaging: fix edge cases with transferring ports
Currently, transferring the port on which postMessage is called causes a
segmentation fault, and transferring the target port causes a subsequent
port.onmessage setting to throw, or a deadlock if onmessage is set
before the postMessage. Fix both of these behaviors and align the
methods more closely with the normative definitions in the HTML
Standard.

Also, per spec postMessage must not throw just because the ports are
disentangled. Implement that behavior.

PR-URL: https://github.com/nodejs/node/pull/21540
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-07-03 22:54:03 -04:00

25 lines
806 B
JavaScript

// Flags: --experimental-worker
'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
const arrayBuf = new ArrayBuffer(10);
common.expectWarning('Warning',
'The target port was posted to itself, and the ' +
'communication channel was lost',
common.noWarnCode);
port2.onmessage = common.mustNotCall();
port2.postMessage(null, [port1, arrayBuf]);
// arrayBuf must be transferred, despite the fact that port2 never received the
// message.
assert.strictEqual(arrayBuf.byteLength, 0);
setTimeout(common.mustNotCall('The communication channel is still open'),
common.platformTimeout(1000)).unref();