summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-message-port-message-port-transferring.js
blob: b4f5726665147d64a8a3ff3e6f86f8f3383a0a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';
const common = require('../common');
const assert = require('assert');

const { MessageChannel } = require('worker_threads');

{
  const { port1: basePort1, port2: basePort2 } = new MessageChannel();
  const {
    port1: transferredPort1, port2: transferredPort2
  } = new MessageChannel();

  basePort1.postMessage({ transferredPort1 }, [ transferredPort1 ]);
  basePort2.on('message', common.mustCall(({ transferredPort1 }) => {
    transferredPort1.postMessage('foobar');
    transferredPort2.on('message', common.mustCall((msg) => {
      assert.strictEqual(msg, 'foobar');
      transferredPort1.close(common.mustCall());
      basePort1.close(common.mustCall());
    }));
  }));
}