Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for asynchronous tests with pending state #926

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/postman-sandbox-fleet.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,24 @@ class PostmanSandboxFleet {
/**
* Dispose off all initialized sandbox instances from the fleet
*
* @param {Function} [callback] -
* @returns {void}
*/
disposeAll () {
disposeAll (callback) {
let disposedCount = 0;

if (typeof callback !== 'function') {
callback = _.noop;
}

this.fleet.forEach((context, templateName) => {
context.dispose();
context.dispose(() => {
this.fleet.delete(templateName);

this.fleet.delete(templateName);
if (++disposedCount === this.fleet.size) {
return callback();
}
});
});
}
}
Expand Down
30 changes: 20 additions & 10 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 @@ -130,20 +132,28 @@ class PostmanSandbox extends UniversalVM {
});
}

dispose () {
_.forEach(this._executing, (irq, id) => {
irq && clearTimeout(irq);
dispose (callback) {
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();

typeof callback === 'function' && callback();
});

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

Expand Down
15 changes: 13 additions & 2 deletions lib/sandbox/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ module.exports = function (bridge, glob) {
// For caching required information provided during
// initialization which will be used during execution
let initializationOptions = {},
initializeExecution;
initializeExecution,

// Tests state in the context of the current execution
testsState = {};

/**
* @param {Object} options
Expand Down Expand Up @@ -83,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 Expand Up @@ -214,7 +225,7 @@ module.exports = function (bridge, glob) {
var eventId = timers.setEvent(callback);

bridge.dispatch(executionRequestEventName, options.cursor, id, eventId, request);
}, dispatchAssertions, new PostmanCookieStore(id, bridge, timers), {
}, testsState, dispatchAssertions, new PostmanCookieStore(id, bridge, timers), {
disabledAPIs: initializationOptions.disabledAPIs
})
),
Expand Down