schedule-cron-jobs.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. var sinon = require('sinon');
  3. var main = require('../package.json').main;
  4. var schedule = require('../' + main);
  5. var clock;
  6. module.exports = {
  7. ".scheduleJob(cron_expr, fn)": {
  8. setUp: function(cb) {
  9. var now = Date.now();
  10. clock = sinon.useFakeTimers();
  11. clock.tick(now);
  12. cb();
  13. },
  14. "Runs job every second": function(test) {
  15. test.expect(3);
  16. var timeout = 3 * 1000 + 150;
  17. var job = schedule.scheduleJob('* * * * * *', function() {
  18. test.ok(true);
  19. });
  20. setTimeout(function() {
  21. job.cancel();
  22. test.done();
  23. }, timeout);
  24. clock.tick(timeout);
  25. },
  26. "Runs job every minute": function(test) {
  27. test.expect(3);
  28. var timeout = 3 * 60 * 1000;
  29. var job = schedule.scheduleJob('0 * * * * *', function() {
  30. test.ok(true);
  31. });
  32. setTimeout(function() {
  33. job.cancel();
  34. test.done();
  35. }, timeout);
  36. clock.tick(timeout);
  37. },
  38. "Runs job every hour": function(test) {
  39. test.expect(3);
  40. var timeout = 3 * 60 * 60 * 1000;
  41. var job = schedule.scheduleJob('0 0 * * * *', function() {
  42. test.ok(true);
  43. });
  44. setTimeout(function() {
  45. job.cancel();
  46. test.done();
  47. }, timeout);
  48. clock.tick(timeout);
  49. },
  50. "Runs job every day": function(test) {
  51. test.expect(3);
  52. var timeout = 3 * 24 * 60 * 60 * 1000;
  53. var job = schedule.scheduleJob('0 0 0 * * *', function() {
  54. test.ok(true);
  55. });
  56. setTimeout(function() {
  57. job.cancel();
  58. test.done();
  59. }, timeout);
  60. clock.tick(timeout);
  61. },
  62. "Runs job every week": function(test) {
  63. test.expect(3);
  64. var timeout = 3 * 7 * 24 * 60 * 60 * 1000;
  65. var job = schedule.scheduleJob('0 0 0 * * 1', function() {
  66. test.ok(true);
  67. });
  68. setTimeout(function() {
  69. job.cancel();
  70. test.done();
  71. }, timeout);
  72. clock.tick(timeout);
  73. },
  74. "Runs job every month": function(test) {
  75. test.expect(48);
  76. var timeout = 4 * 365.25 * 24 * 60 * 60 * 1000;
  77. var job = schedule.scheduleJob('0 0 0 1 * *', function() {
  78. test.ok(true);
  79. });
  80. setTimeout(function() {
  81. job.cancel();
  82. test.done();
  83. }, timeout);
  84. clock.tick(timeout);
  85. },
  86. "Runs job every year": function(test) {
  87. test.expect(4);
  88. var timeout = 4 * 365.25 * 24 * 60 * 60 * 1000;
  89. var job = schedule.scheduleJob('0 0 0 1 1 *', function() {
  90. test.ok(true);
  91. });
  92. setTimeout(function() {
  93. job.cancel();
  94. test.done();
  95. }, timeout);
  96. clock.tick(timeout);
  97. },
  98. tearDown: function(cb) {
  99. clock.restore();
  100. cb();
  101. }
  102. }
  103. };