123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- 'use strict';
- var sinon = require('sinon');
- var main = require('../package.json').main;
- var schedule = require('../' + main);
- var clock;
- module.exports = {
- setUp: function(cb) {
- clock = sinon.useFakeTimers();
- cb();
- },
- ".scheduleJob": {
- "Returns Job instance": function(test) {
- var job = schedule.scheduleJob(new Date(Date.now() + 1000), function() {});
- test.ok(job instanceof schedule.Job);
- job.cancel();
- test.done();
- }
- },
- ".scheduleJob(Date, fn)": {
- "Runs job once at some date": function(test) {
- test.expect(1);
- schedule.scheduleJob(new Date(Date.now() + 3000), function() {
- test.ok(true);
- });
- setTimeout(function() {
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- "Job doesn't emit initial 'scheduled' event": function(test) {
- var job = schedule.scheduleJob(new Date(Date.now() + 1000), function() {});
- job.on('scheduled', function() {
- test.ok(false);
- });
- setTimeout(function() {
- test.done();
- }, 1250);
- clock.tick(1250);
- },
- "Won't run job if scheduled in the past": function(test) {
- test.expect(1);
- var job = schedule.scheduleJob(new Date(Date.now() - 3000), function() {
- test.ok(false);
- });
- test.equal(job, null);
- setTimeout(function() {
- test.done();
- }, 1000);
- clock.tick(1000);
- }
- },
- ".scheduleJob(RecurrenceRule, fn)": {
- "Runs job at interval based on recur rule, repeating indefinitely": function(test) {
- test.expect(3);
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- "Job doesn't emit initial 'scheduled' event": function(test) {
- /*
- * If this was Job#schedule it'd fire 4 times.
- */
- test.expect(3);
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = new schedule.scheduleJob(rule, function() {});
- job.on('scheduled', function(runOnDate) {
- test.ok(true);
- });
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- "Doesn't invoke job if recur rule schedules it in the past": function(test) {
- test.expect(1);
- var rule = new schedule.RecurrenceRule();
- rule.year = 1960;
- var job = schedule.scheduleJob(rule, function() {
- test.ok(false);
- });
-
- test.equal(job, null);
- setTimeout(function() {
- test.done();
- }, 1000);
- clock.tick(1000);
- }
- },
- ".scheduleJob({...}, fn)": {
- "Runs job at interval based on object, repeating indefinitely": function(test) {
- test.expect(3);
- var job = new schedule.scheduleJob({
- second: null // Fire every second
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- "Job doesn't emit initial 'scheduled' event": function(test) {
- /*
- * With Job#schedule this would be 3:
- * scheduled at time 0
- * scheduled at time 1000
- * scheduled at time 2000
- */
- test.expect(2);
- var job = schedule.scheduleJob({
- second: null // fire every second
- }, function() {});
- job.on('scheduled', function() {
- test.ok(true);
- });
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 2250);
- clock.tick(2250);
- },
- "Doesn't invoke job if object schedules it in the past": function(test) {
- test.expect(1);
-
- var job = schedule.scheduleJob({
- year: 1960
- }, function() {
- test.ok(false);
- });
-
- test.equal(job, null);
- setTimeout(function() {
- test.done();
- }, 1000);
- clock.tick(1000);
- }
- },
- ".scheduleJob({...}, {...}, fn)": {
- "Callback called for each job if callback is provided": function(test) {
- test.expect(3);
- var job = new schedule.scheduleJob({
- second: null // Fire every second
- }, function() {}, function() {
- test.ok(true);
- });
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 3250);
- clock.tick(3250);
- }
- },
- ".rescheduleJob(job, {...})": {
- "Reschedule jobs from object based to object based": function(test) {
- test.expect(3);
- var job = new schedule.scheduleJob({
- second: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job, {
- minute: null
- });
- }, 3250);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 5000);
- clock.tick(5000);
- },
- "Reschedule jobs from every minutes to every second": function(test) {
- test.expect(3);
- var timeout = 60 * 1000;
- var job = new schedule.scheduleJob({
- minute: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job, {
- second: null
- });
- }, timeout);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, timeout + 2250);
- clock.tick(timeout + 2250);
- }
- },
- ".rescheduleJob(job, Date)": {
- "Reschedule jobs from Date to Date": function(test) {
- test.expect(1);
- var job = new schedule.scheduleJob(new Date(Date.now() + 3000), function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job, new Date(Date.now() + 5000));
- }, 1000);
- setTimeout(function() {
- test.done();
- }, 6150);
- clock.tick(6150);
- },
- "Reschedule jobs that has been executed": function(test) {
- test.expect(2);
- var job = new schedule.scheduleJob(new Date(Date.now() + 1000), function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job, new Date(Date.now() + 2000));
- }, 2000);
- setTimeout(function() {
- test.done();
- }, 5150);
- clock.tick(5150);
- }
- },
- ".rescheduleJob(job, RecurrenceRule)": {
- "Reschedule jobs from RecurrenceRule to RecurrenceRule": function(test) {
- test.expect(3);
- var timeout = 60 * 1000;
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- var newRule = new schedule.RecurrenceRule();
- newRule.minute = null;
- setTimeout(function() {
- schedule.rescheduleJob(job, newRule);
- }, 2250);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, timeout + 2250);
- clock.tick(timeout + 2250);
- },
- "Reschedule jobs from RecurrenceRule to Date": function(test) {
- test.expect(3);
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job, new Date(Date.now() + 2000));
- }, 2150);
- setTimeout(function() {
- test.done();
- }, 4250);
- clock.tick(4250);
- },
- "Reschedule jobs from RecurrenceRule to {...}": function(test) {
- test.expect(3);
- var timeout = 60 * 1000;
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job, {
- minute: null
- });
- }, 2150);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, timeout + 2150);
- clock.tick(timeout + 2150);
- },
- "Reschedule jobs that is not available": function(test) {
- test.expect(4);
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(null, new Date(Date.now() + 2000));
- }, 2150);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 4250);
- clock.tick(4250);
- }
- },
- '.rescheduleJob("job name", {...})': {
- "Reschedule jobs from object based to object based": function(test) {
- test.expect(3);
- var job = new schedule.scheduleJob({
- second: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job.name, {
- minute: null
- });
- }, 3250);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 5000);
- clock.tick(5000);
- },
- "Reschedule jobs from every minutes to every second": function(test) {
- test.expect(3);
- var timeout = 60 * 1000;
- var job = new schedule.scheduleJob({
- minute: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job.name, {
- second: null
- });
- }, timeout);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, timeout + 2250);
- clock.tick(timeout + 2250);
- }
- },
- '.rescheduleJob("job name", Date)': {
- "Reschedule jobs from Date to Date": function(test) {
- test.expect(1);
- var job = new schedule.scheduleJob(new Date(Date.now() + 3000), function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job.name, new Date(Date.now() + 5000));
- }, 1000);
- setTimeout(function() {
- test.done();
- }, 6150);
- clock.tick(6150);
- },
- "Reschedule jobs that has been executed": function(test) {
- test.expect(2);
- var job = new schedule.scheduleJob(new Date(Date.now() + 1000), function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job.name, new Date(Date.now() + 2000));
- }, 2000);
- setTimeout(function() {
- test.done();
- }, 5150);
- clock.tick(5150);
- }
- },
- '.rescheduleJob("job name", RecurrenceRule)': {
- "Reschedule jobs from RecurrenceRule to RecurrenceRule": function(test) {
- test.expect(3);
- var timeout = 60 * 1000;
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- var newRule = new schedule.RecurrenceRule();
- newRule.minute = null;
- setTimeout(function() {
- schedule.rescheduleJob(job.name, newRule);
- }, 2250);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, timeout + 2250);
- clock.tick(timeout + 2250);
- },
- "Reschedule jobs from RecurrenceRule to Date": function(test) {
- test.expect(3);
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job.name, new Date(Date.now() + 2000));
- }, 2150);
- setTimeout(function() {
- test.done();
- }, 4250);
- clock.tick(4250);
- },
- "Reschedule jobs from RecurrenceRule to {...}": function(test) {
- test.expect(3);
- var timeout = 60 * 1000;
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob(job.name, {
- minute: null
- });
- }, 2150);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, timeout + 2150);
- clock.tick(timeout + 2150);
- },
- "Reschedule jobs that is not available": function(test) {
- test.expect(4);
- var rule = new schedule.RecurrenceRule();
- rule.second = null; // fire every second
- var job = schedule.scheduleJob(rule, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.rescheduleJob("Blah", new Date(Date.now() + 2000));
- }, 2150);
- setTimeout(function() {
- job.cancel();
- test.done();
- }, 4250);
- clock.tick(4250);
- }
- },
- ".cancelJob(Job)": {
- "Prevents all future invocations of Job passed in": function(test) {
- test.expect(2);
- var job = schedule.scheduleJob({
- second: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.cancelJob(job);
- }, 2250);
- setTimeout(function() {
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- "Can cancel Jobs scheduled with Job#schedule": function(test) {
- test.expect(2);
- var job = new schedule.Job(function() {
- test.ok(true);
- });
- job.schedule({
- second: null
- });
- setTimeout(function() {
- schedule.cancelJob(job);
- }, 2250);
- setTimeout(function() {
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- "Job emits 'canceled' event": function(test) {
- test.expect(1);
- var job = schedule.scheduleJob({
- second: null
- }, function() {});
- job.on('canceled', function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.cancelJob(job);
- test.done();
- }, 1250);
- clock.tick(1250);
- }
- },
- '.cancelJob("job name")': {
- "Prevents all future invocations of Job identified by name": function(test) {
- test.expect(2);
- var job = schedule.scheduleJob({
- second: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.cancelJob(job.name);
- }, 2250);
- setTimeout(function() {
- test.done();
- }, 3250);
- clock.tick(3250);
- },
- /*
- "Can cancel Jobs scheduled with Job#schedule": function(test) {
- test.expect(2);
- var job = new schedule.Job(function() {
- test.ok(true);
- });
- job.schedule({
- second: null
- });
- setTimeout(function() {
- schedule.cancelJob(job.name);
- }, 2250);
- setTimeout(function() {
- test.done();
- }, 3250);
- },*/
- "Job emits 'canceled' event": function(test) {
- test.expect(1);
- var job = schedule.scheduleJob({
- second: null
- }, function() {});
- job.on('canceled', function() {
- test.ok(true);
- });
- setTimeout(function() {
- schedule.cancelJob(job.name);
- test.done();
- }, 1250);
- clock.tick(1250);
- },
- "Does nothing if no job found by that name": function(test) {
- test.expect(3);
- var job = schedule.scheduleJob({
- second: null
- }, function() {
- test.ok(true);
- });
- setTimeout(function() {
- // This cancel should not affect anything
- schedule.cancelJob('blah');
- }, 2250);
- setTimeout(function() {
- job.cancel(); // prevent tests from hanging
- test.done();
- }, 3250);
- clock.tick(3250);
- }
- },
- '.pendingInvocations()': {
- "Retrieves pendingInvocations of the job": function(test) {
- var job = schedule.scheduleJob(new Date(Date.now() + 1000), function() {});
- test.ok(job instanceof schedule.Job);
- test.ok(job.pendingInvocations()[0].job);
- job.cancel();
- test.done();
- }
- },
- tearDown: function(cb) {
- clock.restore();
- cb();
- }
- };
|