2018-10-19 01:37:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// https://github.com/nodejs/node/issues/3020
|
|
|
|
// Promises, nextTick, and queueMicrotask allow code to escape the timeout
|
|
|
|
// set for runInContext, runInNewContext, and runInThisContext
|
|
|
|
|
2018-11-11 01:05:01 +01:00
|
|
|
const common = require('../common');
|
2018-10-19 01:37:24 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const vm = require('vm');
|
|
|
|
|
|
|
|
const NS_PER_MS = 1000000n;
|
|
|
|
|
|
|
|
const hrtime = process.hrtime.bigint;
|
|
|
|
|
2020-02-26 09:31:29 +01:00
|
|
|
const loopDuration = common.platformTimeout(1000n);
|
|
|
|
const timeout = common.platformTimeout(100);
|
2019-01-14 23:20:07 +01:00
|
|
|
|
2018-10-19 01:37:24 +02:00
|
|
|
function loop() {
|
|
|
|
const start = hrtime();
|
|
|
|
while (1) {
|
|
|
|
const current = hrtime();
|
|
|
|
const span = (current - start) / NS_PER_MS;
|
2019-01-14 23:20:07 +01:00
|
|
|
if (span >= loopDuration) {
|
2018-10-19 01:37:24 +02:00
|
|
|
throw new Error(
|
2019-01-14 23:20:07 +01:00
|
|
|
`escaped ${timeout}ms timeout at ${span}ms`);
|
2018-10-19 01:37:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
vm.runInNewContext(
|
|
|
|
'queueMicrotask(loop); loop();',
|
|
|
|
{
|
|
|
|
hrtime,
|
|
|
|
queueMicrotask,
|
2023-02-01 10:46:55 +01:00
|
|
|
loop,
|
2018-10-19 01:37:24 +02:00
|
|
|
},
|
2023-02-01 10:46:55 +01:00
|
|
|
{ timeout, microtaskMode: 'afterScriptRun' },
|
2018-10-19 01:37:24 +02:00
|
|
|
);
|
|
|
|
}, {
|
|
|
|
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
|
2023-02-01 10:46:55 +01:00
|
|
|
message: `Script execution timed out after ${timeout}ms`,
|
2018-10-19 01:37:24 +02:00
|
|
|
});
|