• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4
5const { MessageChannel } = require('worker_threads');
6
7// Make sure that an infinite asynchronous .on('message')/postMessage loop
8// does not lead to a stack overflow and does not starve the event loop.
9// We schedule timeouts both from before the the .on('message') handler and
10// inside of it, which both should run.
11
12const { port1, port2 } = new MessageChannel();
13let count = 0;
14port1.on('message', () => {
15  if (count === 0) {
16    setTimeout(common.mustCall(() => {
17      port1.close();
18    }), 0);
19  }
20
21  port2.postMessage(0);
22  assert(count++ < 10000, `hit ${count} loop iterations`);
23});
24
25port2.postMessage(0);
26
27// This is part of the test -- the event loop should be available and not stall
28// out due to the recursive .postMessage() calls.
29setTimeout(common.mustCall(), 0);
30