This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
test_beameutils.js
157 lines (142 loc) · 6.23 KB
/
test_beameutils.js
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
"use strict";
const assert = require('assert').strict;
const beameUtils = require('../../src/utils/BeameUtils');
const sleep = require('util').promisify(setTimeout);
describe('background jobs', () => {
const runningInterval = 15;
const name = 'test123', name2 = 'test321';
it('start/stopping job', () => {
beameUtils.startBackgroundJob(name, () => {}, 2000);
assert(beameUtils.getBackgroundJob(name) !== undefined);
assert(beameUtils.getBackgroundJob(name).called === 0);
assert(beameUtils.getBackgroundJob(name).interval === 2000);
assert(beameUtils.getBackgroundJob(name).handle);
beameUtils.stopBackgroundJob(name);
assert(beameUtils.getBackgroundJob(name) === undefined);
});
it('job runs multiple times', async () => {
beameUtils.startBackgroundJob(name,() => {}, runningInterval);
await sleep(runningInterval * 2.5);
assert(beameUtils.getBackgroundJob(name).called === 2);
beameUtils.stopBackgroundJob(name);
});
it('correct running status', async () => {
beameUtils.startBackgroundJob(name,async () => { await sleep(runningInterval)}, runningInterval);
assert(beameUtils.getBackgroundJob(name).called === 0);
await sleep(runningInterval*1.2);
assert(beameUtils.getBackgroundJob(name).called === 1);
assert(beameUtils.getBackgroundJob(name).running);
await sleep(runningInterval*1.2);
assert(!beameUtils.getBackgroundJob(name).running);
beameUtils.stopBackgroundJob(name);
assert(beameUtils.getBackgroundJob(name) === undefined);
});
it('correct storing of return value', async () => {
beameUtils.startBackgroundJob(name,async () => { return 'test_completed'}, runningInterval);
await sleep(runningInterval*1.5);
assert(beameUtils.getBackgroundJob(name).called === 1);
assert(beameUtils.getBackgroundJob(name).lastRun.result === 'test_completed');
assert(!beameUtils.getBackgroundJob(name).lastRun.error);
assert(beameUtils.getBackgroundJob(name).lastRun.timestamp);
beameUtils.stopBackgroundJob(name);
assert(beameUtils.getBackgroundJob(name) === undefined);
});
it('failing runs simple throw', async () => {
beameUtils.startBackgroundJob(name,async () => { throw 'test_error'}, runningInterval);
await sleep(runningInterval*2.5);
assert(beameUtils.getBackgroundJob(name).called === 2);
assert(beameUtils.getBackgroundJob(name).lastRun.error === 'test_error');
assert(beameUtils.getBackgroundJob(name).failed === 2);
beameUtils.stopBackgroundJob(name);
assert(beameUtils.getBackgroundJob(name) === undefined);
});
it('failing runs error throw', async () => {
beameUtils.startBackgroundJob(name,async () => { throw new Error('test_error')}, runningInterval);
await sleep(runningInterval*2.5);
assert(beameUtils.getBackgroundJob(name).called === 2);
assert(beameUtils.getBackgroundJob(name).lastRun.error.message === 'test_error');
assert(beameUtils.getBackgroundJob(name).lastRun.error.stack);
assert(beameUtils.getBackgroundJob(name).failed === 2);
beameUtils.stopBackgroundJob(name);
assert(beameUtils.getBackgroundJob(name) === undefined);
});
it('conditional fail run', async () => {
beameUtils.startBackgroundJob(name,async () => {
if(beameUtils.getBackgroundJob(name).called === 1) {return 'test_completed'}
else { throw new Error('test_error')}}, runningInterval);
await sleep(runningInterval*1.5);
assert(beameUtils.getBackgroundJob(name).called === 1);
assert(beameUtils.getBackgroundJob(name).lastRun.result === 'test_completed');
assert(!beameUtils.getBackgroundJob(name).lastRun.error);
assert(beameUtils.getBackgroundJob(name).failed === 0);
await sleep(runningInterval);
assert(beameUtils.getBackgroundJob(name).called === 2);
assert(!beameUtils.getBackgroundJob(name).lastRun.result);
assert(beameUtils.getBackgroundJob(name).lastRun.error.message === 'test_error');
assert(beameUtils.getBackgroundJob(name).failed === 1);
beameUtils.stopBackgroundJob(name);
assert(beameUtils.getBackgroundJob(name) === undefined);
});
it('can run multiple jobs simultaneously', () => {
beameUtils.startBackgroundJob(name,() => {}, 2000);
beameUtils.startBackgroundJob(name2,() => {}, 2000);
assert(beameUtils.getBackgroundJob(name) !== undefined);
assert(beameUtils.getBackgroundJob(name2) !== undefined);
beameUtils.stopBackgroundJob(name);
beameUtils.stopBackgroundJob(name2);
assert(beameUtils.getBackgroundJob(name) === undefined);
assert(beameUtils.getBackgroundJob(name2) === undefined);
});
it('can run multiple jobs simultaneously multiple times', async () => {
beameUtils.startBackgroundJob(name,() => {}, runningInterval);
beameUtils.startBackgroundJob(name2,() => {}, runningInterval);
assert(beameUtils.getBackgroundJob(name).called === 0);
assert(beameUtils.getBackgroundJob(name2).called === 0);
await sleep(runningInterval*1.5);
assert(beameUtils.getBackgroundJob(name).called === 1);
assert(beameUtils.getBackgroundJob(name2).called === 1);
await sleep(runningInterval);
assert(beameUtils.getBackgroundJob(name).called === 2);
assert(beameUtils.getBackgroundJob(name2).called === 2);
beameUtils.stopBackgroundJob(name);
await sleep(runningInterval);
assert(beameUtils.getBackgroundJob(name2).called === 3);
assert(beameUtils.getBackgroundJob(name) === undefined);
beameUtils.stopBackgroundJob(name2);
assert(beameUtils.getBackgroundJob(name2) === undefined);
});
it('adding already existing job fails', () => {
beameUtils.startBackgroundJob(name,() => {}, 2000);
try {
beameUtils.startBackgroundJob(name,() => {}, 2000);
assert.fail("Should have failed");
}
catch (e) {
assert(e && e.message, "error shouldn't be empty");
}
beameUtils.stopBackgroundJob(name);
});
it('stop non existing job fails', () => {
try {
beameUtils.stopBackgroundJob('nonexistingjob');
assert.fail("Should have failed");
}
catch (e) {
assert(e && e.message, "error shouldn't be empty");
}
});
it('double stopping fails second time', () => {
beameUtils.startBackgroundJob(name,() => {}, 2000);
beameUtils.stopBackgroundJob(name);
try {
beameUtils.stopBackgroundJob(name);
assert.fail("Should have failed");
}
catch (e) {
assert(e && e.message, "error shouldn't be empty");
}
});
it('info non existing job returns undefined', () => {
assert(beameUtils.getBackgroundJob('nonexistingjob') === undefined);
});
});