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