diff options
author | Marvin Hagemeister <marvin@deno.com> | 2024-07-25 23:06:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-25 23:06:19 +0200 |
commit | 0cf7f268a7df7711ac6ab8c2c67b4d7abf454fcd (patch) | |
tree | 7520b6dd22c3088a8431d9feda65b981e872b6ef /tests/node_compat/test/parallel/test-worker-message-port-infinite-message-loop.js | |
parent | 763f05e74dfd0032b238603f625893a52e363591 (diff) |
fix(node/worker_threads): support `port.once()` (#24725)
Support `MessagePort.once` in Node mode and enable relevant
`worker_threads` test. Noticed that another Node test was passing as
well, so I enabled that too.
Diffstat (limited to 'tests/node_compat/test/parallel/test-worker-message-port-infinite-message-loop.js')
-rw-r--r-- | tests/node_compat/test/parallel/test-worker-message-port-infinite-message-loop.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/node_compat/test/parallel/test-worker-message-port-infinite-message-loop.js b/tests/node_compat/test/parallel/test-worker-message-port-infinite-message-loop.js new file mode 100644 index 000000000..784f72b9b --- /dev/null +++ b/tests/node_compat/test/parallel/test-worker-message-port-infinite-message-loop.js @@ -0,0 +1,36 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'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); |