• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const util = require('util');
6const { MessageChannel } = require('worker_threads');
7const tick = require('../common/tick');
8
9const { port1, port2 } = new MessageChannel();
10
11assert.throws(common.mustCall(() => {
12  port1.postMessage(null, [port1]);
13}), common.mustCall((err) => {
14  assert.strictEqual(err.name, 'DataCloneError');
15  assert.strictEqual(err.message, 'Transfer list contains source port');
16  assert.strictEqual(err.code, 25);
17  assert.ok(err instanceof Error);
18
19  const DOMException = err.constructor;
20  assert.ok(err instanceof DOMException);
21  assert.strictEqual(DOMException.name, 'DOMException');
22
23  return true;
24}));
25
26// The failed transfer should not affect the ports in anyway.
27port2.onmessage = common.mustCall((message) => {
28  assert.strictEqual(message.data, 2);
29
30  const inspectedPort1 = util.inspect(port1);
31  const inspectedPort2 = util.inspect(port2);
32  assert(inspectedPort1.includes('active: true'), inspectedPort1);
33  assert(inspectedPort2.includes('active: true'), inspectedPort2);
34
35  port1.close();
36
37  tick(10, () => {
38    const inspectedPort1 = util.inspect(port1);
39    const inspectedPort2 = util.inspect(port2);
40    assert(inspectedPort1.includes('active: false'), inspectedPort1);
41    assert(inspectedPort2.includes('active: false'), inspectedPort2);
42  });
43});
44port1.postMessage(2);
45