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;
|
|
|
|
|
2019-01-14 23:20:07 +01:00
|
|
|
const loopDuration = common.platformTimeout(100n);
|
|
|
|
const timeout = common.platformTimeout(10);
|
|
|
|
|
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,
|
|
|
|
loop
|
|
|
|
},
|
2019-01-14 23:20:07 +01:00
|
|
|
{ timeout }
|
2018-10-19 01:37:24 +02:00
|
|
|
);
|
|
|
|
}, {
|
|
|
|
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
|
2019-01-14 23:20:07 +01:00
|
|
|
message: `Script execution timed out after ${timeout}ms`
|
2018-10-19 01:37:24 +02:00
|
|
|
});
|