Refactor Spec and QueueRunner [#62585700]

- QueueRunner now responsible for timing out async specs instead of
   Spec
 - Make sure only spec functions are timeoutable and not suites (due to
   the refactor)
This commit is contained in:
Greg Cobb and Sheel Choksi
2014-02-25 12:15:12 -08:00
parent 84160ff51d
commit 5aac3e3292
8 changed files with 143 additions and 174 deletions

View File

@@ -136,6 +136,7 @@ getJasmineRequireObj().Env = function(j$) {
var queueRunnerFactory = function(options) {
options.catchException = catchException;
options.clearStack = options.clearStack || clearStack;
options.timer = {setTimeout: realSetTimeout, clearTimeout: realClearTimeout};
new j$.QueueRunner(options).execute();
};
@@ -271,8 +272,7 @@ getJasmineRequireObj().Env = function(j$) {
description: description,
expectationResultFactory: expectationResultFactory,
queueRunnerFactory: queueRunnerFactory,
fn: fn,
timer: {setTimeout: realSetTimeout, clearTimeout: realClearTimeout}
fn: fn
});
runnableLookupTable[spec.id] = spec;

View File

@@ -1,4 +1,4 @@
getJasmineRequireObj().QueueRunner = function() {
getJasmineRequireObj().QueueRunner = function(j$) {
function QueueRunner(attrs) {
this.fns = attrs.fns || [];
@@ -6,7 +6,9 @@ getJasmineRequireObj().QueueRunner = function() {
this.clearStack = attrs.clearStack || function(fn) {fn();};
this.onException = attrs.onException || function() {};
this.catchException = attrs.catchException || function() { return true; };
this.enforceTimeout = attrs.enforceTimeout || function() { return false; };
this.userContext = {};
this.timer = attrs.timeout || {setTimeout: setTimeout, clearTimeout: clearTimeout};
}
QueueRunner.prototype.execute = function() {
@@ -42,7 +44,21 @@ getJasmineRequireObj().QueueRunner = function() {
}
function attemptAsync(fn) {
var next = function () { self.run(fns, iterativeIndex + 1); };
var clearTimeout = function () {
Function.prototype.apply.apply(self.timer.clearTimeout, [j$.getGlobal(), [timeoutId]]);
},
next = function () {
clearTimeout(timeoutId);
self.run(fns, iterativeIndex + 1);
},
timeoutId;
if (self.enforceTimeout()) {
timeoutId = Function.prototype.apply.apply(self.timer.setTimeout, [j$.getGlobal(), [function() {
self.onException(new Error('Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.'));
next();
}, j$.DEFAULT_TIMEOUT_INTERVAL]]);
}
try {
fn.call(self.userContext, next);

View File

@@ -14,8 +14,6 @@ getJasmineRequireObj().Spec = function(j$) {
this.queueRunnerFactory = attrs.queueRunnerFactory || function() {};
this.catchingExceptions = attrs.catchingExceptions || function() { return true; };
this.timer = attrs.timer || {setTimeout: setTimeout, clearTimeout: clearTimeout};
if (!this.fn) {
this.pend();
}
@@ -40,8 +38,7 @@ getJasmineRequireObj().Spec = function(j$) {
};
Spec.prototype.execute = function(onComplete) {
var self = this,
timeout;
var self = this;
this.onStart(this);
@@ -50,42 +47,16 @@ getJasmineRequireObj().Spec = function(j$) {
return;
}
function timeoutable(fn) {
return function(done) {
timeout = Function.prototype.apply.apply(self.timer.setTimeout, [j$.getGlobal(), [function() {
onException(new Error('Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.'));
done();
}, j$.DEFAULT_TIMEOUT_INTERVAL]]);
var callDone = function() {
clearTimeoutable();
done();
};
fn.call(this, callDone); //TODO: do we care about more than 1 arg?
};
}
function clearTimeoutable() {
Function.prototype.apply.apply(self.timer.clearTimeout, [j$.getGlobal(), [timeout]]);
timeout = void 0;
}
var allFns = this.beforeFns().concat(this.fn).concat(this.afterFns()),
allTimeoutableFns = [];
for (var i = 0; i < allFns.length; i++) {
var fn = allFns[i];
allTimeoutableFns.push(fn.length > 0 ? timeoutable(fn) : fn);
}
var allFns = this.beforeFns().concat(this.fn).concat(this.afterFns());
this.queueRunnerFactory({
fns: allTimeoutableFns,
fns: allFns,
onException: onException,
onComplete: complete
onComplete: complete,
enforceTimeout: function() { return true; }
});
function onException(e) {
clearTimeoutable();
if (Spec.isPendingSpecException(e)) {
self.pend();
return;

View File

@@ -24,7 +24,7 @@ getJasmineRequireObj().core = function(jRequire) {
j$.matchersUtil = jRequire.matchersUtil(j$);
j$.ObjectContaining = jRequire.ObjectContaining(j$);
j$.pp = jRequire.pp(j$);
j$.QueueRunner = jRequire.QueueRunner();
j$.QueueRunner = jRequire.QueueRunner(j$);
j$.ReportDispatcher = jRequire.ReportDispatcher();
j$.Spec = jRequire.Spec(j$);
j$.SpyStrategy = jRequire.SpyStrategy();