summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-message-port-close.js
blob: 6562824d6a9ed319e127aaf9189531d738a2ef22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel, moveMessagePortToContext } = require('worker_threads');

// Make sure that .start() and .stop() do not throw on closing/closed
// MessagePorts.
// Refs: https://github.com/nodejs/node/issues/26463

function dummy() {}

{
  const { port1, port2 } = new MessageChannel();
  port1.close(common.mustCall(() => {
    port1.on('message', dummy);
    port1.off('message', dummy);
    port2.on('message', dummy);
    port2.off('message', dummy);
  }));
  port1.on('message', dummy);
  port1.off('message', dummy);
  port2.on('message', dummy);
  port2.off('message', dummy);
}

{
  const { port1 } = new MessageChannel();
  port1.on('message', dummy);
  port1.close(common.mustCall(() => {
    port1.off('message', dummy);
  }));
}

{
  const { port2 } = new MessageChannel();
  port2.close();
  assert.throws(() => moveMessagePortToContext(port2, {}), {
    code: 'ERR_CLOSED_MESSAGE_PORT',
    message: 'Cannot send data on closed MessagePort'
  });
}

// Refs: https://github.com/nodejs/node/issues/42296
{
  const ch = new MessageChannel();
  ch.port1.onmessage = common.mustNotCall();
  ch.port2.close();
  ch.port2.postMessage('fhqwhgads');
}