11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/3020
31cb0ef41Sopenharmony_ci// Promises used to allow code to escape the timeout
41cb0ef41Sopenharmony_ci// set for runInContext, runInNewContext, and runInThisContext.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cirequire('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst vm = require('vm');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst NS_PER_MS = 1000000n;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst hrtime = process.hrtime.bigint;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction loop() {
151cb0ef41Sopenharmony_ci  const start = hrtime();
161cb0ef41Sopenharmony_ci  while (1) {
171cb0ef41Sopenharmony_ci    const current = hrtime();
181cb0ef41Sopenharmony_ci    const span = (current - start) / NS_PER_MS;
191cb0ef41Sopenharmony_ci    if (span >= 2000n) {
201cb0ef41Sopenharmony_ci      throw new Error(
211cb0ef41Sopenharmony_ci        `escaped timeout at ${span} milliseconds!`);
221cb0ef41Sopenharmony_ci    }
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciassert.throws(() => {
271cb0ef41Sopenharmony_ci  vm.runInNewContext(
281cb0ef41Sopenharmony_ci    'Promise.resolve().then(() => loop());',
291cb0ef41Sopenharmony_ci    {
301cb0ef41Sopenharmony_ci      hrtime,
311cb0ef41Sopenharmony_ci      loop
321cb0ef41Sopenharmony_ci    },
331cb0ef41Sopenharmony_ci    { timeout: 10, microtaskMode: 'afterEvaluate' }
341cb0ef41Sopenharmony_ci  );
351cb0ef41Sopenharmony_ci}, {
361cb0ef41Sopenharmony_ci  code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
371cb0ef41Sopenharmony_ci  message: 'Script execution timed out after 10ms'
381cb0ef41Sopenharmony_ci});
39