From bbf99ad40e84e8682c5dd391d05a228ed633cecd Mon Sep 17 00:00:00 2001 From: Kai Tamkun Date: Fri, 16 May 2025 14:54:57 -0700 Subject: [PATCH] Add test-vm-timeout-escape-promise.js --- .../test-vm-timeout-escape-promise.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/js/node/test/parallel/test-vm-timeout-escape-promise.js diff --git a/test/js/node/test/parallel/test-vm-timeout-escape-promise.js b/test/js/node/test/parallel/test-vm-timeout-escape-promise.js new file mode 100644 index 0000000000..36a76001af --- /dev/null +++ b/test/js/node/test/parallel/test-vm-timeout-escape-promise.js @@ -0,0 +1,39 @@ +'use strict'; + +// https://github.com/nodejs/node/issues/3020 +// Promises used to allow code to escape the timeout +// set for runInContext, runInNewContext, and runInThisContext. + +require('../common'); +const assert = require('assert'); +const vm = require('vm'); + +const NS_PER_MS = 1000000n; + +const hrtime = process.hrtime.bigint; + +function loop() { + const start = hrtime(); + while (1) { + const current = hrtime(); + const span = (current - start) / NS_PER_MS; + if (span >= 2000n) { + throw new Error( + `escaped timeout at ${span} milliseconds!`); + } + } +} + +assert.throws(() => { + vm.runInNewContext( + 'Promise.resolve().then(() => loop()); loop();', + { + hrtime, + loop + }, + { timeout: 5, microtaskMode: 'afterEvaluate' } + ); +}, { + code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', + message: 'Script execution timed out after 5ms' +});