summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-message-transfer-port-mark-as-untransferable.js
blob: 240928263871a48a2bd5bb0918f52ec1745a817b (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
'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel, markAsUntransferable } = require('worker_threads');

{
  const ab = new ArrayBuffer(8);

  markAsUntransferable(ab);
  assert.strictEqual(ab.byteLength, 8);

  const { port1, port2 } = new MessageChannel();
  port1.postMessage(ab, [ ab ]);

  assert.strictEqual(ab.byteLength, 8);  // The AB is not detached.
  port2.once('message', common.mustCall());
}

{
  const channel1 = new MessageChannel();
  const channel2 = new MessageChannel();

  markAsUntransferable(channel2.port1);

  assert.throws(() => {
    channel1.port1.postMessage(channel2.port1, [ channel2.port1 ]);
  }, /was found in message but not listed in transferList/);

  channel2.port1.postMessage('still works, not closed/transferred');
  channel2.port2.once('message', common.mustCall());
}

{
  for (const value of [0, null, false, true, undefined, [], {}]) {
    markAsUntransferable(value);  // Has no visible effect.
  }
}