1'use strict'; 2// https://github.com/nodejs/node/issues/3020 3// Promises used to allow code to escape the timeout 4// set for runInContext, runInNewContext, and runInThisContext. 5 6require('../common'); 7const assert = require('assert'); 8const vm = require('vm'); 9 10const NS_PER_MS = 1000000n; 11 12const hrtime = process.hrtime.bigint; 13 14function loop() { 15 const start = hrtime(); 16 while (1) { 17 const current = hrtime(); 18 const span = (current - start) / NS_PER_MS; 19 if (span >= 2000n) { 20 throw new Error( 21 `escaped timeout at ${span} milliseconds!`); 22 } 23 } 24} 25 26assert.throws(() => { 27 vm.runInNewContext( 28 'Promise.resolve().then(() => loop());', 29 { 30 hrtime, 31 loop 32 }, 33 { timeout: 10, microtaskMode: 'afterEvaluate' } 34 ); 35}, { 36 code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 37 message: 'Script execution timed out after 10ms' 38}); 39