Skip to content

Commit

Permalink
updated test suite with before() hooks to resolve timeout issues in C…
Browse files Browse the repository at this point in the history
…I builds
  • Loading branch information
timgit committed May 12, 2016
1 parent 7812594 commit aae700b
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 82 deletions.
41 changes: 24 additions & 17 deletions test/delayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,41 @@ var assert = require('chai').assert;
var helper = require('./testHelper');

describe('delayed jobs', function(){

var boss;

before(function(finished){
helper.start()
.then(dabauce => {
boss = dabauce;
finished();
});
});

it('should wait before processing a delayed job submission', function(finished) {

var delaySeconds = 2;
this.timeout(3000);

helper.start().then(boss => {
boss.subscribe('wait', function(job, done) {
var start = new Date(job.data.submitted);
var end = new Date();

boss.subscribe('wait', function(job, done) {
var start = new Date(job.data.submitted);
var end = new Date();
var elapsedSeconds = Math.floor((end-start)/1000);

var elapsedSeconds = Math.floor((end-start)/1000);
console.log('job '+ job.id + ' received in ' + elapsedSeconds + ' seconds with payload: ' + job.data.message);

console.log('job '+ job.id + ' received in ' + elapsedSeconds + ' seconds with payload: ' + job.data.message);

done().then(function() {
assert.isAtLeast(delaySeconds, elapsedSeconds);
finished();
});
done().then(function() {
assert.isAtLeast(delaySeconds, elapsedSeconds);
finished();
});

boss.publish('wait', {message: 'hold your horses', submitted: Date.now()}, {startIn: delaySeconds})
.then(function(jobId) {
console.log('job ' + jobId + ' requested to start in ' + delaySeconds + ' seconds');
});

});

boss.publish('wait', {message: 'hold your horses', submitted: Date.now()}, {startIn: delaySeconds})
.then(function(jobId) {
console.log('job ' + jobId + ' requested to start in ' + delaySeconds + ' seconds');
});

});
});

Expand Down
71 changes: 62 additions & 9 deletions test/publishTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,76 @@ var assert = require('chai').assert;
var helper = require('./testHelper');

describe('publish', function(){
it('should publish undefined for payload without error', function(finished) {

helper.start().then(boss => {
var boss;

var jobName = 'publishUndefined';
before(function(finished){
helper.start()
.then(dabauce => {
boss = dabauce;
finished();
});
});

it('should fail with no arguments', function() {
assert.throws(() => boss.publish());
});

it('should accept single string argument', function(finished) {
var jobName = 'publishNameOnly';

boss.subscribe(jobName, (job, done) => {
done().then(() => {
assert(true);
finished();
});
boss.subscribe(jobName, (job, done) => {
done().then(() => {
assert(true);
finished();
});
});

boss.publish(jobName);
boss.publish(jobName);
});


it('should accept job object argument with only name', function(finished){
var jobName = 'publishJobNameOnly';

boss.subscribe(jobName, (job, done) => {
done().then(() => {
assert(true);
finished();
});
});

boss.publish({name: jobName});
});


it('should accept job object with name and data only', function(finished){
var jobName = 'publishJobNameAndData';
var message = 'hi';

boss.subscribe(jobName, (job, done) => {
done().then(() => {
assert.equal(message, job.data.message);
finished();
});
});

boss.publish({name: jobName, data: {message}});
});


it('should accept job object with name and options only', function(finished){
var jobName = 'publishJobNameAndOptions';
var options = {someCrazyOption:'whatever'};

boss.subscribe(jobName, (job, done) => {
done().then(() => {
assert.isNull(job.data);
finished();
});
});

boss.publish({name: jobName, options});
});

});
Expand Down
44 changes: 26 additions & 18 deletions test/retryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@ var assert = require('chai').assert;
var helper = require('./testHelper');

describe('retries', function() {

var boss;

before(function(finished){
helper.start({expireCheckIntervalSeconds:1})
.then(dabauce => {
boss = dabauce;
finished();
});
});

it('should retry a job that didn\'t complete', function (finished) {

this.timeout(7000);

var expireIn = '1 second';
var retries = 2;
var subscribeCount = 0;

this.timeout(7000);

helper.start({expireCheckIntervalSeconds:1}).then(boss => {
var subscribeCount = 0;

boss.subscribe('unreliable', function(job, done) {
// not calling done so it will expire
subscribeCount++;
});
boss.subscribe('unreliable', function(job, done) {
// not calling done so it will expire
subscribeCount++;
});

boss.publish('unreliable', null, {
expireIn: expireIn,
retryLimit: retries
});
boss.publish('unreliable', null, {
expireIn: expireIn,
retryLimit: retries
});

setTimeout(function() {
assert.equal(subscribeCount, retries + 1);
finished();
setTimeout(function() {
assert.equal(subscribeCount, retries + 1);
finished();

}, 6000);
});
}, 6000);

});
});
51 changes: 29 additions & 22 deletions test/speedTest.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
var assert = require('chai').assert;
var helper = require('./testHelper');

var expectedSeconds = 5;
var jobCount = 1000;
describe('speed', function() {

describe('performance', function() {
it('should be able to complete ' + jobCount + ' jobs in ' + expectedSeconds + ' seconds', function (finished) {
var boss;

before(function(finished){
helper.start()
.then(dabauce => {
boss = dabauce;
finished();
});
});

var expectedSeconds = 5;
var jobCount = 1000;

it('should be able to complete ' + jobCount + ' jobs in ' + expectedSeconds + ' seconds', function (finished) {
// add an extra second to test timeout
this.timeout((expectedSeconds + 1) * 1000);

helper.start().then(boss => {

var receivedCount = 0;
var jobName = 'one_of_many';
var receivedCount = 0;
var jobName = 'one_of_many';

for (var x = 1; x <= jobCount; x++) {
boss.publish(jobName, {message: 'message #' + x});
}
for (var x = 1; x <= jobCount; x++) {
boss.publish(jobName, {message: 'message #' + x});
}

var startTime = new Date();
var startTime = new Date();

boss.subscribe(jobName, {teamSize: jobCount}, function (job, done) {
boss.subscribe(jobName, {teamSize: jobCount}, function (job, done) {

done().then(function () {
receivedCount++;
done().then(function () {
receivedCount++;

if (receivedCount === jobCount) {
var elapsed = new Date().getTime() - startTime.getTime();
console.log('finished ' + jobCount + ' jobs in ' + elapsed + 'ms');
if (receivedCount === jobCount) {
var elapsed = new Date().getTime() - startTime.getTime();
console.log('finished ' + jobCount + ' jobs in ' + elapsed + 'ms');

assert.isBelow(elapsed / 1000, expectedSeconds);
finished();
}
assert.isBelow(elapsed / 1000, expectedSeconds);
finished();
}

});
});
});
});
Expand Down
41 changes: 25 additions & 16 deletions test/throttleTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ var assert = require('chai').assert;
var helper = require('./testHelper');

describe('throttle', function() {

var boss;

before(function(finished){
helper.start()
.then(dabauce => {
boss = dabauce;
finished();
});
});

it('should process at most 1 job per second', function (finished) {

var singletonSeconds = 1;
Expand All @@ -12,28 +23,26 @@ describe('throttle', function() {
// add an extra second to test timeout
this.timeout((jobCount + 1) * 1000);

helper.start().then(boss => {
var publishCount = 0;
var subscribeCount = 0;
var publishCount = 0;
var subscribeCount = 0;

boss.subscribe('expensive', function(job, done) {
done().then(function() { subscribeCount++; });
});
boss.subscribe('expensive', function(job, done) {
done().then(function() { subscribeCount++; });
});

setTimeout(function() {
console.log('published ' + publishCount + ' jobs in ' + assertTimeout/1000 + ' seconds but received ' + subscribeCount + ' jobs');
assert.isAtMost(subscribeCount, jobCount + 1);
setTimeout(function() {
console.log('published ' + publishCount + ' jobs in ' + assertTimeout/1000 + ' seconds but received ' + subscribeCount + ' jobs');
assert.isAtMost(subscribeCount, jobCount + 1);

finished();
finished();

}, assertTimeout);
}, assertTimeout);


setInterval(function() {
boss.publish('expensive', null, {singletonSeconds: singletonSeconds})
.then(function() { publishCount++; });
}, publishInterval);
});
setInterval(function() {
boss.publish('expensive', null, {singletonSeconds: singletonSeconds})
.then(function() { publishCount++; });
}, publishInterval);

});
});

0 comments on commit aae700b

Please sign in to comment.