• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { MessageChannel, moveMessagePortToContext } = require('worker_threads');
5
6// Make sure that .start() and .stop() do not throw on closing/closed
7// MessagePorts.
8// Refs: https://github.com/nodejs/node/issues/26463
9
10function dummy() {}
11
12{
13  const { port1, port2 } = new MessageChannel();
14  port1.close(common.mustCall(() => {
15    port1.on('message', dummy);
16    port1.off('message', dummy);
17    port2.on('message', dummy);
18    port2.off('message', dummy);
19  }));
20  port1.on('message', dummy);
21  port1.off('message', dummy);
22  port2.on('message', dummy);
23  port2.off('message', dummy);
24}
25
26{
27  const { port1 } = new MessageChannel();
28  port1.on('message', dummy);
29  port1.close(common.mustCall(() => {
30    port1.off('message', dummy);
31  }));
32}
33
34{
35  const { port2 } = new MessageChannel();
36  port2.close();
37  assert.throws(() => moveMessagePortToContext(port2, {}), {
38    code: 'ERR_CLOSED_MESSAGE_PORT',
39    message: 'Cannot send data on closed MessagePort'
40  });
41}
42
43// Refs: https://github.com/nodejs/node/issues/42296
44{
45  const ch = new MessageChannel();
46  ch.port1.onmessage = common.mustNotCall();
47  ch.port2.close();
48  ch.port2.postMessage('fhqwhgads');
49}
50