cron 1.0.5 → 1.0.9

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