Skip to content

Commit

Permalink
Remove global event listeners and abort all pending tests on context …
Browse files Browse the repository at this point in the history
…dispose.
  • Loading branch information
appurva21 committed May 29, 2023
1 parent 64dc679 commit a2b5dba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/postman-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const _ = require('lodash'),

TO_WAIT_BUFFER = 500, // time to wait for sandbox to declare timeout
CONSOLE_EVENT_NAME = 'execution.console',
ASSERTION_EVENT_NAME = 'execution.assertion',
ERROR_EVENT_NAME = 'execution.error',
EXECUTION_TIMEOUT_ERROR_MESSAGE = 'sandbox not responding',
BRIDGE_DISCONNECTING_ERROR_MESSAGE = 'sandbox: execution interrupted, bridge disconnecting.';

Expand Down Expand Up @@ -131,19 +133,25 @@ class PostmanSandbox extends UniversalVM {
}

dispose () {
_.forEach(this._executing, (irq, id) => {
irq && clearTimeout(irq);
this.once('dispose', () => {
_.forEach(this._executing, (irq, id) => {
irq && clearTimeout(irq);

// send an abort event to the sandbox so that it can do cleanups
this.dispatch('execution.abort.' + id);
// send an abort event to the sandbox so that it can do cleanups
this.dispatch('execution.abort.' + id);

// even though sandbox could bubble the result event upon receiving abort, that would reduce
// stability of the system in case sandbox was unresponsive.
this.emit('execution.result.' + id, new Error(BRIDGE_DISCONNECTING_ERROR_MESSAGE));
// even though sandbox could bubble the result event upon receiving abort, that would reduce
// stability of the system in case sandbox was unresponsive.
this.emit('execution.result.' + id, new Error(BRIDGE_DISCONNECTING_ERROR_MESSAGE));
});

this.removeAllListeners(CONSOLE_EVENT_NAME);
this.removeAllListeners(ASSERTION_EVENT_NAME);
this.removeAllListeners(ERROR_EVENT_NAME);
this.disconnect();
});

this.removeAllListeners(CONSOLE_EVENT_NAME);
this.disconnect();
this.dispatch('dispose');
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/sandbox/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ module.exports = function (bridge, glob) {
});
});

bridge.once('dispose', () => {
// Abort all pending assertions and cleanup the global tests state
Object.values(testsState).forEach((test) => { test.abort(); });
testsState = {};

bridge.dispatch('dispose');
});

/**
* @param {String} id
* @param {Event} event
Expand Down
6 changes: 6 additions & 0 deletions lib/sandbox/pmapi-setup-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ module.exports = function (pm, testsState, onAssertion) {
testId = testState.testId,
assertionData = getAssertionObject(testId, name, false);

// TODO: Do this along with test state initialization.
testState.abort = () => {
markAssertionAsFailure(assertionData, new Error('Execution aborted before test could complete'));
processAssertion(_testId, assertionData, options);
};

// if there is no assertion function, we simply move on
if (typeof assert !== FUNCTION) {
// Sending `options` as empty to force resolve the test
Expand Down

0 comments on commit a2b5dba

Please sign in to comment.