1'use strict'; 2 3// https://github.com/nodejs/node/issues/3020 4// Promises, nextTick, and queueMicrotask allow code to escape the timeout 5// set for runInContext, runInNewContext, and runInThisContext 6 7const common = require('../common'); 8const assert = require('assert'); 9const vm = require('vm'); 10 11const NS_PER_MS = 1000000n; 12 13const hrtime = process.hrtime.bigint; 14const nextTick = process.nextTick; 15 16const waitDuration = common.platformTimeout(100n); 17 18function loop() { 19 const start = hrtime(); 20 while (1) { 21 const current = hrtime(); 22 const span = (current - start) / NS_PER_MS; 23 if (span >= waitDuration) { 24 throw new Error( 25 `escaped timeout at ${span} milliseconds!`); 26 } 27 } 28} 29 30// The bug won't happen 100% reliably so run the test a small number of times to 31// make sure we catch it if the bug exists. 32for (let i = 0; i < 4; i++) { 33 assert.throws(() => { 34 vm.runInNewContext( 35 'nextTick(loop); loop();', 36 { 37 hrtime, 38 nextTick, 39 loop 40 }, 41 { timeout: common.platformTimeout(10) } 42 ); 43 }, { 44 code: 'ERR_SCRIPT_EXECUTION_TIMEOUT' 45 }); 46} 47