Skip to content

Commit a2b5dba

Browse files
committed
Remove global event listeners and abort all pending tests on context dispose.
1 parent 64dc679 commit a2b5dba

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib/postman-sandbox.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const _ = require('lodash'),
66

77
TO_WAIT_BUFFER = 500, // time to wait for sandbox to declare timeout
88
CONSOLE_EVENT_NAME = 'execution.console',
9+
ASSERTION_EVENT_NAME = 'execution.assertion',
10+
ERROR_EVENT_NAME = 'execution.error',
911
EXECUTION_TIMEOUT_ERROR_MESSAGE = 'sandbox not responding',
1012
BRIDGE_DISCONNECTING_ERROR_MESSAGE = 'sandbox: execution interrupted, bridge disconnecting.';
1113

@@ -131,19 +133,25 @@ class PostmanSandbox extends UniversalVM {
131133
}
132134

133135
dispose () {
134-
_.forEach(this._executing, (irq, id) => {
135-
irq && clearTimeout(irq);
136+
this.once('dispose', () => {
137+
_.forEach(this._executing, (irq, id) => {
138+
irq && clearTimeout(irq);
136139

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

140-
// even though sandbox could bubble the result event upon receiving abort, that would reduce
141-
// stability of the system in case sandbox was unresponsive.
142-
this.emit('execution.result.' + id, new Error(BRIDGE_DISCONNECTING_ERROR_MESSAGE));
143+
// even though sandbox could bubble the result event upon receiving abort, that would reduce
144+
// stability of the system in case sandbox was unresponsive.
145+
this.emit('execution.result.' + id, new Error(BRIDGE_DISCONNECTING_ERROR_MESSAGE));
146+
});
147+
148+
this.removeAllListeners(CONSOLE_EVENT_NAME);
149+
this.removeAllListeners(ASSERTION_EVENT_NAME);
150+
this.removeAllListeners(ERROR_EVENT_NAME);
151+
this.disconnect();
143152
});
144153

145-
this.removeAllListeners(CONSOLE_EVENT_NAME);
146-
this.disconnect();
154+
this.dispatch('dispose');
147155
}
148156
}
149157

lib/sandbox/execute.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ module.exports = function (bridge, glob) {
8686
});
8787
});
8888

89+
bridge.once('dispose', () => {
90+
// Abort all pending assertions and cleanup the global tests state
91+
Object.values(testsState).forEach((test) => { test.abort(); });
92+
testsState = {};
93+
94+
bridge.dispatch('dispose');
95+
});
96+
8997
/**
9098
* @param {String} id
9199
* @param {Event} event

lib/sandbox/pmapi-setup-runner.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ module.exports = function (pm, testsState, onAssertion) {
195195
testId = testState.testId,
196196
assertionData = getAssertionObject(testId, name, false);
197197

198+
// TODO: Do this along with test state initialization.
199+
testState.abort = () => {
200+
markAssertionAsFailure(assertionData, new Error('Execution aborted before test could complete'));
201+
processAssertion(_testId, assertionData, options);
202+
};
203+
198204
// if there is no assertion function, we simply move on
199205
if (typeof assert !== FUNCTION) {
200206
// Sending `options` as empty to force resolve the test

0 commit comments

Comments
 (0)