0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/test/parallel/test-worker-message-port-close-while-receiving.js
Anna Henningsen b34f05ecf2
worker: prevent event loop starvation through MessagePorts
Limit the number of messages processed without interruption on a
given `MessagePort` to prevent event loop starvation, but still
make sure that all messages are emitted that were already in the
queue when emitting began.

This aligns the behaviour better with the web.

Refs: https://github.com/nodejs/node/pull/28030

PR-URL: https://github.com/nodejs/node/pull/29315
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2019-09-09 22:18:41 +02:00

16 lines
425 B
JavaScript

'use strict';
const common = require('../common');
const { MessageChannel } = require('worker_threads');
// Make sure that closing a message port while receiving messages on it does
// not stop messages that are already in the queue from being emitted.
const { port1, port2 } = new MessageChannel();
port1.on('message', common.mustCall(() => {
port1.close();
}, 2));
port2.postMessage('foo');
port2.postMessage('bar');