-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
233 lines (185 loc) · 5.37 KB
/
test.js
File metadata and controls
233 lines (185 loc) · 5.37 KB
1
2
3
4
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
const t = require('tap');
const {CronJob} = require('cron');
const sinon = require('sinon');
const CronGroup = require('.');
function noop() {}
t.test('CronGroup', async (t) => {
await t.test('constructor', (t) => {
const group = new CronGroup();
t.ok(group, 'success');
t.strictSame(group.jobs, {}, 'check jobs');
t.is(group.runningCount, 0, 'check runningCount');
t.end();
});
await t.test('add', async (t) => {
await t.test('success', (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: noop
});
const {foo} = group.jobs;
t.ok(foo, 'check added job');
t.type(foo.cron, CronJob, 'check CronJob instance');
t.is(foo.schedule, '* * * * * *', 'check schedule');
t.is(foo.worker, noop, 'check worker');
t.is(foo.isRunning, false, 'check isRunning');
t.end();
});
await t.test('already added', (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: noop
});
t.throws(
() => group.add({
name: 'foo',
schedule: '* * * * * *',
worker: noop
}),
{message: 'CronGroup: job with name "foo" already exist'},
'throw'
);
t.end();
});
});
await t.test('run', async (t) => {
await t.test('unknown job', async (t) => {
const group = new CronGroup();
await t.rejects(
group.run('foo'),
{message: 'CronGroup: unknown job "foo"'},
'should reject'
);
});
await t.test('success', async (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: () => Promise.resolve('ok!')
});
const runSpy = sinon.spy();
const completeSpy = sinon.spy();
group.on('run', ({name, runAt, cause}) => {
t.is(name, 'foo', 'check name');
t.ok(runAt, 'check runAt');
t.is(cause, 'manual', 'check cause');
t.is(group.jobs.foo.isRunning, true, 'check isRunning');
t.is(group.runningCount, 1, 'check runningCount');
runSpy();
});
group.on('complete', ({name, result, runAt, completedAt}) => {
t.is(name, 'foo', 'check name');
t.ok(runAt, 'check runAt');
t.ok(completedAt, 'check completedAt');
t.is(result, 'ok!', 'check result');
t.is(group.jobs.foo.isRunning, false, 'check isRunning');
t.is(group.runningCount, 0, 'check runningCount');
completeSpy();
});
await group.run('foo');
t.ok(runSpy.calledOnce, 'run event emitted once');
t.ok(completeSpy.calledOnce, 'complete event emitted once');
});
await t.test('already running', async (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: () => new Promise((resolve) => setTimeout(resolve, 100))
});
const runSpy = sinon.spy();
const completeSpy = sinon.spy();
group.on('run', runSpy);
group.on('complete', completeSpy);
await Promise.all([
group.run('foo'),
group.run('foo')
]);
t.ok(runSpy.calledOnce, 'run event emitted once');
t.ok(completeSpy.calledOnce, 'complete event emitted once');
});
await t.test('with error', async (t) => {
const group = new CronGroup();
const workerError = new Error('error!');
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: () => Promise.reject(workerError)
});
const runSpy = sinon.spy();
const completeSpy = sinon.spy();
const errorSpy = sinon.spy();
group.on('run', runSpy);
group.on('complete', completeSpy);
group.on('error', ({name, err}) => {
t.is(name, 'foo', 'check name');
t.is(err, workerError, 'check error');
errorSpy();
});
await group.run('foo');
t.ok(runSpy.calledOnce, 'run event emitted once');
t.ok(completeSpy.notCalled, 'complete event never emitted');
t.ok(errorSpy.calledOnce, 'error event emitted once');
});
});
await t.test('start', (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: () => new Promise((resolve) => setTimeout(resolve, 100))
});
group.on('complete', ({name}) => {
group.stop();
t.is(name, 'foo', 'cron is runned');
t.end();
});
group.start();
});
await t.test('stop', async (t) => {
await t.test('without running jobs', async (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: noop
});
t.notOk(await group.stop(), 'stopped');
});
await t.test('wait untill all completed', async (t) => {
const group = new CronGroup();
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: () => new Promise((resolve) => setTimeout(resolve, 100))
});
group.add({
name: 'bar',
schedule: '* * * * * *',
worker: () => new Promise((resolve) => setTimeout(resolve, 200))
});
group.add({
name: 'baz',
schedule: '* * * * * *',
worker: () => new Promise((resolve, reject) => setTimeout(reject, 300))
});
const completeSpy = sinon.spy();
const errorSpy = sinon.spy();
group.on('complete', completeSpy);
group.on('error', errorSpy);
group.run('foo');
group.run('bar');
group.run('baz');
await group.stop();
t.ok(completeSpy.calledTwice, 'complete emitted twice');
t.ok(errorSpy.calledOnce, 'error emitted once');
t.is(group.listenerCount('complete'), 1, 'check "complete" listeners count');
t.is(group.listenerCount('error'), 1, 'check "error" listeners count');
});
});
});