cron 1.7.1 → 1.8.2
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/.eslintrc +26 -32
- package/.travis.yml +1 -1
- package/CHANGELOG.md +6 -0
- package/README.md +5 -4
- package/examples/basic.js +1 -1
- package/lib/cron.js +53 -15
- package/package.json +15 -13
- package/tests/cron.test.js +843 -0
- package/tests/crontime.test.js +406 -0
- package/Makefile +0 -10
- package/tests/test-cron.js +0 -1060
- package/tests/test-crontime.js +0 -354
package/tests/test-crontime.js
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
var chai = require('chai');
|
|
2
|
-
var expect = chai.expect;
|
|
3
|
-
var cron = require('../lib/cron');
|
|
4
|
-
var moment = require('moment-timezone');
|
|
5
|
-
var sinon = require('sinon');
|
|
6
|
-
|
|
7
|
-
/* eslint-disable no-new */
|
|
8
|
-
|
|
9
|
-
describe('crontime', function() {
|
|
10
|
-
it('should test stars (* * * * * *)', function() {
|
|
11
|
-
expect(function() {
|
|
12
|
-
new cron.CronTime('* * * * * *');
|
|
13
|
-
}).to.not.throw(Error);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should test digit (0 * * * * *)', function() {
|
|
17
|
-
expect(function() {
|
|
18
|
-
new cron.CronTime('0 * * * * *');
|
|
19
|
-
}).to.not.throw(Error);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should test multi digits (08 * * * * *)', function() {
|
|
23
|
-
expect(function() {
|
|
24
|
-
new cron.CronTime('08 * * * * *');
|
|
25
|
-
}).to.not.throw(Error);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should test all digits (08 8 8 8 8 5)', function() {
|
|
29
|
-
expect(function() {
|
|
30
|
-
new cron.CronTime('08 * * * * *');
|
|
31
|
-
}).to.not.throw(Error);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should test too many digits (08 8 8 8 8 5)', function() {
|
|
35
|
-
expect(function() {
|
|
36
|
-
new cron.CronTime('08 * * * * *');
|
|
37
|
-
}).to.not.throw(Error);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should test standard cron format (* * * * *)', function() {
|
|
41
|
-
expect(function() {
|
|
42
|
-
new cron.CronTime('* * * * *');
|
|
43
|
-
}).to.not.throw(Error);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should test standard cron format (8 8 8 8 5)', function() {
|
|
47
|
-
var standard = new cron.CronTime('8 8 8 8 5');
|
|
48
|
-
var extended = new cron.CronTime('0 8 8 8 8 5');
|
|
49
|
-
|
|
50
|
-
expect(standard.dayOfWeek).to.deep.eql(extended.dayOfWeek);
|
|
51
|
-
expect(standard.month).to.deep.eql(extended.month);
|
|
52
|
-
expect(standard.dayOfMonth).to.deep.eql(extended.dayOfMonth);
|
|
53
|
-
expect(standard.hour).to.deep.eql(extended.hour);
|
|
54
|
-
expect(standard.minute).to.deep.eql(extended.minute);
|
|
55
|
-
expect(standard.second).to.deep.eql(extended.second);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should test hyphen (0-10 * * * * *)', function() {
|
|
59
|
-
expect(function() {
|
|
60
|
-
new cron.CronTime('0-10 * * * * *');
|
|
61
|
-
}).to.not.throw(Error);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should test multi hyphens (0-10 0-10 * * * *)', function() {
|
|
65
|
-
expect(function() {
|
|
66
|
-
new cron.CronTime('0-10 0-10 * * * *');
|
|
67
|
-
}).to.not.throw(Error);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should test all hyphens (0-10 0-10 0-10 0-10 0-10 0-1)', function() {
|
|
71
|
-
expect(function() {
|
|
72
|
-
new cron.CronTime('0-10 0-10 0-10 0-10 0-10 0-1');
|
|
73
|
-
}).to.not.throw(Error);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should test comma (0,10 * * * * *)', function() {
|
|
77
|
-
expect(function() {
|
|
78
|
-
new cron.CronTime('0,10 * * * * *');
|
|
79
|
-
}).to.not.throw(Error);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should test multi commas (0,10 0,10 * * * *)', function() {
|
|
83
|
-
expect(function() {
|
|
84
|
-
new cron.CronTime('0,10 0,10 * * * *');
|
|
85
|
-
}).to.not.throw(Error);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should test all commas (0,10 0,10 0,10 0,10 0,10 0,1)', function() {
|
|
89
|
-
expect(function() {
|
|
90
|
-
new cron.CronTime('0,10 0,10 0,10 0,10 0,10 0,1');
|
|
91
|
-
}).to.not.throw(Error);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should test alias (* * * * jan *)', function() {
|
|
95
|
-
expect(function() {
|
|
96
|
-
new cron.CronTime('* * * * jan *');
|
|
97
|
-
}).to.not.throw(Error);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should test multi aliases (* * * * jan,feb *)', function() {
|
|
101
|
-
expect(function() {
|
|
102
|
-
new cron.CronTime('* * * * jan,feb *');
|
|
103
|
-
}).to.not.throw(Error);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should test all aliases (* * * * jan,feb mon,tue)', function() {
|
|
107
|
-
expect(function() {
|
|
108
|
-
new cron.CronTime('* * * * jan,feb mon,tue');
|
|
109
|
-
}).to.not.throw(Error);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('should test every second monday (* * * * * mon/2)');
|
|
113
|
-
|
|
114
|
-
it('should test unknown alias (* * * * jar *)', function() {
|
|
115
|
-
expect(function() {
|
|
116
|
-
new cron.CronTime('* * * * jar *');
|
|
117
|
-
}).to.throw(Error);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should test unknown alias - short (* * * * j *)', function() {
|
|
121
|
-
expect(function() {
|
|
122
|
-
new cron.CronTime('* * * * j *');
|
|
123
|
-
}).to.throw(Error);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('should test Date', function() {
|
|
127
|
-
var d = new Date();
|
|
128
|
-
var ct = new cron.CronTime(d);
|
|
129
|
-
expect(ct.source.isSame(d.getTime())).to.be.true;
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('should test day roll-over', function() {
|
|
133
|
-
var numHours = 24;
|
|
134
|
-
var ct = new cron.CronTime('0 0 17 * * *');
|
|
135
|
-
|
|
136
|
-
for (var hr = 0; hr < numHours; hr++) {
|
|
137
|
-
var start = new Date(2012, 3, 16, hr, 30, 30);
|
|
138
|
-
var next = ct._getNextDateFrom(start);
|
|
139
|
-
|
|
140
|
-
expect(next - start).to.be.lt(24 * 60 * 60 * 1000);
|
|
141
|
-
expect(next._d).to.be.gt(start);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('should test illegal repetition syntax', function() {
|
|
146
|
-
expect(function() {
|
|
147
|
-
new cron.CronTime('* * /4 * * *');
|
|
148
|
-
}).to.throw(Error);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it('should test next date', function() {
|
|
152
|
-
var ct = new cron.CronTime('0 0 */4 * * *');
|
|
153
|
-
|
|
154
|
-
var nextDate = new Date();
|
|
155
|
-
nextDate.setHours(23);
|
|
156
|
-
var nextdt = ct._getNextDateFrom(nextDate);
|
|
157
|
-
|
|
158
|
-
expect(nextdt._d).to.be.gt(nextDate);
|
|
159
|
-
expect(nextdt.hours() % 4).to.eql(0);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('should throw an exception because next date is invalid', function() {
|
|
163
|
-
var ct = new cron.CronTime('0 0 * * * *');
|
|
164
|
-
var nextDate = new Date('My invalid date string');
|
|
165
|
-
try {
|
|
166
|
-
var nextdt = ct._getNextDateFrom(nextDate);
|
|
167
|
-
} catch (e) {
|
|
168
|
-
expect(e.message).to.eql('ERROR: You specified an invalid date.');
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('should test next real date', function() {
|
|
173
|
-
var initialDate = new Date();
|
|
174
|
-
var ct = new cron.CronTime(initialDate);
|
|
175
|
-
|
|
176
|
-
var nextDate = new Date();
|
|
177
|
-
nextDate.setMonth(nextDate.getMonth() + 1);
|
|
178
|
-
expect(nextDate).to.be.gt(ct.source._d);
|
|
179
|
-
var nextdt = ct.sendAt(0);
|
|
180
|
-
// there shouldn't be a "next date" when using a real date.
|
|
181
|
-
// execution happens once
|
|
182
|
-
// so the return should be the date passed in unless explicitly reset
|
|
183
|
-
expect(nextdt.isBefore(nextDate)).to.be.true;
|
|
184
|
-
expect(nextdt.isSame(initialDate)).to.be.true;
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('should test next month selection');
|
|
188
|
-
|
|
189
|
-
describe('should throw an exception because `L` not supported', function() {
|
|
190
|
-
it('(* * * L * *)', function() {
|
|
191
|
-
expect(function() {
|
|
192
|
-
new cron.CronTime('* * * L * *');
|
|
193
|
-
}).to.throw(Error);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('(* * * * * L)', function() {
|
|
197
|
-
expect(function() {
|
|
198
|
-
new cron.CronTime('* * * * * L');
|
|
199
|
-
}).to.throw(Error);
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it('should strip off millisecond', function() {
|
|
204
|
-
var cronTime = new cron.CronTime('0 */10 * * * *');
|
|
205
|
-
var x = cronTime._getNextDateFrom(new Date('2018-08-10T02:20:00.999Z'));
|
|
206
|
-
expect(x.toISOString()).to.equal('2018-08-10T02:30:00.000Z');
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
it('should strip off millisecond (2)', function() {
|
|
210
|
-
var cronTime = new cron.CronTime('0 */10 * * * *');
|
|
211
|
-
var x = cronTime._getNextDateFrom(new Date('2018-08-10T02:19:59.999Z'));
|
|
212
|
-
expect(x.toISOString()).to.equal('2018-08-10T02:20:00.000Z');
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it('should generete the right next days when cron is set to every minute', function() {
|
|
216
|
-
var cronTime = new cron.CronTime('* * * * *');
|
|
217
|
-
var min = 60000;
|
|
218
|
-
var previousDate = new Date(Date.UTC(2018, 5, 3, 0, 0));
|
|
219
|
-
for (var i = 0; i < 25; i++) {
|
|
220
|
-
var nextDate = cronTime._getNextDateFrom(previousDate);
|
|
221
|
-
expect(nextDate.valueOf()).to.equal(previousDate.valueOf() + min);
|
|
222
|
-
previousDate = nextDate;
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('should generete the right next days when cron is set to every 15 min', function() {
|
|
227
|
-
var cronTime = new cron.CronTime('*/15 * * * *');
|
|
228
|
-
var min = 60000 * 15;
|
|
229
|
-
var previousDate = new Date(Date.UTC(2016, 6, 3, 0, 0));
|
|
230
|
-
for (var i = 0; i < 25; i++) {
|
|
231
|
-
var nextDate = cronTime._getNextDateFrom(previousDate);
|
|
232
|
-
expect(nextDate.valueOf()).to.equal(previousDate.valueOf() + min);
|
|
233
|
-
previousDate = nextDate;
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
it('should work around time zone changes that shifts time back (1)',function(){
|
|
237
|
-
var d = new Date("10-7-2018")
|
|
238
|
-
// America/Sao_Paulo has a time zone change around NOV 3 2018.
|
|
239
|
-
var cronTime = new cron.CronTime('0 0 9 4 * *');
|
|
240
|
-
var nextDate = cronTime._getNextDateFrom(d, 'America/Sao_Paulo');
|
|
241
|
-
expect(nextDate.valueOf()).to.equal(moment('2018-11-04T09:00:00.000-02:00').valueOf())
|
|
242
|
-
});
|
|
243
|
-
it('should work around time zone changes that shifts time back (2)',function(){
|
|
244
|
-
// Asia/Amman DST ends in 26 - OCT-2018 (-1 to hours)
|
|
245
|
-
var d = moment.tz('2018-10-25T23:00','Asia/Amman');
|
|
246
|
-
var cronTime = new cron.CronTime('0 0 * * *');
|
|
247
|
-
var nextDate = cronTime._getNextDateFrom(d, 'Asia/Amman');
|
|
248
|
-
expect(nextDate-moment.tz('2018-10-26T00:00','Asia/Amman')).to.equal(0);
|
|
249
|
-
});
|
|
250
|
-
it('should work around time zone changes that shifts time forward',function(){
|
|
251
|
-
// Asia/Amman DST starts in 30-March-2018 (+1 to hours)
|
|
252
|
-
var currentDate = moment.tz('2018-03-29T23:00','Asia/Amman');
|
|
253
|
-
var cronTime = new cron.CronTime('* * * * *');
|
|
254
|
-
for(var i=0;i<100;i++){
|
|
255
|
-
var nextDate = cronTime._getNextDateFrom(currentDate,'Asia/Amman');
|
|
256
|
-
expect(nextDate-currentDate).to.equal(1000*60);
|
|
257
|
-
currentDate=nextDate;
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
it('should generate the right N next days for * * * * *',function(){
|
|
261
|
-
var cronTime = new cron.CronTime('* * * * *');
|
|
262
|
-
var currentDate=moment().seconds(0).milliseconds(0);
|
|
263
|
-
for(var i=0;i<100;i++){
|
|
264
|
-
var nextDate = cronTime._getNextDateFrom(currentDate);
|
|
265
|
-
expect(nextDate-currentDate).to.equal(1000*60);
|
|
266
|
-
currentDate=nextDate;
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
it('should generate the right N next days for 0 0 9 * * *',function(){
|
|
270
|
-
var cronTime = new cron.CronTime('0 0 9 * * *');
|
|
271
|
-
var currentDate=moment().utc().seconds(0).milliseconds(0).hours(9).minutes(0);
|
|
272
|
-
for(var i=0;i<100;i++){
|
|
273
|
-
var nextDate = cronTime._getNextDateFrom(currentDate);
|
|
274
|
-
expect(nextDate-currentDate).to.equal(1000*60*60*24);
|
|
275
|
-
currentDate=nextDate;
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
it('should generate the right N next days for 0 0 * * * with a time zone',function(){
|
|
279
|
-
var cronTime = new cron.CronTime('0 * * * *');
|
|
280
|
-
var currentDate=moment.tz('2018-11-02T23:00','America/Sao_Paulo').seconds(0).milliseconds(0);
|
|
281
|
-
for(var i=0;i<25;i++){
|
|
282
|
-
var nextDate = cronTime._getNextDateFrom(currentDate,'America/Sao_Paulo');
|
|
283
|
-
expect(nextDate-currentDate).to.equal(1000*60*60);
|
|
284
|
-
currentDate=nextDate;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
});
|
|
288
|
-
it('should generate the right N next days for */3 * * * * with a time zone',function(){
|
|
289
|
-
var cronTime = new cron.CronTime('*/3 * * * *');
|
|
290
|
-
var currentDate=moment.tz('2018-11-02T23:00','America/Sao_Paulo').seconds(0).milliseconds(0);
|
|
291
|
-
for(var i=0;i<25;i++){
|
|
292
|
-
var nextDate = cronTime._getNextDateFrom(currentDate,'America/Sao_Paulo');
|
|
293
|
-
expect(nextDate-currentDate).to.equal(1000*60*3);
|
|
294
|
-
currentDate=nextDate;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
});
|
|
298
|
-
it('should generete the right next day when cron is set to every 15 min in Feb', function() {
|
|
299
|
-
var cronTime = new cron.CronTime('*/15 * * FEB *');
|
|
300
|
-
var previousDate = new Date(Date.UTC(2018, 3, 0, 0, 0));
|
|
301
|
-
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
|
|
302
|
-
expect(nextDate.valueOf()).to.equal(
|
|
303
|
-
new Date(Date.UTC(2019, 1, 1, 0, 0)).valueOf()
|
|
304
|
-
);
|
|
305
|
-
});
|
|
306
|
-
it('should generate the right next day when cron is set to both day of the month and day of the week (1)', function() {
|
|
307
|
-
var cronTime = new cron.CronTime('0 8 1 * 4');
|
|
308
|
-
var previousDate = new Date(Date.UTC(2019, 3, 22, 0, 0));
|
|
309
|
-
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
|
|
310
|
-
expect(nextDate.valueOf()).to.equal(
|
|
311
|
-
new Date(Date.UTC(2019, 3, 25, 8, 0)).valueOf()
|
|
312
|
-
);
|
|
313
|
-
});
|
|
314
|
-
it('should generate the right next day when cron is set to both day of the month and day of the week (2)', function() {
|
|
315
|
-
var cronTime = new cron.CronTime('0 8 1 * 4');
|
|
316
|
-
var previousDate = new Date(Date.UTC(2019, 3, 26, 0, 0));
|
|
317
|
-
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
|
|
318
|
-
expect(nextDate.valueOf()).to.equal(
|
|
319
|
-
new Date(Date.UTC(2019, 4, 1, 8, 0)).valueOf()
|
|
320
|
-
);
|
|
321
|
-
});
|
|
322
|
-
it('should generate the right next day when cron is set to both day of the month and day of the week (3)', function() {
|
|
323
|
-
var cronTime = new cron.CronTime('0 8 1 * 4');
|
|
324
|
-
var previousDate = new Date(Date.UTC(2019, 7, 1, 7, 59));
|
|
325
|
-
var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
|
|
326
|
-
expect(nextDate.valueOf()).to.equal(
|
|
327
|
-
new Date(Date.UTC(2019, 7, 1, 8, 0)).valueOf()
|
|
328
|
-
);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it('should accept 0 as a valid UTC offset', function() {
|
|
332
|
-
var clock = sinon.useFakeTimers();
|
|
333
|
-
|
|
334
|
-
var cronTime = new cron.CronTime('0 11 * * *', null, 0);
|
|
335
|
-
var expected = moment().add(11, 'hours').unix();
|
|
336
|
-
var actual = cronTime.sendAt().unix();
|
|
337
|
-
|
|
338
|
-
expect(actual).to.equal(expected);
|
|
339
|
-
|
|
340
|
-
clock.restore();
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
it('should accept -120 as a valid UTC offset', function() {
|
|
344
|
-
var clock = sinon.useFakeTimers();
|
|
345
|
-
|
|
346
|
-
var cronTime = new cron.CronTime('0 11 * * *', null, -120);
|
|
347
|
-
var expected = moment().add(13, 'hours').unix();
|
|
348
|
-
var actual = cronTime.sendAt().unix();
|
|
349
|
-
|
|
350
|
-
expect(actual).to.equal(expected);
|
|
351
|
-
|
|
352
|
-
clock.restore();
|
|
353
|
-
});
|
|
354
|
-
});
|