| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- var testCase = require('nodeunit').testCase,
- cron = require('../lib/cron');
- module.exports = testCase({
- 'test second (* * * * * *)': function(assert) {
- assert.expect(1);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- }, null, true);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 1250);
- },
- 'test second with oncomplete (* * * * * *)': function(assert) {
- assert.expect(2);
- var c = new cron.CronJob('* * * * * *', function(done) {
- assert.ok(true);
- }, function () {
- assert.ok(true);
- assert.done();
- }, true);
- setTimeout(function() {
- c.stop();
- }, 1250);
- },
- 'test every second for 5 seconds (* * * * * *)': function(assert) {
- assert.expect(5);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- }, null, true);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 5250);
- },
- 'test standard cron no-seconds syntax doesnt send on seconds (* * * * *)': function(assert) {
- assert.expect(0);
- // Delay test from running at minute boundary
- var prepDate = new Date();
- if (prepDate.getSeconds() >= 55) {
- setTimeout(testRun, 5000);
- } else {
- testRun();
- }
- function testRun() {
- var c = new cron.CronJob('* * * * *', function() {
- assert.ok(true);
- }, null, true);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 5250);
- }
- },
- 'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
- assert.expect(6);
- var c = new cron.CronJob('* * * * * *', function(done) {
- assert.ok(true);
- }, function() {
- assert.ok(true);
- assert.done();
- }, true);
- setTimeout(function() {
- c.stop();
- }, 5250);
- },
- 'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) {
- assert.expect(5);
- var c = new cron.CronJob('*/1 * * * * *', function() {
- assert.ok(true);
- }, null, true);
- setTimeout(function() {
- assert.done();
- c.stop();
- }, 5250);
- },
- 'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) {
- assert.expect(6);
- var c = new cron.CronJob('*/1 * * * * *', function(done) {
- assert.ok(true);
- }, function() {
- assert.ok(true);
- assert.done();
- }, true);
- setTimeout(function() {
- c.stop();
- }, 5250);
- },
- 'test every second for a range ([start]-[end] * * * * *)': function(assert) {
- assert.expect(5);
- var prepDate = new Date();
- if ((54 - prepDate.getSeconds()) <= 0) {
- setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
- } else {
- testRun();
- }
- function testRun() {
- var d = new Date();
- var s = d.getSeconds()+2;
- var e = s + 6; //end value is inclusive
- var c = new cron.CronJob(s + '-' + e +' * * * * *', function() {
- assert.ok(true);
- }, null, true);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 6250);
- }
- },
- 'test every second for a range with oncomplete ([start]-[end] * * * * *)': function(assert) {
- assert.expect(6);
- var prepDate = new Date();
- if ((54 - prepDate.getSeconds()) <= 0) {
- setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
- } else {
- testRun();
- }
- function testRun() {
- var d = new Date();
- var s = d.getSeconds()+2;
- var e = s + 6; //end value is inclusive
- var c = new cron.CronJob(s + '-' + e +' * * * * *', function() {
- assert.ok(true);
- }, function() {
- assert.ok(true);
- assert.done();
- }, true);
- setTimeout(function() {
- c.stop();
- }, 6250);
- }
- },
- 'test second (* * * * * *) object constructor': function(assert) {
- assert.expect(1);
- var c = new cron.CronJob({
- cronTime: '* * * * * *',
- onTick: function() {
- assert.ok(true);
- },
- start: true
- });
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 1250);
- },
- 'test second with oncomplete (* * * * * *) object constructor': function(assert) {
- assert.expect(2);
- var c = new cron.CronJob({
- cronTime: '* * * * * *',
- onTick: function(done) {
- assert.ok(true);
- },
- onComplete: function () {
- assert.ok(true);
- assert.done();
- },
- start: true
- });
- setTimeout(function() {
- c.stop();
- }, 1250);
- },
- 'test start/stop': function(assert) {
- assert.expect(1);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- this.stop();
- });
- c.start();
- setTimeout(function() {
- assert.done();
- }, 3250);
- },
- 'test specifying a specific date': function(assert) {
- assert.expect(2);
- var prepDate = new Date();
- if ((58 - prepDate.getSeconds()) <= 0) {
- setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
- } else {
- testRun();
- }
- function testRun() {
- var d = new Date();
- var s = d.getSeconds()+1;
- d.setSeconds(s);
- var c = new cron.CronJob(d, function() {
- var t = new Date();
- assert.equal(t.getSeconds(), d.getSeconds());
- assert.ok(true);
- }, null, true);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 2250);
- }
- },
- 'test specifying a specific date with oncomplete': function(assert) {
- assert.expect(3);
- var prepDate = new Date();
- if ((58 - prepDate.getSeconds()) <= 0) {
- setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
- } else {
- testRun();
- }
- function testRun() {
- var d = new Date();
- var s = d.getSeconds()+1;
- d.setSeconds(s);
- var c = new cron.CronJob(d, function() {
- var t = new Date();
- assert.equal(t.getSeconds(), d.getSeconds());
- assert.ok(true);
- }, function() {
- assert.ok(true);
- assert.done();
- }, true);
- setTimeout(function() {
- c.stop();
- }, 2250);
- }
- },
- 'test a job with a string and a given time zone': function (assert) {
- assert.expect(3);
- var time = require("time");
- var zone = "America/Chicago";
- // New Orleans time
- var t = new time.Date();
- t.setTimezone(zone);
- // Current time
- d = new Date();
- // If current time is New Orleans time, switch to Los Angeles..
- if (t.getHours() === d.getHours()) {
- zone = "America/Los_Angeles";
- t.setTimezone(zone);
- }
- assert.notEqual(d.getHours(), t.getHours());
- assert.ok(!(Date instanceof time.Date));
- // If t = 59s12m then t.setSeconds(60)
- // becones 00s13m so we're fine just doing
- // this and no testRun callback.
- t.setSeconds(t.getSeconds()+1);
- // 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 c = new cron.CronJob(t.getSeconds() + ' ' + t.getMinutes() + ' ' + t.getHours() + ' * * *', function(){
- assert.ok(true);
- }, undefined, true, zone);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 1250);
- },
- 'test a job with a date and a given time zone': function (assert) {
- assert.expect(3);
- var time = require("time");
- var zone = "America/Chicago";
- // New Orleans time
- var t = new time.Date();
- t.setTimezone(zone);
- // Current time
- d = new Date();
- // If current time is New Orleans time, switch to Los Angeles..
- if (t.getHours() === d.getHours()) {
- zone = "America/Los_Angeles";
- t.setTimezone(zone);
- }
- assert.notEqual(d.getHours(), t.getHours());
- assert.ok(!(Date instanceof time.Date));
- if ((58 - t.getSeconds()) <= 0) {
- setTimeout(testRun, (60000 - (t.getSeconds()*1000)) + 1000);
- } else {
- testRun();
- }
- function testRun() {
- var s = d.getSeconds()+1;
- d.setSeconds(s);
- var c = new cron.CronJob(d, function() {
- assert.ok(true);
- }, null, true, zone);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 2250);
- }
- },
- 'test dates fire only once': function(assert) {
- assert.expect(1);
- var count = 0;
- var d = new Date().getTime() + 1000;
- var job = cron.job(new Date(d), function() {
- count++;
- });
- job.start();
- setTimeout(function() {
- job.stop();
- assert.equal(count, 1);
- assert.done();
- }, 5250);
- },
- 'test long wait should not fire immediately': function(assert) {
- assert.expect(1);
- var count = 0;
- var d = new Date().getTime() + 31 * 86400 * 1000;
- var job = cron.job(new Date(d), function() {
- assert.ok(false);
- });
- job.start();
- setTimeout(function() {
- job.stop();
- assert.ok(true);
- assert.done();
- }, 250);
- },
- 'test start, change time, start again': function(assert) {
- assert.expect(3);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- });
- var time = cron.time('*/2 * * * * *');
- c.start();
- setTimeout(function() {
- c.stop();
- c.setTime(time);
- c.start();
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 4250);
- }, 1250);
- },
- 'test start, change time, excpetion': function(assert) {
- assert.expect(2);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- });
- var time = new Date();
- c.start();
- setTimeout(function() {
- c.stop();
- assert.throws(function() {
- c.setTime(time);
- });
- assert.done();
- }, 1250);
- },
- 'test cronjob scoping': function(assert) {
- assert.expect(2);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- assert.ok(c instanceof cron.CronJob);
- }, null, true);
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 1250);
- },
- 'test non-cronjob scoping': function(assert) {
- assert.expect(2);
- var c = new cron.CronJob('* * * * * *', function() {
- assert.ok(true);
- assert.equal(this.hello, 'world');
- }, null, true, null, {'hello':'world'});
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 1250);
- },
- 'test non-cronjob scoping inside object': function(assert) {
- assert.expect(2);
- var c = new cron.CronJob({
- cronTime: '* * * * * *',
- onTick: function() {
- assert.ok(true);
- assert.equal(this.hello, 'world');
- },
- start: true,
- context: {hello: 'world'}
- });
- setTimeout(function() {
- c.stop();
- assert.done();
- }, 1250);
- }
- });
|