test-cron.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. var testCase = require('nodeunit').testCase,
  2. cron = require('../lib/cron');
  3. module.exports = testCase({
  4. 'test second (* * * * * *)': function(assert) {
  5. assert.expect(1);
  6. var c = new cron.CronJob('* * * * * *', function() {
  7. assert.ok(true);
  8. }, null, true);
  9. setTimeout(function() {
  10. c.stop();
  11. assert.done();
  12. }, 1250);
  13. },
  14. 'test second with oncomplete (* * * * * *)': function(assert) {
  15. assert.expect(2);
  16. var c = new cron.CronJob('* * * * * *', function(done) {
  17. assert.ok(true);
  18. }, function () {
  19. assert.ok(true);
  20. assert.done();
  21. }, true);
  22. setTimeout(function() {
  23. c.stop();
  24. }, 1250);
  25. },
  26. 'test every second for 5 seconds (* * * * * *)': function(assert) {
  27. assert.expect(5);
  28. var c = new cron.CronJob('* * * * * *', function() {
  29. assert.ok(true);
  30. }, null, true);
  31. setTimeout(function() {
  32. c.stop();
  33. assert.done();
  34. }, 5250);
  35. },
  36. 'test standard cron no-seconds syntax doesnt send on seconds (* * * * *)': function(assert) {
  37. assert.expect(0);
  38. // Delay test from running at minute boundary
  39. var prepDate = new Date();
  40. if (prepDate.getSeconds() >= 55) {
  41. setTimeout(testRun, 5000);
  42. } else {
  43. testRun();
  44. }
  45. function testRun() {
  46. var c = new cron.CronJob('* * * * *', function() {
  47. assert.ok(true);
  48. }, null, true);
  49. setTimeout(function() {
  50. c.stop();
  51. assert.done();
  52. }, 5250);
  53. }
  54. },
  55. 'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
  56. assert.expect(6);
  57. var c = new cron.CronJob('* * * * * *', function(done) {
  58. assert.ok(true);
  59. }, function() {
  60. assert.ok(true);
  61. assert.done();
  62. }, true);
  63. setTimeout(function() {
  64. c.stop();
  65. }, 5250);
  66. },
  67. 'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) {
  68. assert.expect(5);
  69. var c = new cron.CronJob('*/1 * * * * *', function() {
  70. assert.ok(true);
  71. }, null, true);
  72. setTimeout(function() {
  73. assert.done();
  74. c.stop();
  75. }, 5250);
  76. },
  77. 'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) {
  78. assert.expect(6);
  79. var c = new cron.CronJob('*/1 * * * * *', function(done) {
  80. assert.ok(true);
  81. }, function() {
  82. assert.ok(true);
  83. assert.done();
  84. }, true);
  85. setTimeout(function() {
  86. c.stop();
  87. }, 5250);
  88. },
  89. 'test every second for a range ([start]-[end] * * * * *)': function(assert) {
  90. assert.expect(5);
  91. var prepDate = new Date();
  92. if ((54 - prepDate.getSeconds()) <= 0) {
  93. setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
  94. } else {
  95. testRun();
  96. }
  97. function testRun() {
  98. var d = new Date();
  99. var s = d.getSeconds()+2;
  100. var e = s + 6; //end value is inclusive
  101. var c = new cron.CronJob(s + '-' + e +' * * * * *', function() {
  102. assert.ok(true);
  103. }, null, true);
  104. setTimeout(function() {
  105. c.stop();
  106. assert.done();
  107. }, 6250);
  108. }
  109. },
  110. 'test every second for a range with oncomplete ([start]-[end] * * * * *)': function(assert) {
  111. assert.expect(6);
  112. var prepDate = new Date();
  113. if ((54 - prepDate.getSeconds()) <= 0) {
  114. setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
  115. } else {
  116. testRun();
  117. }
  118. function testRun() {
  119. var d = new Date();
  120. var s = d.getSeconds()+2;
  121. var e = s + 6; //end value is inclusive
  122. var c = new cron.CronJob(s + '-' + e +' * * * * *', function() {
  123. assert.ok(true);
  124. }, function() {
  125. assert.ok(true);
  126. assert.done();
  127. }, true);
  128. setTimeout(function() {
  129. c.stop();
  130. }, 6250);
  131. }
  132. },
  133. 'test second (* * * * * *) object constructor': function(assert) {
  134. assert.expect(1);
  135. var c = new cron.CronJob({
  136. cronTime: '* * * * * *',
  137. onTick: function() {
  138. assert.ok(true);
  139. },
  140. start: true
  141. });
  142. setTimeout(function() {
  143. c.stop();
  144. assert.done();
  145. }, 1250);
  146. },
  147. 'test second with oncomplete (* * * * * *) object constructor': function(assert) {
  148. assert.expect(2);
  149. var c = new cron.CronJob({
  150. cronTime: '* * * * * *',
  151. onTick: function(done) {
  152. assert.ok(true);
  153. },
  154. onComplete: function () {
  155. assert.ok(true);
  156. assert.done();
  157. },
  158. start: true
  159. });
  160. setTimeout(function() {
  161. c.stop();
  162. }, 1250);
  163. },
  164. 'test start/stop': function(assert) {
  165. assert.expect(1);
  166. var c = new cron.CronJob('* * * * * *', function() {
  167. assert.ok(true);
  168. this.stop();
  169. });
  170. c.start();
  171. setTimeout(function() {
  172. assert.done();
  173. }, 3250);
  174. },
  175. 'test specifying a specific date': function(assert) {
  176. assert.expect(2);
  177. var prepDate = new Date();
  178. if ((58 - prepDate.getSeconds()) <= 0) {
  179. setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
  180. } else {
  181. testRun();
  182. }
  183. function testRun() {
  184. var d = new Date();
  185. var s = d.getSeconds()+1;
  186. d.setSeconds(s);
  187. var c = new cron.CronJob(d, function() {
  188. var t = new Date();
  189. assert.equal(t.getSeconds(), d.getSeconds());
  190. assert.ok(true);
  191. }, null, true);
  192. setTimeout(function() {
  193. c.stop();
  194. assert.done();
  195. }, 2250);
  196. }
  197. },
  198. 'test specifying a specific date with oncomplete': function(assert) {
  199. assert.expect(3);
  200. var prepDate = new Date();
  201. if ((58 - prepDate.getSeconds()) <= 0) {
  202. setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
  203. } else {
  204. testRun();
  205. }
  206. function testRun() {
  207. var d = new Date();
  208. var s = d.getSeconds()+1;
  209. d.setSeconds(s);
  210. var c = new cron.CronJob(d, function() {
  211. var t = new Date();
  212. assert.equal(t.getSeconds(), d.getSeconds());
  213. assert.ok(true);
  214. }, function() {
  215. assert.ok(true);
  216. assert.done();
  217. }, true);
  218. setTimeout(function() {
  219. c.stop();
  220. }, 2250);
  221. }
  222. },
  223. 'test a job with a string and a given time zone': function (assert) {
  224. assert.expect(3);
  225. var time = require("time");
  226. var zone = "America/Chicago";
  227. // New Orleans time
  228. var t = new time.Date();
  229. t.setTimezone(zone);
  230. // Current time
  231. d = new Date();
  232. // If current time is New Orleans time, switch to Los Angeles..
  233. if (t.getHours() === d.getHours()) {
  234. zone = "America/Los_Angeles";
  235. t.setTimezone(zone);
  236. }
  237. assert.notEqual(d.getHours(), t.getHours());
  238. assert.ok(!(Date instanceof time.Date));
  239. // If t = 59s12m then t.setSeconds(60)
  240. // becones 00s13m so we're fine just doing
  241. // this and no testRun callback.
  242. t.setSeconds(t.getSeconds()+1);
  243. // Run a job designed to be executed at a given
  244. // time in `zone`, making sure that it is a different
  245. // hour than local time.
  246. var c = new cron.CronJob(t.getSeconds() + ' ' + t.getMinutes() + ' ' + t.getHours() + ' * * *', function(){
  247. assert.ok(true);
  248. }, undefined, true, zone);
  249. setTimeout(function() {
  250. c.stop();
  251. assert.done();
  252. }, 1250);
  253. },
  254. 'test a job with a date and a given time zone': function (assert) {
  255. assert.expect(3);
  256. var time = require("time");
  257. var zone = "America/Chicago";
  258. // New Orleans time
  259. var t = new time.Date();
  260. t.setTimezone(zone);
  261. // Current time
  262. d = new Date();
  263. // If current time is New Orleans time, switch to Los Angeles..
  264. if (t.getHours() === d.getHours()) {
  265. zone = "America/Los_Angeles";
  266. t.setTimezone(zone);
  267. }
  268. assert.notEqual(d.getHours(), t.getHours());
  269. assert.ok(!(Date instanceof time.Date));
  270. if ((58 - t.getSeconds()) <= 0) {
  271. setTimeout(testRun, (60000 - (t.getSeconds()*1000)) + 1000);
  272. } else {
  273. testRun();
  274. }
  275. function testRun() {
  276. var s = d.getSeconds()+1;
  277. d.setSeconds(s);
  278. var c = new cron.CronJob(d, function() {
  279. assert.ok(true);
  280. }, null, true, zone);
  281. setTimeout(function() {
  282. c.stop();
  283. assert.done();
  284. }, 2250);
  285. }
  286. },
  287. 'test dates fire only once': function(assert) {
  288. assert.expect(1);
  289. var count = 0;
  290. var d = new Date().getTime() + 1000;
  291. var job = cron.job(new Date(d), function() {
  292. count++;
  293. });
  294. job.start();
  295. setTimeout(function() {
  296. job.stop();
  297. assert.equal(count, 1);
  298. assert.done();
  299. }, 5250);
  300. },
  301. 'test long wait should not fire immediately': function(assert) {
  302. assert.expect(1);
  303. var count = 0;
  304. var d = new Date().getTime() + 31 * 86400 * 1000;
  305. var job = cron.job(new Date(d), function() {
  306. assert.ok(false);
  307. });
  308. job.start();
  309. setTimeout(function() {
  310. job.stop();
  311. assert.ok(true);
  312. assert.done();
  313. }, 250);
  314. },
  315. 'test start, change time, start again': function(assert) {
  316. assert.expect(3);
  317. var c = new cron.CronJob('* * * * * *', function() {
  318. assert.ok(true);
  319. });
  320. var time = cron.time('*/2 * * * * *');
  321. c.start();
  322. setTimeout(function() {
  323. c.stop();
  324. c.setTime(time);
  325. c.start();
  326. setTimeout(function() {
  327. c.stop();
  328. assert.done();
  329. }, 4250);
  330. }, 1250);
  331. },
  332. 'test start, change time, excpetion': function(assert) {
  333. assert.expect(2);
  334. var c = new cron.CronJob('* * * * * *', function() {
  335. assert.ok(true);
  336. });
  337. var time = new Date();
  338. c.start();
  339. setTimeout(function() {
  340. c.stop();
  341. assert.throws(function() {
  342. c.setTime(time);
  343. });
  344. assert.done();
  345. }, 1250);
  346. },
  347. 'test cronjob scoping': function(assert) {
  348. assert.expect(2);
  349. var c = new cron.CronJob('* * * * * *', function() {
  350. assert.ok(true);
  351. assert.ok(c instanceof cron.CronJob);
  352. }, null, true);
  353. setTimeout(function() {
  354. c.stop();
  355. assert.done();
  356. }, 1250);
  357. },
  358. 'test non-cronjob scoping': function(assert) {
  359. assert.expect(2);
  360. var c = new cron.CronJob('* * * * * *', function() {
  361. assert.ok(true);
  362. assert.equal(this.hello, 'world');
  363. }, null, true, null, {'hello':'world'});
  364. setTimeout(function() {
  365. c.stop();
  366. assert.done();
  367. }, 1250);
  368. },
  369. 'test non-cronjob scoping inside object': function(assert) {
  370. assert.expect(2);
  371. var c = new cron.CronJob({
  372. cronTime: '* * * * * *',
  373. onTick: function() {
  374. assert.ok(true);
  375. assert.equal(this.hello, 'world');
  376. },
  377. start: true,
  378. context: {hello: 'world'}
  379. });
  380. setTimeout(function() {
  381. c.stop();
  382. assert.done();
  383. }, 1250);
  384. }
  385. });