Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Better delayed job test handling
Browse files Browse the repository at this point in the history
  • Loading branch information
scttnlsn committed Mar 23, 2014
1 parent 9d521be commit 04b5a71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.each = function (fixture, fn, done) {

exports.flushWorker = function (worker, done) {
worker.start();
worker.on('empty', function () {
worker.once('empty', function () {
worker.stop(done);
});
};
10 changes: 2 additions & 8 deletions test/test_priority.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ describe('Priority', function () {
beforeEach(function (done) {
worker = new Worker([queue], { interval: 1 });
worker.register({ priority: handler });
worker.start();

helpers.each(jobs, queue.enqueue.bind(queue), done);
});

beforeEach(function (done) {
worker.on('empty', function () {
worker.stop(done);
});
helpers.flushWorker(worker, done);
});

it('calls handler once for each job', function () {
Expand All @@ -53,15 +50,12 @@ describe('Priority', function () {
beforeEach(function (done) {
worker = new Worker([queue], { interval: 1, minPriority: 1 });
worker.register({ priority: handler });
worker.start();

helpers.each(jobs, queue.enqueue.bind(queue), done);
});

beforeEach(function (done) {
worker.on('empty', function () {
worker.stop(done);
});
helpers.flushWorker(worker, done);
});

it('calls handler once for each job with sufficient priority', function () {
Expand Down
38 changes: 30 additions & 8 deletions test/test_retries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var assert = require('assert');
var sinon = require('sinon');
var helpers = require('./helpers');
var Job = require('../lib/job');
var Queue = require('../lib/queue');
var Worker = require('../lib/worker');

Expand All @@ -16,7 +17,7 @@ describe('Retries', function () {

failed = sinon.spy();

worker = new Worker([queue], { interval: 1 });
worker = new Worker([queue], { interval: 10 });
worker.register({ retry: handler });
worker.on('failed', failed);
});
Expand Down Expand Up @@ -52,12 +53,15 @@ describe('Retries', function () {
});

describe('worker retrying job with delay', function () {
var start;

beforeEach(function (done) {
queue.enqueue('retry', {}, { attempts: { count: 3, delay: 10 } }, done);
queue.enqueue('retry', {}, { attempts: { count: 3, delay: 100 } }, done);
});

describe('after first attempt', function () {
beforeEach(function (done) {
start = new Date();
helpers.flushWorker(worker, done);
});

Expand All @@ -69,20 +73,38 @@ describe('Retries', function () {
assert.equal(failed.callCount, 1);
});

it('re-enqueues job', function () {
it('re-enqueues job with delay', function () {
var data = failed.lastCall.args[0];
assert.equal(data.status, 'queued');
assert.ok(new Date(data.delay).getTime() >= start.getTime() + 100);
});

it('does not immediately dequeue job', function (done) {
helpers.flushWorker(worker, function () {
assert.equal(handler.callCount, 1);
done();
});
});
});

describe('after all attempts', function () {
var delay;

beforeEach(function () {
delay = sinon.stub(Job.prototype, 'delay', function (delay, callback) {
assert.equal(delay, 100);

this.data.delay = new Date();
this.enqueue(callback);
});
});

beforeEach(function (done) {
worker.start();
helpers.flushWorker(worker, done);
});

// wait for all attempts
setTimeout(function () {
worker.stop(done);
}, 50);
afterEach(function () {
delay.restore();
});

it('calls the handler once for each retry', function () {
Expand Down

0 comments on commit 04b5a71

Please sign in to comment.