forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-worker-message-port-infinite-message-loop.js
More file actions
40 lines (33 loc) · 1.15 KB
/
test-worker-message-port-infinite-message-loop.js
File metadata and controls
40 lines (33 loc) · 1.15 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
'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel } = require('worker_threads');
// Make sure that an infinite asynchronous .on('message')/postMessage loop
// does not lead to a stack overflow and does not starve the event loop.
// We schedule timeouts both from before the .on('message') handler and
// inside of it, which both should run.
const { port1, port2 } = new MessageChannel();
let count = 0;
port1.on('message', () => {
if (count === 0) {
setTimeout(common.mustCall(() => {
port1.close();
}), 0);
}
port2.postMessage(0);
assert(count++ < 10000, `hit ${count} loop iterations`);
});
port2.postMessage(0);
// This is part of the test -- the event loop should be available and not stall
// out due to the recursive .postMessage() calls.
setTimeout(common.mustCall(), 0);
// Assert that the 'message' handler was actually called.
//
// We do not want to assert a specific call count, so common.mustCall cannot be
// used in the port1.on('message' callback directly.
process.once(
'beforeExit',
common.mustCall(() => {
assert(count > 0, 'count should be greater than 0');
})
);