diff --git a/lib/postman-sandbox.js b/lib/postman-sandbox.js index 907a5c45..bcc745db 100644 --- a/lib/postman-sandbox.js +++ b/lib/postman-sandbox.js @@ -146,9 +146,7 @@ class PostmanSandbox extends UniversalVM { this.emit('execution.result.' + id, new Error(BRIDGE_DISCONNECTING_ERROR_MESSAGE)); }); - setTimeout(() => { - this.disconnect(); - }, 100); + this.disconnect(); } } diff --git a/lib/sandbox/execute-context.js b/lib/sandbox/execute-context.js index 835e0d48..40aed288 100644 --- a/lib/sandbox/execute-context.js +++ b/lib/sandbox/execute-context.js @@ -50,13 +50,10 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss }); scope.exec(code, function (err) { - // we check if the execution went async by determining the timer queue length at this time - execution.return.async = (timers.queueLength() > 0); - - // call this hook to perform any post script execution tasks legacy.finish(scope, pmapi, onAssertion); - function complete () { + // call this hook to perform any post script execution tasks + // if timers are running, we do not need to proceed with any logic of completing execution. // instead we wait for timer completion callback to fire if (execution.return.async) { @@ -67,6 +64,6 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss timers.terminate(err); } - setImmediate(complete); + timers.getWrappedTimer('setImmediate')(complete); }); }; diff --git a/lib/sandbox/execute.js b/lib/sandbox/execute.js index d2964bef..8278ebce 100644 --- a/lib/sandbox/execute.js +++ b/lib/sandbox/execute.js @@ -153,7 +153,7 @@ module.exports = function (bridge, glob) { execution.return.async = true; }, function (err, dnd) { // clear timeout tracking timer - waiting && (waiting = clearTimeout(waiting)); + waiting && (waiting = timers.getWrappedTimer('clearTimeout')(waiting)); // do not allow any more timers if (timers) { @@ -180,7 +180,7 @@ module.exports = function (bridge, glob) { // if a timeout is set, we must ensure that all pending timers are cleared and an execution timeout event is // triggered. - _.isFinite(options.timeout) && (waiting = setTimeout(function () { + _.isFinite(options.timeout) && (waiting = timers.getWrappedTimer('setTimeout')(function () { timers.terminate(new Error('sandbox: ' + (execution.return.async ? 'asynchronous' : 'synchronous') + ' script execution timeout')); }, options.timeout)); diff --git a/lib/sandbox/timers.js b/lib/sandbox/timers.js index b408cd02..bc3e78de 100644 --- a/lib/sandbox/timers.js +++ b/lib/sandbox/timers.js @@ -114,7 +114,6 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { total = 0, // accumulator to keep track of total timers pending = 0, // counters to keep track of running timers sealed = false, // flag that stops all new timer additions - wentAsync = false, computeTimerEvents; // do special handling to enable emulation of immediate timers in hosts that lacks them @@ -166,7 +165,7 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { } if (pending === 0 && computeTimerEvents.started) { - setImmediate(maybeEndAllTimers); + timers.setImmediate(maybeEndAllTimers); } if (pending > 0 && !computeTimerEvents.started) { @@ -191,8 +190,6 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { args = arrayProtoSlice.call(arguments); args[0] = function () { - wentAsync = true; // mark that we did go async once. this will ensure we do not pass erroneous events - // call the actual callback with a dummy context try { callback.apply(dummyContext, staticTimerFunctions[name] ? arguments : null); } catch (e) { onError && onError(e); } @@ -211,9 +208,6 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { } }; - // for static timers - staticTimerFunctions[name] && (wentAsync = true); - // call the underlying timer function and keep a track of its irq running[id] = timers[('set' + name)].apply(this, args); args = null; // precaution @@ -245,7 +239,7 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { catch (e) { onError(e); } // decrement counters and call the clearing timer function - computeTimerEvents(-1, !wentAsync); + computeTimerEvents(-1); args = underLyingId = null; // just a precaution }; @@ -272,7 +266,6 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { } }.bind(this)); - /** * @memberof Timerz.prototype * @returns {Number} @@ -281,6 +274,10 @@ function Timerz (delegations, onError, onAnyTimerStart, onAllTimerEnd) { return pending; }; + this.getWrappedTimer = function (fnName) { + return timers[fnName]; + }; + /** * @memberof Timerz.prototype */ diff --git a/test/unit/sandbox-timers.test.js b/test/unit/sandbox-timers.test.js index 283d7462..7a8fd1fa 100644 --- a/test/unit/sandbox-timers.test.js +++ b/test/unit/sandbox-timers.test.js @@ -59,7 +59,7 @@ describe('timers inside sandbox', function () { expect(err).to.be.null; expect(res).to.nested.include({ - 'return.async': false + 'return.async': true }); // we wait for a while to ensure that the timeout was actually cleared. @@ -114,7 +114,7 @@ describe('timers inside sandbox', function () { expect(err).to.be.null; expect(res).to.nested.include({ - 'return.async': false + 'return.async': true }); // we wait for a while to ensure that the timeout was actually cleared.