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.
- package/.travis.yml +1 -2
- package/README.md +0 -4
- package/lib/cron.js +451 -424
- package/package.json +7 -3
- package/test.js +37 -20
- package/tests/test-cron.js +445 -397
- package/tests/test-crontime.js +51 -7
package/tests/test-cron.js
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
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
|
});
|