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_ci 151cb0ef41Sopenharmony_ciconst loopDuration = common.platformTimeout(1000n); 161cb0ef41Sopenharmony_ciconst timeout = common.platformTimeout(100); 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 >= loopDuration) { 241cb0ef41Sopenharmony_ci throw new Error( 251cb0ef41Sopenharmony_ci `escaped ${timeout}ms timeout at ${span}ms`); 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciassert.throws(() => { 311cb0ef41Sopenharmony_ci vm.runInNewContext( 321cb0ef41Sopenharmony_ci 'queueMicrotask(loop); loop();', 331cb0ef41Sopenharmony_ci { 341cb0ef41Sopenharmony_ci hrtime, 351cb0ef41Sopenharmony_ci queueMicrotask, 361cb0ef41Sopenharmony_ci loop, 371cb0ef41Sopenharmony_ci }, 381cb0ef41Sopenharmony_ci { timeout, microtaskMode: 'afterScriptRun' }, 391cb0ef41Sopenharmony_ci ); 401cb0ef41Sopenharmony_ci}, { 411cb0ef41Sopenharmony_ci code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 421cb0ef41Sopenharmony_ci message: `Script execution timed out after ${timeout}ms`, 431cb0ef41Sopenharmony_ci}); 44