11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/3020
41cb0ef41Sopenharmony_ci// Promises used to allow code to escape the timeout
51cb0ef41Sopenharmony_ci// set for runInContext, runInNewContext, and runInThisContext.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cirequire('../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_cifunction loop() {
161cb0ef41Sopenharmony_ci  const start = hrtime();
171cb0ef41Sopenharmony_ci  while (1) {
181cb0ef41Sopenharmony_ci    const current = hrtime();
191cb0ef41Sopenharmony_ci    const span = (current - start) / NS_PER_MS;
201cb0ef41Sopenharmony_ci    if (span >= 2000n) {
211cb0ef41Sopenharmony_ci      throw new Error(
221cb0ef41Sopenharmony_ci        `escaped timeout at ${span} milliseconds!`);
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciassert.throws(() => {
281cb0ef41Sopenharmony_ci  vm.runInNewContext(
291cb0ef41Sopenharmony_ci    'Promise.resolve().then(() => loop()); loop();',
301cb0ef41Sopenharmony_ci    {
311cb0ef41Sopenharmony_ci      hrtime,
321cb0ef41Sopenharmony_ci      loop
331cb0ef41Sopenharmony_ci    },
341cb0ef41Sopenharmony_ci    { timeout: 5, microtaskMode: 'afterEvaluate' }
351cb0ef41Sopenharmony_ci  );
361cb0ef41Sopenharmony_ci}, {
371cb0ef41Sopenharmony_ci  code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
381cb0ef41Sopenharmony_ci  message: 'Script execution timed out after 5ms'
391cb0ef41Sopenharmony_ci});
40