eventric-testing
Version:
Testing helpers for eventric.js
68 lines (60 loc) • 2.33 kB
JavaScript
var EventualConsistencyUtilities;
EventualConsistencyUtilities = (function() {
function EventualConsistencyUtilities() {}
EventualConsistencyUtilities.prototype.waitForQueryToReturnResult = function(context, queryName, params, timeout) {
if (timeout == null) {
timeout = 5000;
}
return this.waitForResult(function() {
return context.query(queryName, params);
}, timeout)["catch"](function(error) {
var ref;
if ((error != null ? (ref = error.message) != null ? ref.indexOf('waitForResult') : void 0 : void 0) > -1) {
throw new Error("waitForQueryToReturnResult timed out for query '" + queryName + "' on context '" + context.name + "'\nwith params " + (JSON.stringify(params)));
} else {
throw error;
}
});
};
EventualConsistencyUtilities.prototype.waitForCommandToResolve = function(context, commandName, params, timeout) {
if (timeout == null) {
timeout = 5000;
}
return this.waitForResult(function() {
return context.command(commandName, params).then(function(result) {
return result || true;
})["catch"](function() {
return void 0;
});
}, timeout)["catch"](function() {
throw new Error("waitForCommandToResolve timed out for command '" + commandName + "' on context '" + context.name + "'\nwith params " + (JSON.stringify(params)));
});
};
EventualConsistencyUtilities.prototype.waitForResult = function(promiseFactory, timeout) {
if (timeout == null) {
timeout = 5000;
}
return new Promise(function(resolve, reject) {
var pollPromise, startTime;
startTime = new Date();
pollPromise = function() {
return promiseFactory().then(function(result) {
var timeoutExceeded;
if (result != null) {
resolve(result);
return;
}
timeoutExceeded = (new Date() - startTime) >= timeout;
if (!timeoutExceeded) {
setTimeout(pollPromise, 15);
return;
}
return reject(new Error("waitForResult timed out for '" + (promiseFactory.toString()) + "'"));
})["catch"](reject);
};
return pollPromise();
});
};
return EventualConsistencyUtilities;
})();
module.exports = new EventualConsistencyUtilities;