cron
Advanced tools
Comparing version 1.0.9 to 1.1.0
@@ -349,3 +349,4 @@ var exports, | ||
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context) { | ||
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) { | ||
var _cronTime = cronTime; | ||
if (typeof cronTime != "string" && arguments.length == 1) { | ||
@@ -356,5 +357,6 @@ //crontime is an object... | ||
context = cronTime.context; | ||
startNow = cronTime.start || cronTime.startNow; | ||
startNow = cronTime.start || cronTime.startNow || cronTime.startJob; | ||
timeZone = cronTime.timeZone; | ||
cronTime = cronTime.cronTime; | ||
runOnInit = cronTime.runOnInit; | ||
_cronTime = cronTime.cronTime; | ||
} | ||
@@ -365,6 +367,7 @@ | ||
this.onComplete = command2function(onComplete); | ||
this.cronTime = new CronTime(cronTime, timeZone); | ||
this.cronTime = new CronTime(_cronTime, timeZone); | ||
addCallback.call(this, command2function(onTick)); | ||
if (runOnInit) fireOnTick.call(this); | ||
if (startNow) start.call(this); | ||
@@ -381,3 +384,3 @@ | ||
CronJob.prototype.setTime = function(time) { | ||
if (!(time instanceof CronTime)) throw '\'time\' must be an instance of CronTime.'; | ||
if (!(time instanceof CronTime)) throw new Error('\'time\' must be an instance of CronTime.'); | ||
this.stop(); | ||
@@ -391,2 +394,8 @@ this.cronTime = time; | ||
var fireOnTick = function() { | ||
for (var i = (this._callbacks.length - 1); i >= 0; i--) | ||
this._callbacks[i].call(this.context, this.onComplete); | ||
} | ||
CronJob.prototype.fireOnTick = fireOnTick; | ||
var start = function() { | ||
@@ -431,4 +440,5 @@ if (this.running) return; | ||
for (var i = (self._callbacks.length - 1); i >= 0; i--) | ||
self._callbacks[i].call(self.context, self.onComplete); | ||
self.fireOnTick(); | ||
//for (var i = (self._callbacks.length - 1); i >= 0; i--) | ||
//self._callbacks[i].call(self.context, self.onComplete); | ||
} | ||
@@ -435,0 +445,0 @@ } |
{ | ||
"name": "cron", | ||
"description": "Cron jobs for your node", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)", | ||
@@ -21,3 +21,4 @@ "bugs" : { | ||
"devDependencies": { | ||
"nodeunit": ">=0.5.4", | ||
"mocha": "~2.3.3", | ||
"chai": "~3.4.0", | ||
"sinon": "~1.12.x" | ||
@@ -24,0 +25,0 @@ }, |
105
README.md
@@ -37,6 +37,8 @@ node-cron | ||
var CronJob = require('cron').CronJob; | ||
new CronJob('* * * * * *', function(){ | ||
console.log('You will see this message every second'); | ||
}, null, true, "America/Los_Angeles"); | ||
```javascript | ||
var CronJob = require('cron').CronJob; | ||
new CronJob('* * * * * *', function() { | ||
console.log('You will see this message every second'); | ||
}, null, true, 'America/Los_Angeles'); | ||
``` | ||
@@ -68,13 +70,17 @@ | ||
var CronJob = require('cron').CronJob; | ||
var job = new CronJob('00 30 11 * * 1-5', function(){ | ||
// Runs every weekday (Monday through Friday) | ||
// at 11:30:00 AM. It does not run on Saturday | ||
// or Sunday. | ||
}, function () { | ||
// This function is executed when the job stops | ||
}, | ||
true /* Start the job right now */, | ||
timeZone /* Time zone of this job. */ | ||
); | ||
```javascript | ||
var CronJob = require('cron').CronJob; | ||
var job = new CronJob('00 30 11 * * 1-5', function() { | ||
/* | ||
* Runs every weekday (Monday through Friday) | ||
* at 11:30:00 AM. It does not run on Saturday | ||
* or Sunday. | ||
*/ | ||
}, function () { | ||
/* This function is executed when the job stops */ | ||
}, | ||
true, /* Start the job right now */ | ||
timeZone /* Time zone of this job. */ | ||
); | ||
``` | ||
@@ -84,11 +90,13 @@ Another example with Date | ||
var CronJob = require('cron').CronJob; | ||
var job = new CronJob(new Date(), function(){ | ||
//runs once at the specified date. | ||
}, function () { | ||
// This function is executed when the job stops | ||
}, | ||
true /* Start the job right now */, | ||
timeZone /* Time zone of this job. */ | ||
); | ||
```javascript | ||
var CronJob = require('cron').CronJob; | ||
var job = new CronJob(new Date(), function() { | ||
/* runs once at the specified date. */ | ||
}, function () { | ||
/* This function is executed when the job stops */ | ||
}, | ||
true, /* Start the job right now */ | ||
timeZone /* Time zone of this job. */ | ||
); | ||
``` | ||
@@ -98,14 +106,18 @@ For good measure | ||
var CronJob = require('cron').CronJob; | ||
var job = new CronJob({ | ||
cronTime: '00 30 11 * * 1-5', | ||
onTick: function() { | ||
// Runs every weekday (Monday through Friday) | ||
// at 11:30:00 AM. It does not run on Saturday | ||
// or Sunday. | ||
}, | ||
start: false, | ||
timeZone: "America/Los_Angeles" | ||
}); | ||
job.start(); | ||
```javascript | ||
var CronJob = require('cron').CronJob; | ||
var job = new CronJob({ | ||
cronTime: '00 30 11 * * 1-5', | ||
onTick: function() { | ||
/* | ||
* Runs every weekday (Monday through Friday) | ||
* at 11:30:00 AM. It does not run on Saturday | ||
* or Sunday. | ||
*/ | ||
}, | ||
start: false, | ||
timeZone: 'America/Los_Angeles' | ||
}); | ||
job.start(); | ||
``` | ||
@@ -116,9 +128,11 @@ | ||
try { | ||
new CronJob('invalid cron pattern', function() { | ||
console.log('this should not be printed'); | ||
}) | ||
} catch(ex) { | ||
console.log("cron pattern not valid"); | ||
} | ||
```javascript | ||
try { | ||
new CronJob('invalid cron pattern', function() { | ||
console.log('this should not be printed'); | ||
}) | ||
} catch(ex) { | ||
console.log("cron pattern not valid"); | ||
} | ||
``` | ||
@@ -140,9 +154,10 @@ | ||
* `constructor(cronTime, onTick, onComplete, start, timezone, context)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above). | ||
* `constructor(cronTime, onTick, onComplete, start, timezone, context, runOnInit)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above). | ||
* `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object. | ||
* `onTick` - [REQUIRED] - The function to fire at the specified time. | ||
* `onComplete` - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped. | ||
* `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). | ||
* `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs. | ||
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. | ||
* `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object. | ||
* `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisit initialization has happened. This option is set to `false` by default for backwards compatability. | ||
* `start` - Runs your job. | ||
@@ -149,0 +164,0 @@ * `stop` - Stops your job. |
18
test.js
@@ -36,5 +36,15 @@ var CronJob = require('./lib/cron').CronJob; | ||
//console.log(CronJob.prototype); | ||
// | ||
new CronJob('* * * * * *', function() { | ||
console.log('tick: ' + Date.now()); | ||
}, null, true); | ||
//new CronJob('* * * * * *', function() { | ||
//console.log('tick: ' + Date.now()); | ||
//}, null, true, null, null, true); | ||
new CronJob({ | ||
cronTime: '0 0 17 1 * *', | ||
onTick: function() { | ||
console.log('tick: ' + Date.now()); | ||
}, | ||
start: true | ||
}); | ||
console.log('do stuff'); |
@@ -1,38 +0,44 @@ | ||
var testCase = require('nodeunit').testCase, | ||
sinon = require('sinon'), | ||
cron = require('../lib/cron'); | ||
var chai = require('chai'), | ||
expect = chai.expect, | ||
sinon = require('sinon'), | ||
cron = require('../lib/cron'); | ||
module.exports = testCase({ | ||
'test second (* * * * * *)': function(assert) { | ||
describe('cron', function() { | ||
it('should run every second (* * * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(1); | ||
var c = 0; | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
assert.ok(true); | ||
c++; | ||
}, null, true); | ||
clock.tick(1000); | ||
job.stop(); | ||
clock.restore(); | ||
clock.restore(); | ||
assert.done(); | ||
}, | ||
'test second with oncomplete (* * * * * *)': function(assert) { | ||
expect(c).to.eql(1); | ||
}); | ||
it('should run second with oncomplete (* * * * * *)', function(done) { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(2); | ||
var job = new cron.CronJob('* * * * * *', function(done) { | ||
assert.ok(true); | ||
var c = 0; | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
c++; | ||
}, function () { | ||
assert.ok(true); | ||
assert.done(); | ||
expect(c).to.eql(1); | ||
done(); | ||
}, true); | ||
clock.tick(1000); | ||
clock.restore(); | ||
job.stop(); | ||
}, | ||
'test standard cron no-seconds syntax doesnt send on seconds (* * * * *)': function(assert) { | ||
}); | ||
it('should use standard cron no-seconds syntax (* * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(1); | ||
var c = 0; | ||
var job = new cron.CronJob('* * * * *', function() { | ||
assert.ok(true); | ||
c++; | ||
}, null, true); | ||
@@ -46,10 +52,12 @@ | ||
clock.restore(); | ||
assert.done(); | ||
}, | ||
'test every second for 5 seconds (* * * * * *)': function(assert) { | ||
expect(c).to.eql(1); | ||
}); | ||
it('should run every second for 5 seconds (* * * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(5); | ||
var c = 0; | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
assert.ok(true); | ||
c++; | ||
}, null, true); | ||
@@ -62,12 +70,15 @@ | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) { | ||
expect(c).to.eql(5); | ||
}); | ||
it('should run every second for 5 seconds with oncomplete (* * * * * *)', function(done) { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(6); | ||
var job = new cron.CronJob('* * * * * *', function(done) { | ||
assert.ok(true); | ||
var c = 0; | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
c++; | ||
}, function() { | ||
assert.ok(true); | ||
assert.done(); | ||
expect(c).to.eql(5); | ||
done(); | ||
}, true); | ||
@@ -80,9 +91,10 @@ | ||
job.stop(); | ||
}, | ||
'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) { | ||
}); | ||
it('should run every second for 5 seconds (*/1 * * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(5); | ||
var c = 0; | ||
var job = new cron.CronJob('*/1 * * * * *', function() { | ||
assert.ok(true); | ||
c++; | ||
}, null, true); | ||
@@ -95,28 +107,27 @@ | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) { | ||
expect(c).to.eql(5); | ||
}); | ||
//ensure that this is running on the second second | ||
it('should run every 2 seconds for 1 seconds (*/2 * * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(6); | ||
var job = new cron.CronJob('*/1 * * * * *', function(done) { | ||
assert.ok(true); | ||
}, function() { | ||
assert.ok(true); | ||
assert.done(); | ||
}, true); | ||
var job = new cron.CronJob('*/2 * * * * *', function() { | ||
c++; | ||
}, null, true); | ||
for (var i = 0; i < 5; i++) | ||
clock.tick(1000); | ||
clock.tick(1000); | ||
clock.restore(); | ||
job.stop(); | ||
}, | ||
'test every second for a range ([start]-[end] * * * * *)': function(assert) { | ||
expect(c).to.eql(0); | ||
}); | ||
it('should run every 2 seconds for 5 seconds (*/2 * * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(5); | ||
var job = new cron.CronJob('0-5 * * * * *', function() { | ||
assert.ok(true); | ||
var job = new cron.CronJob('*/2 * * * * *', function() { | ||
c++; | ||
}, null, true); | ||
@@ -129,14 +140,14 @@ | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test every second for a range with oncomplete ([start]-[end] * * * * *)': function(assert) { | ||
expect(c).to.eql(2); | ||
}); | ||
it('should run every second for 5 seconds with oncomplete (*/1 * * * * *)', function(done) { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(6); | ||
var job = new cron.CronJob('0-5 * * * * *', function() { | ||
assert.ok(true); | ||
var job = new cron.CronJob('*/1 * * * * *', function() { | ||
c++ | ||
}, function() { | ||
assert.ok(true); | ||
assert.done(); | ||
expect(c).to.eql(5); | ||
done(); | ||
}, true); | ||
@@ -149,12 +160,44 @@ | ||
job.stop(); | ||
}, | ||
'test second (* * * * * *) object constructor': function(assert) { | ||
}); | ||
it('should run every second for a range ([start]-[end] * * * * *)', function() { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(1); | ||
var job = new cron.CronJob('0-8 * * * * *', function() { | ||
c++; | ||
}, null, true); | ||
clock.tick(10000); //run for 10 seconds | ||
clock.restore(); | ||
job.stop(); | ||
expect(c).to.eql(8); | ||
}); | ||
it('should run every second for a range with oncomplete ([start]-[end] * * * * *)', function(done) { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
var job = new cron.CronJob('0-8 * * * * *', function() { | ||
c++; | ||
}, function() { | ||
expect(c).to.eql(8); | ||
done(); | ||
}, true); | ||
clock.tick(10000); | ||
clock.restore(); | ||
job.stop(); | ||
}); | ||
it('should run every second (* * * * * *) using the object constructor', function() { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
var job = new cron.CronJob({ | ||
cronTime: '* * * * * *', | ||
onTick: function() { | ||
assert.ok(true); | ||
c++; | ||
}, | ||
@@ -168,17 +211,17 @@ start: true | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test second with oncomplete (* * * * * *) object constructor': function(assert) { | ||
expect(c).to.eql(1); | ||
}); | ||
it('should run every second with oncomplete (* * * * * *) using the object constructor', function(done) { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(2); | ||
var job = new cron.CronJob({ | ||
cronTime: '* * * * * *', | ||
onTick: function(done) { | ||
assert.ok(true); | ||
c++; | ||
}, | ||
onComplete: function () { | ||
assert.ok(true); | ||
assert.done(); | ||
expect(c).to.eql(1); | ||
done(); | ||
}, | ||
@@ -192,13 +235,16 @@ start: true | ||
job.stop(); | ||
}, | ||
'test start/stop': function(assert) { | ||
}); | ||
it('should start and stop job', function(done) { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(1); | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
c++; | ||
this.stop(); | ||
}, function() { | ||
expect(c).to.eql(1); | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
clock.restore(); | ||
assert.ok(true); | ||
this.stop(); | ||
assert.done(); | ||
done(); | ||
}); | ||
@@ -208,6 +254,6 @@ job.start(); | ||
clock.tick(1000); | ||
}, | ||
'test specifying a specific date': function(assert) { | ||
assert.expect(2); | ||
}); | ||
it('should run on a specific date', function() { | ||
var c = 0; | ||
var d = new Date(); | ||
@@ -217,6 +263,7 @@ var clock = sinon.useFakeTimers(d.getTime()); | ||
d.setSeconds(s); | ||
var job = new cron.CronJob(d, function() { | ||
var t = new Date(); | ||
assert.equal(t.getSeconds(), d.getSeconds()); | ||
assert.ok(true); | ||
expect(t.getSeconds()).to.eql(d.getSeconds()); | ||
c++; | ||
}, null, true); | ||
@@ -227,7 +274,7 @@ clock.tick(1000); | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test specifying a specific date with oncomplete': function(assert) { | ||
assert.expect(3); | ||
expect(c).to.eql(1); | ||
}); | ||
it('should run on a specific date with oncomplete', function(done) { | ||
var c = 0; | ||
var d = new Date(); | ||
@@ -237,9 +284,10 @@ var clock = sinon.useFakeTimers(d.getTime()); | ||
d.setSeconds(s); | ||
var job = new cron.CronJob(d, function() { | ||
var t = new Date(); | ||
assert.equal(t.getSeconds(), d.getSeconds()); | ||
assert.ok(true); | ||
expect(t.getSeconds()).to.eql(d.getSeconds()); | ||
c++; | ||
}, function() { | ||
assert.ok(true); | ||
assert.done(); | ||
expect(c).to.eql(1); | ||
done(); | ||
}, true); | ||
@@ -250,82 +298,86 @@ clock.tick(1000); | ||
job.stop(); | ||
}, | ||
'test a job with a string and a given time zone': function (assert) { | ||
var clock = sinon.useFakeTimers(); | ||
}); | ||
assert.expect(2); | ||
describe('with timezone', function() { | ||
it('should run a job using cron syntax', function () { | ||
var clock = sinon.useFakeTimers(); | ||
var moment = require("moment-timezone"); | ||
var zone = "America/Chicago"; | ||
var c = 0; | ||
// New Orleans time | ||
var t = moment(); | ||
t.tz(zone); | ||
var moment = require("moment-timezone"); | ||
var zone = "America/Chicago"; | ||
// Current time | ||
d = moment(); | ||
// If current time is New Orleans time, switch to Los Angeles.. | ||
if (t.hours() === d.hours()) { | ||
zone = "America/Los_Angeles"; | ||
// New Orleans time | ||
var t = moment(); | ||
t.tz(zone); | ||
} | ||
assert.notEqual(d.hours(), t.hours()); | ||
// If t = 59s12m then t.setSeconds(60) | ||
// becones 00s13m so we're fine just doing | ||
// this and no testRun callback. | ||
t.add(1, 's'); | ||
// Run a job designed to be executed at a given | ||
// time in `zone`, making sure that it is a different | ||
// hour than local time. | ||
var job = new cron.CronJob(t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *', function(){ | ||
assert.ok(true); | ||
}, null, true, zone); | ||
// Current time | ||
d = moment(); | ||
clock.tick(1000); | ||
clock.restore(); | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test a job with a date and a given time zone': function (assert) { | ||
assert.expect(2); | ||
// If current time is New Orleans time, switch to Los Angeles.. | ||
if (t.hours() === d.hours()) { | ||
zone = "America/Los_Angeles"; | ||
t.tz(zone); | ||
} | ||
expect(d.hours()).to.not.eql(t.hours()); | ||
var moment = require("moment-timezone"); | ||
var zone = "America/Chicago"; | ||
// If t = 59s12m then t.setSeconds(60) | ||
// becones 00s13m so we're fine just doing | ||
// this and no testRun callback. | ||
t.add(1, 's'); | ||
// Run a job designed to be executed at a given | ||
// time in `zone`, making sure that it is a different | ||
// hour than local time. | ||
var job = new cron.CronJob(t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *', function(){ | ||
c++; | ||
}, null, true, zone); | ||
// New Orleans time | ||
var t = moment(); | ||
t.tz(zone); | ||
clock.tick(1000); | ||
clock.restore(); | ||
job.stop(); | ||
expect(c).to.eql(1); | ||
}); | ||
// Current time | ||
d = moment(); | ||
it('should run a job using a date', function () { | ||
var c = 0; | ||
// If current time is New Orleans time, switch to Los Angeles.. | ||
if (t.hours() === d.hours()) { | ||
zone = "America/Los_Angeles"; | ||
var moment = require("moment-timezone"); | ||
var zone = "America/Chicago"; | ||
// New Orleans time | ||
var t = moment(); | ||
t.tz(zone); | ||
} | ||
assert.notEqual(d.hours(), t.hours()); | ||
d.add(1, 's'); | ||
var clock = sinon.useFakeTimers(d._d.getTime()); | ||
// Current time | ||
d = moment(); | ||
var job = new cron.CronJob(d._d, function() { | ||
assert.ok(true); | ||
}, null, true, zone); | ||
// If current time is New Orleans time, switch to Los Angeles.. | ||
if (t.hours() === d.hours()) { | ||
zone = "America/Los_Angeles"; | ||
t.tz(zone); | ||
} | ||
expect(d.hours()).to.not.eql(t.hours()); | ||
clock.tick(1000); | ||
clock.restore(); | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test long wait should not fire immediately': function(assert) { | ||
d.add(1, 's'); | ||
var clock = sinon.useFakeTimers(d._d.getTime()); | ||
var job = new cron.CronJob(d._d, function() { | ||
c++; | ||
}, null, true, zone); | ||
clock.tick(1000); | ||
clock.restore(); | ||
job.stop(); | ||
expect(c).to.eql(1); | ||
}); | ||
}); | ||
it('should wait and not fire immediately', function() { | ||
var clock = sinon.useFakeTimers(); | ||
var c = 0; | ||
assert.expect(0); | ||
var d = new Date().getTime() + 31 * 86400 * 1000; | ||
var job = cron.job(new Date(d), function() { | ||
assert.ok(false); | ||
c++; | ||
}); | ||
@@ -338,19 +390,18 @@ job.start(); | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test start, change time, start again': function(assert) { | ||
expect(c).to.eql(0); | ||
}); | ||
it('should start, change time, start again', function() { | ||
var c = 0; | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(3); | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
assert.ok(true); | ||
c++; | ||
}); | ||
var time = cron.time('*/2 * * * * *'); | ||
job.start(); | ||
clock.tick(1000); | ||
job.stop(); | ||
var time = cron.time('*/2 * * * * *'); | ||
job.setTime(time); | ||
@@ -363,11 +414,11 @@ job.start(); | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test start, change time, exception': function(assert) { | ||
expect(c).to.eql(3); | ||
}); | ||
it('should start, change time, exception', function() { | ||
var c = 0; | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(2); | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
assert.ok(true); | ||
c++; | ||
}); | ||
@@ -381,19 +432,17 @@ | ||
job.stop(); | ||
assert.throws(function() { | ||
expect(function() { | ||
job.setTime(time); | ||
}); | ||
}).to.throw; | ||
clock.restore(); | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test cronjob scoping': function(assert) { | ||
expect(c).to.eql(1); | ||
}); | ||
it('should scope onTick to running job', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(3); | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
assert.ok(true); | ||
assert.ok(job instanceof cron.CronJob); | ||
assert.ok(job === this); | ||
expect(job).to.be.instanceOf(cron.CronJob); | ||
expect(job).to.eql(this); | ||
}, null, true); | ||
@@ -405,13 +454,10 @@ | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test non-cronjob scoping': function(assert) { | ||
}); | ||
it('should scope onTick to object', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(3); | ||
var job = new cron.CronJob('* * * * * *', function() { | ||
assert.ok(true); | ||
assert.equal(this.hello, 'world'); | ||
assert.ok(job !== this); | ||
expect(this.hello).to.eql('world'); | ||
expect(job).to.not.eql(this); | ||
}, null, true, null, {'hello':'world'}); | ||
@@ -423,15 +469,12 @@ | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test non-cronjob scoping inside object': function(assert) { | ||
}); | ||
it('should scope onTick to object within contstructor object', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(3); | ||
var job = new cron.CronJob({ | ||
cronTime: '* * * * * *', | ||
onTick: function() { | ||
assert.ok(true); | ||
assert.equal(this.hello, 'world'); | ||
assert.ok(job !== this); | ||
expect(this.hello).to.eql('world'); | ||
expect(job).to.not.eql(this); | ||
}, | ||
@@ -446,9 +489,7 @@ start: true, | ||
job.stop(); | ||
assert.done(); | ||
}, | ||
'test avoid inf loop on invalid time': function(assert) { | ||
}); | ||
it('should not get into an infinite loop on invalid times', function() { | ||
var clock = sinon.useFakeTimers(); | ||
assert.expect(1); | ||
var invalid1 = new cron.CronJob('* 60 * * * *', function() { | ||
@@ -464,3 +505,2 @@ assert.ok(true); | ||
// assert that it gets here | ||
assert.ok(true); | ||
invalid1.stop(); | ||
@@ -470,4 +510,30 @@ invalid2.stop(); | ||
clock.restore(); | ||
assert.done(); | ||
} | ||
}); | ||
it('should test start of month', function() { | ||
var c = 0; | ||
var d = new Date('12/31/2014'); | ||
d.setSeconds(59); | ||
d.setMinutes(59); | ||
d.setHours(23); | ||
var clock = sinon.useFakeTimers(d.getTime()); | ||
var job = new cron.CronJob('0 0 0 1 * *', function() { | ||
c++; | ||
}, null, true); | ||
clock.tick(1001); | ||
expect(c).to.eql(1); | ||
clock.tick(2678399001); | ||
expect(c).to.eql(1); | ||
clock.tick(2678400001); //jump over 2 firsts | ||
clock.restore(); | ||
job.stop(); | ||
expect(c).to.eql(3); | ||
}); | ||
it('should run every second monday'); | ||
}); |
@@ -1,224 +0,203 @@ | ||
var testCase = require('nodeunit').testCase, | ||
cron = require('../lib/cron'); | ||
var chai = require('chai'), | ||
expect = chai.expect, | ||
cron = require('../lib/cron'); | ||
module.exports = testCase({ | ||
'test stars (* * * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('* * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test digit (0 * * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0 * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test multi digits (08 * * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('08 * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test all digits (08 8 8 8 8 5)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('08 * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test too many digits (08 8 8 8 8 5)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('08 * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test no second digit doesnt throw, i.e. standard cron format (* * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('* * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test no second digit defaults to 0, i.e. standard cron format (8 8 8 8 5)': function(assert) { | ||
assert.expect(6); | ||
var now = Date.now(); | ||
var standard = new cron.CronTime('8 8 8 8 5'); | ||
var extended = new cron.CronTime('0 8 8 8 8 5'); | ||
describe('crontime', function() { | ||
it('should test stars (* * * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
assert.deepEqual(standard.dayOfWeek, extended.dayOfWeek); | ||
assert.deepEqual(standard.month, extended.month); | ||
assert.deepEqual(standard.dayOfMonth, extended.dayOfMonth); | ||
assert.deepEqual(standard.hour, extended.hour); | ||
assert.deepEqual(standard.minute, extended.minute); | ||
assert.deepEqual(standard.second, extended.second); | ||
it('should test digit (0 * * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('0 * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
assert.done(); | ||
}, | ||
'test hyphen (0-10 * * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0-10 * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test multi hyphens (0-10 0-10 * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0-10 0-10 * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test all hyphens (0-10 0-10 0-10 0-10 0-10 0-1)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0-10 0-10 0-10 0-10 0-10 0-1'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test comma (0,10 * * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0,10 * * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test multi commas (0,10 0,10 * * * *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0,10 0,10 * * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test all commas (0,10 0,10 0,10 0,10 0,10 0,1)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('0,10 0,10 0,10 0,10 0,10 0,1'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test alias (* * * * jan *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('* * * * jan *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test multi aliases (* * * * jan,feb *)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('* * * * jan,feb *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test all aliases (* * * * jan,feb mon,tue)': function(assert) { | ||
assert.expect(1); | ||
assert.doesNotThrow(function() { | ||
new cron.CronTime('* * * * jan,feb mon,tue'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test unknown alias (* * * * jar *)': function(assert) { | ||
assert.expect(1); | ||
assert.throws(function() { | ||
new cron.CronTime('* * * * jar *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test unknown alias - short (* * * * j *)': function(assert) { | ||
assert.expect(1); | ||
assert.throws(function() { | ||
new cron.CronTime('* * * * j *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test Date': function(assert) { | ||
assert.expect(1); | ||
var d = new Date(); | ||
var ct = new cron.CronTime(d); | ||
assert.ok(ct.source.isSame(d.getTime())); | ||
assert.done(); | ||
}, | ||
'test day roll-over': function(assert) { | ||
var numHours = 24; | ||
assert.expect(numHours * 2); | ||
var ct = new cron.CronTime('0 0 17 * * *'); | ||
for (var hr = 0; hr < numHours; hr++) { | ||
var start = new Date(2012, 3, 16, hr, 30, 30); | ||
var next = ct._getNextDateFrom(start); | ||
assert.ok(next - start < 24*60*60*1000); | ||
assert.ok(next > start); | ||
} | ||
assert.done(); | ||
}, | ||
'test illegal repetition syntax': function(assert){ | ||
assert.throws(function(){ | ||
new cron.CronTime('* * /4 * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test next date': function(assert) { | ||
assert.expect(2); | ||
var ct = new cron.CronTime('0 0 */4 * * *'); | ||
it('should test multi digits (08 * * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('08 * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
var nextDate = new Date(); | ||
nextDate.setHours(23); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
assert.ok(nextdt > nextDate); | ||
assert.ok(nextdt.hours() % 4 === 0); | ||
assert.done(); | ||
}, | ||
'test next date from invalid date': function(assert) { | ||
assert.expect(1); | ||
var ct = new cron.CronTime('0 0 * * * *'); | ||
var nextDate = new Date('My invalid date string'); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
assert.equal(nextdt.toString(), 'Invalid date'); | ||
assert.done(); | ||
}, | ||
'test next real date': function(assert) { | ||
assert.expect(2); | ||
var ct = new cron.CronTime(new Date()); | ||
it('should test all digits (08 8 8 8 8 5)', function() { | ||
expect(function() { | ||
new cron.CronTime('08 * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
var nextDate = new Date(); | ||
nextDate.setMonth(nextDate.getMonth()+1); | ||
assert.ok(nextDate > ct.source); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
assert.ok(nextdt.isSame(nextDate)); | ||
assert.done(); | ||
}, | ||
'test < constraints day of month': function(assert) { | ||
assert.expect(5); | ||
it('should test too many digits (08 8 8 8 8 5)', function() { | ||
expect(function() { | ||
new cron.CronTime('08 * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
var ltm = [1, 3, 5, 8, 10]; | ||
for (var i = 0; i < ltm.length; i++) { | ||
(function(m) { | ||
assert.throws(function() { | ||
var ct = new cron.CronTime('0 0 0 33 ' + m + ' *'); | ||
}); | ||
})(ltm[i]); | ||
} | ||
it('should test standard cron format (* * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * *'); | ||
}).to.not.throw; | ||
}); | ||
assert.done(); | ||
}, | ||
'test next month selection': function(assert) { | ||
assert.expect(1); | ||
var date = new Date(); | ||
var dom = date.getDate() + 1; | ||
var ct = new cron.CronTime('0 0 0 ' + dom + ' * *'); | ||
it('should test standard cron format (8 8 8 8 5)', function() { | ||
var now = Date.now(); | ||
var standard = new cron.CronTime('8 8 8 8 5'); | ||
var extended = new cron.CronTime('0 8 8 8 8 5'); | ||
var saDate = ct.sendAt(); | ||
expect(standard.dayOfWeek).to.deep.eql(extended.dayOfWeek); | ||
expect(standard.month).to.deep.eql(extended.month); | ||
expect(standard.dayOfMonth).to.deep.eql(extended.dayOfMonth); | ||
expect(standard.hour).to.deep.eql(extended.hour); | ||
expect(standard.minute).to.deep.eql(extended.minute); | ||
expect(standard.second).to.deep.eql(extended.second); | ||
}); | ||
if (dom < date.getDate()) | ||
date.setMonth(date.getMonth()+1); | ||
it('should test hyphen (0-10 * * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('0-10 * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
assert.equal(date.getMonth(), saDate.month()); | ||
it('should test multi hyphens (0-10 0-10 * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('0-10 0-10 * * * *'); | ||
}).to.not.throw; | ||
}); | ||
assert.done() | ||
} | ||
it('should test all hyphens (0-10 0-10 0-10 0-10 0-10 0-1)', function() { | ||
expect(function() { | ||
new cron.CronTime('0-10 0-10 0-10 0-10 0-10 0-1'); | ||
}).to.not.throw; | ||
}); | ||
it('should test comma (0,10 * * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('0,10 * * * * *'); | ||
}).to.not.throw; | ||
}); | ||
it('should test multi commas (0,10 0,10 * * * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('0,10 0,10 * * * *'); | ||
}).to.not.throw; | ||
}); | ||
it('should test all commas (0,10 0,10 0,10 0,10 0,10 0,1)', function() { | ||
expect(function() { | ||
new cron.CronTime('0,10 0,10 0,10 0,10 0,10 0,1'); | ||
}).to.not.throw; | ||
}); | ||
it('should test alias (* * * * jan *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * jan *'); | ||
}).to.not.throw; | ||
}); | ||
it('should test multi aliases (* * * * jan,feb *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * jan,feb *'); | ||
}).to.not.throw; | ||
}); | ||
it('should test all aliases (* * * * jan,feb mon,tue)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * jan,feb mon,tue'); | ||
}).to.not.throw; | ||
}); | ||
it('should test every second monday (* * * * * mon/2)'); | ||
it('should test unknown alias (* * * * jar *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * jar *'); | ||
}).to.throw; | ||
}); | ||
it('should test unknown alias - short (* * * * j *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * j *'); | ||
}).to.throw; | ||
}); | ||
it('should test Date', function() { | ||
var d = new Date(); | ||
var ct = new cron.CronTime(d); | ||
expect(ct.source.isSame(d.getTime())).to.be.true | ||
}); | ||
it('should test day roll-over', function() { | ||
var numHours = 24; | ||
var ct = new cron.CronTime('0 0 17 * * *'); | ||
for (var hr = 0; hr < numHours; hr++) { | ||
var start = new Date(2012, 3, 16, hr, 30, 30); | ||
var next = ct._getNextDateFrom(start); | ||
expect(next - start).to.be.lt(24*60*60*1000); | ||
expect(next).to.be.gt(start); | ||
} | ||
}); | ||
it('should test illegal repetition syntax', function() { | ||
expect(function(){ | ||
new cron.CronTime('* * /4 * * *'); | ||
}).to.throw; | ||
}); | ||
it('should test next date', function() { | ||
var ct = new cron.CronTime('0 0 */4 * * *'); | ||
var nextDate = new Date(); | ||
nextDate.setHours(23); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
expect(nextdt).to.be.gt(nextDate); | ||
expect(nextdt.hours() % 4).to.eql(0); | ||
}); | ||
it('should test next date from invalid date', function() { | ||
var ct = new cron.CronTime('0 0 * * * *'); | ||
var nextDate = new Date('My invalid date string'); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
expect(nextdt.toString()).to.eql('Invalid date'); | ||
}); | ||
it('should test next real date', function() { | ||
var ct = new cron.CronTime(new Date()); | ||
var nextDate = new Date(); | ||
nextDate.setMonth(nextDate.getMonth()+1); | ||
expect(nextDate).to.be.gt(ct.source); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
expect(nextdt.isSame(nextDate)).to.be.true; | ||
}); | ||
it('should test < constraints day of month', function() { | ||
var ltm = [1, 3, 5, 8, 10]; | ||
for (var i = 0; i < ltm.length; i++) { | ||
(function(m) { | ||
expect(function() { | ||
var ct = new cron.CronTime('0 0 0 33 ' + m + ' *'); | ||
}).to.throw; | ||
})(ltm[i]); | ||
} | ||
}); | ||
it('should test next month selection'); | ||
describe('should throw an exception because `L` not supported', function() { | ||
it('(* * * L * *)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * L * *'); | ||
}).to.throw(Error); | ||
}); | ||
it('(* * * * * L)', function() { | ||
expect(function() { | ||
new cron.CronTime('* * * * * L'); | ||
}).to.throw(Error); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
69956
13
1175
191
3