forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-worker-message-port-transfer-self.js
More file actions
47 lines (37 loc) · 1.31 KB
/
test-worker-message-port-transfer-self.js
File metadata and controls
47 lines (37 loc) · 1.31 KB
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
// Flags: --experimental-worker
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');
const { MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
assert.throws(common.mustCall(() => {
port1.postMessage(null, [port1]);
}), common.mustCall((err) => {
assert.strictEqual(err.name, 'DataCloneError');
assert.strictEqual(err.message, 'Transfer list contains source port');
assert.strictEqual(err.code, 25);
assert.ok(err instanceof Error);
const DOMException = err.constructor;
assert.ok(err instanceof DOMException);
assert.strictEqual(DOMException.name, 'DOMException');
return true;
}));
// The failed transfer should not affect the ports in anyway.
port2.onmessage = common.mustCall((message) => {
assert.strictEqual(message, 2);
assert(util.inspect(port1).includes('active: true'), util.inspect(port1));
assert(util.inspect(port2).includes('active: true'), util.inspect(port2));
port1.close();
tick(10, () => {
assert(util.inspect(port1).includes('active: false'), util.inspect(port1));
assert(util.inspect(port2).includes('active: false'), util.inspect(port2));
});
});
port1.postMessage(2);
function tick(n, cb) {
if (--n > 0)
return setImmediate(() => tick(n-1, cb));
else
cb();
}