Skip to content

Commit

Permalink
Updated tests to support the +1 second in timing. Also, updated code …
Browse files Browse the repository at this point in the history
…to support the existing onComplete API.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed Sep 25, 2011
1 parent 7d4cfce commit 33c68bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
19 changes: 11 additions & 8 deletions lib/cron.js
Expand Up @@ -22,7 +22,9 @@ function CronTime(time) {
this.dayOfMonth = {};
this.month = {};

this._parse();
if (!(this.source instanceof Date)) {
this._parse();
}

};

Expand All @@ -40,9 +42,9 @@ CronTime.prototype = {
* calculates the next send time
*/

sendAt: function(start) {
sendAt: function() {

var date = start ? start : new Date();
var date = (this.source instanceof Date) ? this.source : new Date();

//add 1 second so next time isn't now (can cause timeout to be 0)
date.setSeconds(date.getSeconds() + 1);
Expand Down Expand Up @@ -240,12 +242,13 @@ CronTime.prototype = {



function CronJob(cronTime, onComplete) {
function CronJob(cronTime, onTick, onComplete) {

this._callbacks = [];
this.onComplete = onComplete;
this.cronTime = new CronTime(cronTime);

this.addCallback(onComplete);
this.addCallback(onTick);

this.start();
}
Expand All @@ -270,7 +273,7 @@ CronJob.prototype = {
for (var i = this._callbacks.length; i--;) {

//send this so the callback can call this.stop();
this._callbacks[i].call(this);
this._callbacks[i].call(this, this.onComplete);
}
},

Expand Down Expand Up @@ -311,9 +314,9 @@ CronJob.prototype = {
};


exports.job = function(cronTime, onComplete) {
exports.job = function(cronTime, onTick, onComplete) {

return new CronJob(cronTime, onComplete);
return new CronJob(cronTime, onTick, onComplete);
}

exports.time = function(cronTime) {
Expand Down
12 changes: 6 additions & 6 deletions tests/test-cron.js
Expand Up @@ -9,7 +9,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
}, 1000);
}, 2000);
},
'test second with oncomplete (* * * * * *)': function(assert) {
assert.expect(1);
Expand All @@ -20,7 +20,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
}, 1000);
}, 2000);
},
'test every second for 5 seconds (* * * * * *)': function(assert) {
assert.expect(5);
Expand All @@ -29,7 +29,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
}, 5000);
}, 6000);
},
'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
assert.expect(5);
Expand All @@ -40,7 +40,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
}, 5000);
}, 6000);
},
'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) {
assert.expect(5);
Expand All @@ -49,7 +49,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
}, 5000);
}, 6000);
},
'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) {
assert.expect(5);
Expand All @@ -60,7 +60,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
}, 5000);
}, 6000);
},
'test every second for a range ([start]-[end] * * * * *)': function(assert) {
assert.expect(5);
Expand Down
9 changes: 7 additions & 2 deletions tests/test-crontime.js
Expand Up @@ -113,7 +113,12 @@ module.exports = testCase({
new cron.CronTime('* * * * j *');
});
assert.done();
},
'test Date': function(assert) {
assert.expect(1);
var d = new Date();
var ct = new cron.CronTime(d);
assert.equals(ct.source.getTime(), d.getTime());
assert.done();
}


});

0 comments on commit 33c68bd

Please sign in to comment.