11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/3020 41cb0ef41Sopenharmony_ci// Promises, nextTick, and queueMicrotask allow code to escape the timeout 51cb0ef41Sopenharmony_ci// set for runInContext, runInNewContext, and runInThisContext 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst common = require('../common'); 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst vm = require('vm'); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst NS_PER_MS = 1000000n; 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst hrtime = process.hrtime.bigint; 141cb0ef41Sopenharmony_ciconst nextTick = process.nextTick; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst waitDuration = common.platformTimeout(200n); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction loop() { 191cb0ef41Sopenharmony_ci const start = hrtime(); 201cb0ef41Sopenharmony_ci while (1) { 211cb0ef41Sopenharmony_ci const current = hrtime(); 221cb0ef41Sopenharmony_ci const span = (current - start) / NS_PER_MS; 231cb0ef41Sopenharmony_ci if (span >= waitDuration) { 241cb0ef41Sopenharmony_ci throw new Error( 251cb0ef41Sopenharmony_ci `escaped timeout at ${span} milliseconds!`); 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci// The bug won't happen 100% reliably so run the test a small number of times to 311cb0ef41Sopenharmony_ci// make sure we catch it if the bug exists. 321cb0ef41Sopenharmony_cifor (let i = 0; i < 4; i++) { 331cb0ef41Sopenharmony_ci assert.throws(() => { 341cb0ef41Sopenharmony_ci vm.runInNewContext( 351cb0ef41Sopenharmony_ci 'nextTick(loop); loop();', 361cb0ef41Sopenharmony_ci { 371cb0ef41Sopenharmony_ci hrtime, 381cb0ef41Sopenharmony_ci nextTick, 391cb0ef41Sopenharmony_ci loop, 401cb0ef41Sopenharmony_ci }, 411cb0ef41Sopenharmony_ci { timeout: common.platformTimeout(100) }, 421cb0ef41Sopenharmony_ci ); 431cb0ef41Sopenharmony_ci }, { 441cb0ef41Sopenharmony_ci code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 451cb0ef41Sopenharmony_ci }); 461cb0ef41Sopenharmony_ci} 47