Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated tests to support the +1 second in timing. Also, updated code …

…to support the existing onComplete API.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
commit 33c68bd54067fcd3580f695bebaf26331ba37911 1 parent 7d4cfce
@ncb000gt authored
Showing with 24 additions and 16 deletions.
  1. +11 −8 lib/cron.js
  2. +6 −6 tests/test-cron.js
  3. +7 −2 tests/test-crontime.js
View
19 lib/cron.js
@@ -22,7 +22,9 @@ function CronTime(time) {
this.dayOfMonth = {};
this.month = {};
- this._parse();
+ if (!(this.source instanceof Date)) {
+ this._parse();
+ }
};
@@ -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);
@@ -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();
}
@@ -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);
}
},
@@ -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) {
View
12 tests/test-cron.js
@@ -9,7 +9,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
- }, 1000);
+ }, 2000);
},
'test second with oncomplete (* * * * * *)': function(assert) {
assert.expect(1);
@@ -20,7 +20,7 @@ module.exports = testCase({
});
setTimeout(function() {
assert.done();
- }, 1000);
+ }, 2000);
},
'test every second for 5 seconds (* * * * * *)': function(assert) {
assert.expect(5);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
View
9 tests/test-crontime.js
@@ -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();
}
-
-
});
Please sign in to comment.
Something went wrong with that request. Please try again.