diff options
author | Timothy Gu <timothygu99@gmail.com> | 2018-06-24 23:10:37 -0400 |
---|---|---|
committer | Timothy Gu <timothygu99@gmail.com> | 2018-07-03 22:54:03 -0400 |
commit | f374d6aaf9a2a171c9cd100a4ca2d26a68f72cb8 (patch) | |
tree | de1cf48cf4a77bf4da01fbe04edf8d98356868c4 /test/parallel/test-worker-message-port-transfer-self.js | |
parent | 5f3bdb016a99d102ebafe86424e7761228ef7f70 (diff) | |
download | node-new-f374d6aaf9a2a171c9cd100a4ca2d26a68f72cb8.tar.gz |
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>
Diffstat (limited to 'test/parallel/test-worker-message-port-transfer-self.js')
-rw-r--r-- | test/parallel/test-worker-message-port-transfer-self.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/parallel/test-worker-message-port-transfer-self.js b/test/parallel/test-worker-message-port-transfer-self.js new file mode 100644 index 0000000000..1855023cdf --- /dev/null +++ b/test/parallel/test-worker-message-port-transfer-self.js @@ -0,0 +1,33 @@ +// Flags: --experimental-worker +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { MessageChannel } = require('worker_threads'); + +const { port1, port2 } = new MessageChannel(); + +assert.throws(common.mustCall(() => { + port1.postMessage(null, [port1]); +}), common.mustCall((err) => { + assert.strictEqual(err.name, 'DataCloneError'); + assert.strictEqual(err.message, 'Transfer list contains source port'); + 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; +})); + +// The failed transfer should not affect the ports in anyway. +port2.onmessage = common.mustCall((message) => { + assert.strictEqual(message, 2); + port1.close(); + + setTimeout(common.mustNotCall('The communication channel is still open'), + common.platformTimeout(1000)).unref(); +}); +port1.postMessage(2); |