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

@@ -55,21 +55,15 @@ describe("QueueRunner", function() {
afterCallback = jasmine.createSpy('afterCallback'),
fn1 = function(done) {
beforeCallback();
setTimeout(function() {
done()
}, 100);
setTimeout(done, 100);
},
fn2 = function(done) {
fnCallback();
setTimeout(function() {
done()
}, 100);
setTimeout(done, 100);
},
fn3 = function(done) {
afterCallback();
setTimeout(function() {
done()
}, 100);
setTimeout(done, 100);
},
queueRunner = new j$.QueueRunner({
fns: [fn1, fn2, fn3],
@@ -98,6 +92,86 @@ describe("QueueRunner", function() {
expect(onComplete).toHaveBeenCalled();
});
it("sets a timeout if requested for asynchronous functions so they don't go on forever", function() {
var beforeFn = function(done) { },
fn = jasmine.createSpy('fn'),
onComplete = jasmine.createSpy('onComplete'),
onException = jasmine.createSpy('onException'),
queueRunner = new j$.QueueRunner({
fns: [beforeFn, fn],
onComplete: onComplete,
onException: onException,
enforceTimeout: function() { return true; }
});
queueRunner.execute();
expect(fn).not.toHaveBeenCalled();
jasmine.clock().tick(j$.DEFAULT_TIMEOUT_INTERVAL);
expect(onException).toHaveBeenCalledWith(jasmine.any(Error));
expect(fn).toHaveBeenCalled();
expect(onComplete).toHaveBeenCalled();
});
it("by default does not set a timeout for asynchronous functions", function() {
var beforeFn = function(done) { },
fn = jasmine.createSpy('fn'),
onComplete = jasmine.createSpy('onComplete'),
onException = jasmine.createSpy('onException'),
queueRunner = new j$.QueueRunner({
fns: [beforeFn, fn],
onComplete: onComplete,
onException: onException,
});
queueRunner.execute();
expect(fn).not.toHaveBeenCalled();
jasmine.clock().tick(j$.DEFAULT_TIMEOUT_INTERVAL);
expect(onException).not.toHaveBeenCalled();
expect(fn).not.toHaveBeenCalled();
expect(onComplete).not.toHaveBeenCalled();
});
it("clears the timeout when an async function throws an exception, to prevent additional onException calls", function() {
var fn = function(done) { throw new Error("error!"); },
onComplete = jasmine.createSpy('onComplete'),
onException = jasmine.createSpy('onException'),
queueRunner = new j$.QueueRunner({
fns: [fn],
onComplete: onComplete,
onException: onException
});
queueRunner.execute();
expect(onComplete).toHaveBeenCalled();
expect(onException).toHaveBeenCalled();
jasmine.clock().tick(j$.DEFAULT_TIMEOUT_INTERVAL);
expect(onException.calls.count()).toEqual(1);
});
it("clears the timeout when the done callback is called", function() {
var fn = function(done) { done(); },
onComplete = jasmine.createSpy('onComplete'),
onException = jasmine.createSpy('onException'),
queueRunner = new j$.QueueRunner({
fns: [fn],
onComplete: onComplete,
onException: onException
});
queueRunner.execute();
expect(onComplete).toHaveBeenCalled();
jasmine.clock().tick(j$.DEFAULT_TIMEOUT_INTERVAL);
expect(onException).not.toHaveBeenCalled();
});
});
it("calls an exception handler when an exception is thrown in a fn", function() {
@@ -124,7 +198,7 @@ describe("QueueRunner", function() {
catchException: function(e) { return false; }
});
expect(function() { queueRunner.execute(); }).toThrow();
expect(queueRunner.execute).toThrow();
});
it("continues running the functions even after an exception is thrown in an async spec", function() {

View File

@@ -222,90 +222,7 @@ describe("Spec", function() {
expect(specNameSpy.calls.mostRecent().args[0].id).toEqual(spec.id);
});
it("sets a timeout for async functions to keep them from running forever", function() {
var queueRunnerSpy = jasmine.createSpy('queue runner'),
setTimeoutSpy = jasmine.createSpy('setTimeout'),
spec = new j$.Spec({
beforeFns: function() { return [function(done) { }]; },
fn: function(done) { },
afterFns: function() { return [function(done) { }]; },
timer: {
setTimeout: setTimeoutSpy,
clearTimeout: function() {}
},
queueRunnerFactory: queueRunnerSpy
});
spec.execute();
var fns = queueRunnerSpy.calls.mostRecent().args[0].fns;
for (var i = 0; i < fns.length; i++) {
fns[i]();
}
expect(setTimeoutSpy.calls.count()).toEqual(3);
expect(setTimeoutSpy).toHaveBeenCalledWith(jasmine.any(Function), j$.DEFAULT_TIMEOUT_INTERVAL);
});
it("resets the timeout timer when an async before throws an exception", function() {
var queueRunnerSpy = jasmine.createSpy('queueRunner'),
clearTimeoutSpy = jasmine.createSpy('clear timeout'),
spec = new j$.Spec({
beforeFns: function() { return [function(done) {}]; },
fn: function() { },
timer: {
setTimeout: function () { return 920; },
clearTimeout: clearTimeoutSpy
},
queueRunnerFactory: queueRunnerSpy
});
spec.execute();
queueRunnerSpy.calls.mostRecent().args[0].fns[0]();
queueRunnerSpy.calls.mostRecent().args[0].onException(new Error());
expect(clearTimeoutSpy).toHaveBeenCalledWith(920);
});
it("resets the timeout timer when an async spec throws an exception", function() {
var queueRunnerSpy = jasmine.createSpy('queueRunner'),
clearTimeoutSpy = jasmine.createSpy('clear timeout'),
spec = new j$.Spec({
fn: function(done) { },
timer: {
setTimeout: function () { return 920; },
clearTimeout: clearTimeoutSpy
},
queueRunnerFactory: queueRunnerSpy
});
spec.execute();
queueRunnerSpy.calls.mostRecent().args[0].fns[0]();
queueRunnerSpy.calls.mostRecent().args[0].onException(new Error());
expect(clearTimeoutSpy).toHaveBeenCalledWith(920);
});
it("resets the timeout timer when an async after spec throws an exception", function() {
var queueRunnerSpy = jasmine.createSpy('queueRunner'),
clearTimeoutSpy = jasmine.createSpy('clear timeout'),
spec = new j$.Spec({
fn: function() { },
afterFns: function() { return [function(done) {}]; },
timer: {
setTimeout: function () { return 920; },
clearTimeout: clearTimeoutSpy
},
queueRunnerFactory: queueRunnerSpy
});
spec.execute();
queueRunnerSpy.calls.mostRecent().args[0].fns[1]();
queueRunnerSpy.calls.mostRecent().args[0].onException(new Error());
expect(clearTimeoutSpy).toHaveBeenCalledWith(920);
});
describe("when a spec is marked pending during execution", function() {
describe("when a spec is marked pending during execution", function() {
it("should mark the spec as pending", function() {
var fakeQueueRunner = function(opts) {
opts.onException(new Error(j$.Spec.pendingSpecExceptionMessage));

View File

@@ -299,10 +299,14 @@ describe("Env integration", function() {
it("should wait a specified interval before failing specs haven't called done yet", function(done) {
var env = new j$.Env(),
reporter = jasmine.createSpyObj('fakeReporter', [ "specDone" ]);
reporter = jasmine.createSpyObj('fakeReporter', [ "specDone", "jasmineDone" ]);
reporter.specDone.and.callFake(function() {
expect(reporter.specDone).toHaveBeenCalledWith(jasmine.objectContaining({status: 'failed'}));
});
reporter.jasmineDone.and.callFake(function() {
expect(reporter.jasmineDone.calls.count()).toEqual(1);
done();
});
@@ -311,7 +315,7 @@ describe("Env integration", function() {
env.it("async spec that doesn't call done", function(underTestCallback) {
env.expect(true).toBeTruthy();
jasmine.getEnv().clock.tick(8415);
jasmine.getEnv().clock.tick(8416);
});
env.execute();