schedule-cron-jobs.js 2.7 KB

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