@whotracksme/reporting 5.1.17
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/.editorconfig +12 -0
- package/.eslintignore +3 -0
- package/.eslintrc +16 -0
- package/.github/workflows/release.yml +34 -0
- package/.github/workflows/tests.yml +20 -0
- package/.prettierrc +9 -0
- package/.tool-versions +1 -0
- package/CHANGELOG.md +795 -0
- package/LICENSE +373 -0
- package/README.md +1 -0
- package/communication/README.md +11 -0
- package/communication/example/index.js +43 -0
- package/communication/example/manifest.json +16 -0
- package/communication/karma.conf.cjs +19 -0
- package/communication/package.json +52 -0
- package/communication/rollup.common-config.cjs +17 -0
- package/communication/rollup.config.cjs +10 -0
- package/communication/src/encoding.js +54 -0
- package/communication/src/errors.js +84 -0
- package/communication/src/index.js +76 -0
- package/communication/src/logger.js +41 -0
- package/communication/src/padding.js +60 -0
- package/communication/src/proxied-http.js +231 -0
- package/communication/src/server-public-key-accessor.js +123 -0
- package/communication/src/timestamps.js +37 -0
- package/communication/src/trusted-clock.js +58 -0
- package/communication/src/utils.js +28 -0
- package/communication/src/zlib.js +17 -0
- package/communication/test/helpers/in-memory-database.js +43 -0
- package/communication/test/server-public-key-accessor.spec.js +135 -0
- package/communication/test/utils.spec.js +43 -0
- package/package.json +50 -0
- package/reporting/TODO.md +2 -0
- package/reporting/assets/request/bloom_filter.dat +0 -0
- package/reporting/assets/request/update.json +1 -0
- package/reporting/example/content.js +11 -0
- package/reporting/example/index.js +132 -0
- package/reporting/example/inspector/index.html +17 -0
- package/reporting/example/inspector/index.js +27 -0
- package/reporting/example/manifests/chromium.json +37 -0
- package/reporting/example/manifests/firefox.json +35 -0
- package/reporting/example/rules.json +62 -0
- package/reporting/example/setup.js +1 -0
- package/reporting/example/tests/test1.html +10 -0
- package/reporting/karma.conf.cjs +22 -0
- package/reporting/package.json +63 -0
- package/reporting/rollup.common-config.cjs +19 -0
- package/reporting/rollup.config.cjs +20 -0
- package/reporting/src/LRU.js +149 -0
- package/reporting/src/activity-estimator.js +298 -0
- package/reporting/src/alive-check.js +139 -0
- package/reporting/src/alive-message-generator.js +142 -0
- package/reporting/src/bloom-filters.js +345 -0
- package/reporting/src/country-provider.js +295 -0
- package/reporting/src/cyrb53.js +32 -0
- package/reporting/src/digest.js +33 -0
- package/reporting/src/doublefetch-page-handler.js +548 -0
- package/reporting/src/duplicate-detector.js +233 -0
- package/reporting/src/errors.js +84 -0
- package/reporting/src/event-listener-queue.js +121 -0
- package/reporting/src/fixed-size-cache.js +63 -0
- package/reporting/src/hash-detector.js +1526 -0
- package/reporting/src/html-parser.js +17 -0
- package/reporting/src/http.js +265 -0
- package/reporting/src/index.js +14 -0
- package/reporting/src/job-scheduler.js +912 -0
- package/reporting/src/logger.js +121 -0
- package/reporting/src/md5.js +334 -0
- package/reporting/src/message-sender.js +51 -0
- package/reporting/src/network.js +140 -0
- package/reporting/src/new-page-approver.js +119 -0
- package/reporting/src/page-aggregator.js +185 -0
- package/reporting/src/page-quorum-check-handler.js +200 -0
- package/reporting/src/page-structure.js +136 -0
- package/reporting/src/pagedb.js +671 -0
- package/reporting/src/pages.js +1798 -0
- package/reporting/src/patterns-updater.js +310 -0
- package/reporting/src/patterns.js +416 -0
- package/reporting/src/persisted-bitarray.js +265 -0
- package/reporting/src/persisted-hashes.js +189 -0
- package/reporting/src/quorum-checker.js +327 -0
- package/reporting/src/random.js +39 -0
- package/reporting/src/reporting.js +290 -0
- package/reporting/src/request/config.js +116 -0
- package/reporting/src/request/content-script.js +77 -0
- package/reporting/src/request/database.js +147 -0
- package/reporting/src/request/dnt.js +24 -0
- package/reporting/src/request/hash.js +101 -0
- package/reporting/src/request/index.js +874 -0
- package/reporting/src/request/page-telemetry.js +42 -0
- package/reporting/src/request/qs-whitelist2.js +247 -0
- package/reporting/src/request/resources/prob.js +601 -0
- package/reporting/src/request/steps/block-rules.js +40 -0
- package/reporting/src/request/steps/check-context.js +37 -0
- package/reporting/src/request/steps/cookie-context.js +209 -0
- package/reporting/src/request/steps/oauth-detector.js +101 -0
- package/reporting/src/request/steps/page-logger.js +177 -0
- package/reporting/src/request/steps/redirect-tagger.js +75 -0
- package/reporting/src/request/steps/subdomain-check.js +34 -0
- package/reporting/src/request/steps/token-checker/index.js +189 -0
- package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
- package/reporting/src/request/steps/token-examiner.js +182 -0
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
- package/reporting/src/request/steps/token-telemetry/index.js +234 -0
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
- package/reporting/src/request/time.js +73 -0
- package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
- package/reporting/src/request/utils/chrome-storage-map.js +282 -0
- package/reporting/src/request/utils/subject.js +37 -0
- package/reporting/src/request/utils/temp-set.js +56 -0
- package/reporting/src/request/utils/tlds.js +55 -0
- package/reporting/src/request/utils.js +41 -0
- package/reporting/src/request-reporter.js +111 -0
- package/reporting/src/sanitizer.js +519 -0
- package/reporting/src/search-extractor.js +338 -0
- package/reporting/src/self-check.js +105 -0
- package/reporting/src/seq-executor.js +43 -0
- package/reporting/src/session-storage.js +399 -0
- package/reporting/src/url-analyzer.js +161 -0
- package/reporting/src/url-cleaner.js +60 -0
- package/reporting/src/utils/pacemaker.js +279 -0
- package/reporting/src/utils/url.js +114 -0
- package/reporting/src/utils.js +345 -0
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
- package/reporting/src/webrequest-pipeline/index.js +287 -0
- package/reporting/src/webrequest-pipeline/logger.js +14 -0
- package/reporting/src/webrequest-pipeline/page-store.js +282 -0
- package/reporting/src/webrequest-pipeline/page.js +140 -0
- package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
- package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
- package/reporting/test/README.md +2 -0
- package/reporting/test/activity-estimator.spec.js +356 -0
- package/reporting/test/alive-check.spec.js +156 -0
- package/reporting/test/alive-message-generator.spec.js +186 -0
- package/reporting/test/country-provider.spec.js +263 -0
- package/reporting/test/doublefetch-page-handler.spec.js +818 -0
- package/reporting/test/duplicate-detector.spec.js +689 -0
- package/reporting/test/helpers/dom-parsers.js +41 -0
- package/reporting/test/helpers/fake-session-storage.js +69 -0
- package/reporting/test/helpers/fast-check-utils.js +28 -0
- package/reporting/test/helpers/fetch-mock.js +57 -0
- package/reporting/test/helpers/in-memory-database.js +14 -0
- package/reporting/test/helpers/memory-map.js +72 -0
- package/reporting/test/helpers/pagedb-mocks.js +41 -0
- package/reporting/test/index.js +47 -0
- package/reporting/test/job-scheduler.spec.js +691 -0
- package/reporting/test/network.spec.js +72 -0
- package/reporting/test/page-aggregator.spec.js +104 -0
- package/reporting/test/page-quorum-check-handler.spec.js +86 -0
- package/reporting/test/pagedb.spec.js +250 -0
- package/reporting/test/pages.spec.js +693 -0
- package/reporting/test/patterns-updater.spec.js +446 -0
- package/reporting/test/patterns.spec.js +751 -0
- package/reporting/test/persisted-hashes.spec.js +169 -0
- package/reporting/test/reporting.spec.js +205 -0
- package/reporting/test/request/database.spec.js +59 -0
- package/reporting/test/request/hash.spec.js +108 -0
- package/reporting/test/request/index.spec.js +378 -0
- package/reporting/test/request/qs-whitelist2.spec.js +189 -0
- package/reporting/test/request/steps/check-context.spec.js +51 -0
- package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
- package/reporting/test/request/test-pages.js +1246 -0
- package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
- package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
- package/reporting/test/request/utils/tldts.spec.js +62 -0
- package/reporting/test/request/utils.spec.js +168 -0
- package/reporting/test/sanitizer.spec.js +1087 -0
- package/reporting/test/search-extractor.spec.js +590 -0
- package/reporting/test/seq-executor.spec.js +158 -0
- package/reporting/test/session-storage.spec.js +400 -0
- package/reporting/test/setup.js +24 -0
- package/reporting/test/url-analyzer.spec.js +451 -0
- package/reporting/test/url-cleaner.spec.js +102 -0
- package/reporting/test/utils/pacemaker.spec.js +222 -0
- package/reporting/test/utils/url.spec.js +660 -0
- package/reporting/test/utils.spec.js +299 -0
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { expect, assert } from 'chai';
|
|
13
|
+
import sinon from 'sinon';
|
|
14
|
+
import * as fc from 'fast-check';
|
|
15
|
+
|
|
16
|
+
import JobScheduler from '../src/job-scheduler.js';
|
|
17
|
+
|
|
18
|
+
const SECOND = 1000;
|
|
19
|
+
const MINUTE = 60 * SECOND;
|
|
20
|
+
const HOUR = 60 * MINUTE;
|
|
21
|
+
const DAY = 24 * HOUR;
|
|
22
|
+
const MONTH = 30 * DAY;
|
|
23
|
+
const YEAR = 365 * DAY;
|
|
24
|
+
|
|
25
|
+
function mockStorage(storageKey) {
|
|
26
|
+
return {
|
|
27
|
+
async get(key) {
|
|
28
|
+
expect(key).to.equal(storageKey);
|
|
29
|
+
return this._content;
|
|
30
|
+
},
|
|
31
|
+
async set(key, obj) {
|
|
32
|
+
expect(key).to.equal(storageKey);
|
|
33
|
+
this._content = obj;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('#JobScheduler', function () {
|
|
39
|
+
const storageKey = 'some-storage-key';
|
|
40
|
+
let uut;
|
|
41
|
+
let storage;
|
|
42
|
+
let clock;
|
|
43
|
+
|
|
44
|
+
async function passesSelfChecks() {
|
|
45
|
+
const checks = await uut.selfChecks();
|
|
46
|
+
if (!checks.allPassed()) {
|
|
47
|
+
const fullReport = JSON.stringify(checks.report(), null, 2);
|
|
48
|
+
const shortReport = JSON.stringify(checks.report().log.failures, null, 2);
|
|
49
|
+
assert.fail(
|
|
50
|
+
`self checks failed:\n${fullReport}:\n\nThis is the error:\n${shortReport}`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function initMocks() {
|
|
56
|
+
clock?.restore();
|
|
57
|
+
clock = sinon.useFakeTimers(new Date('2020-01-01'));
|
|
58
|
+
storage = mockStorage(storageKey);
|
|
59
|
+
uut = newJobScheduler();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function newJobScheduler() {
|
|
63
|
+
return new JobScheduler({
|
|
64
|
+
storage,
|
|
65
|
+
storageKey,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Helper that simulate an event like a restart of the service worker/background script:
|
|
69
|
+
// it keeps the storage but purges everything that was in memory.
|
|
70
|
+
async function simulateRestart() {
|
|
71
|
+
uut.unload();
|
|
72
|
+
uut = newJobScheduler();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function tearDown() {
|
|
76
|
+
uut?.unload();
|
|
77
|
+
uut = null;
|
|
78
|
+
|
|
79
|
+
clock?.restore();
|
|
80
|
+
clock = null;
|
|
81
|
+
storage = null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
beforeEach(initMocks);
|
|
85
|
+
afterEach(tearDown);
|
|
86
|
+
|
|
87
|
+
function someJob(type = 'testjob') {
|
|
88
|
+
return { type };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function runScriptedScenario({
|
|
92
|
+
jobs,
|
|
93
|
+
events,
|
|
94
|
+
runPeriodicSelfChecks = true,
|
|
95
|
+
}) {
|
|
96
|
+
let step = -1;
|
|
97
|
+
try {
|
|
98
|
+
let handlers;
|
|
99
|
+
const installHandlers = () => {
|
|
100
|
+
handlers = {};
|
|
101
|
+
for (const { type, config = {}, handler = () => {} } of jobs) {
|
|
102
|
+
expect(type).to.be.a('string');
|
|
103
|
+
handlers[type] = handler;
|
|
104
|
+
uut.registerHandler(type, handler, config);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
installHandlers();
|
|
108
|
+
|
|
109
|
+
await uut.init();
|
|
110
|
+
for (const { times = 1, ...event } of events) {
|
|
111
|
+
step += 1;
|
|
112
|
+
for (let i = 0; i < times; i += 1) {
|
|
113
|
+
if (runPeriodicSelfChecks) {
|
|
114
|
+
await passesSelfChecks();
|
|
115
|
+
}
|
|
116
|
+
if (event.run) {
|
|
117
|
+
const [cmd, ...args] = event.run;
|
|
118
|
+
if (cmd === 'new') {
|
|
119
|
+
expect(args).to.have.a.lengthOf(1);
|
|
120
|
+
let job;
|
|
121
|
+
if (typeof args[0] === 'string') {
|
|
122
|
+
job = { type: args[0] };
|
|
123
|
+
} else {
|
|
124
|
+
job = args[0];
|
|
125
|
+
}
|
|
126
|
+
await uut.registerJob(job);
|
|
127
|
+
} else if (cmd === 'await-all') {
|
|
128
|
+
await clock.runAllAsync();
|
|
129
|
+
} else if (cmd === 'wait') {
|
|
130
|
+
expect(args).to.have.a.lengthOf(1);
|
|
131
|
+
await clock.tickAsync(args[0]);
|
|
132
|
+
} else if (cmd === 'suspend') {
|
|
133
|
+
expect(args).to.have.a.lengthOf(1);
|
|
134
|
+
clock.jump(args[0]);
|
|
135
|
+
} else if (cmd === 'process') {
|
|
136
|
+
await uut.processPendingJobs();
|
|
137
|
+
} else if (cmd === 'restart') {
|
|
138
|
+
await simulateRestart();
|
|
139
|
+
installHandlers();
|
|
140
|
+
await uut.init();
|
|
141
|
+
} else {
|
|
142
|
+
assert.fail(`Unexpected run command: ${cmd}`);
|
|
143
|
+
}
|
|
144
|
+
} else if (event.assume) {
|
|
145
|
+
if (typeof event.assume === 'function') {
|
|
146
|
+
// user-defined hook
|
|
147
|
+
event.assume();
|
|
148
|
+
} else {
|
|
149
|
+
const state = uut._describeJobs();
|
|
150
|
+
const allStats = { ...state, ...uut.stats };
|
|
151
|
+
for (const [key, value] of Object.entries(event.assume)) {
|
|
152
|
+
expect(allStats[key]).to.eql(value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
assert.fail(`Unexpected command: ${event}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
await clock.runAllAsync();
|
|
161
|
+
if (runPeriodicSelfChecks) {
|
|
162
|
+
await passesSelfChecks();
|
|
163
|
+
}
|
|
164
|
+
} catch (e) {
|
|
165
|
+
const events_ = events.map((x, i) => ({ step: i, ...x }));
|
|
166
|
+
const scenario = JSON.stringify({ jobs, events_ }, null, 2);
|
|
167
|
+
const where =
|
|
168
|
+
step === -1
|
|
169
|
+
? 'at init'
|
|
170
|
+
: `at step #${step} (${JSON.stringify(events[step])})`;
|
|
171
|
+
console.error(
|
|
172
|
+
`Scenario:\n\n---\n${scenario}\nScenario failed ${where}:\nError:`,
|
|
173
|
+
e,
|
|
174
|
+
);
|
|
175
|
+
throw e;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
describe('#init', function () {
|
|
180
|
+
it('load + unload should not fail', async function () {
|
|
181
|
+
await uut.init();
|
|
182
|
+
uut.unload();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should support multiple init and unload calls', async function () {
|
|
186
|
+
await uut.init();
|
|
187
|
+
await uut.init();
|
|
188
|
+
uut.unload();
|
|
189
|
+
uut.unload();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should support concurrent init calls', async function () {
|
|
193
|
+
const pending = [uut.init(), uut.init(), uut.init()];
|
|
194
|
+
await Promise.all([pending]);
|
|
195
|
+
uut.unload();
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe('should enforce limits', function () {
|
|
200
|
+
it('global job limit', async function () {
|
|
201
|
+
let jobRejectedEvents = 0;
|
|
202
|
+
uut.addObserver('jobRejected', () => {
|
|
203
|
+
jobRejectedEvents += 1;
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
uut.registerHandler(someJob().type, () => {});
|
|
207
|
+
expect(uut.globalJobLimit).to.be.at.least(10);
|
|
208
|
+
uut.globalJobLimit = 10; // to speed up the test
|
|
209
|
+
|
|
210
|
+
await uut.init();
|
|
211
|
+
for (let i = 0; i < uut.globalJobLimit; i += 1) {
|
|
212
|
+
await uut.registerJob(someJob());
|
|
213
|
+
expect(jobRejectedEvents).to.eql(0);
|
|
214
|
+
expect(uut.stats.jobRegistered).to.eql(i + 1);
|
|
215
|
+
expect(uut.stats.jobRejected).to.eql(0);
|
|
216
|
+
expect(uut.getTotalJobs()).to.eql(i + 1);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// now the limit is reached and jobs will be rejectec
|
|
220
|
+
await uut.registerJob(someJob());
|
|
221
|
+
expect(jobRejectedEvents).to.eql(1);
|
|
222
|
+
expect(uut.stats.jobRegistered).to.eql(uut.globalJobLimit);
|
|
223
|
+
expect(uut.stats.jobRejected).to.eql(1);
|
|
224
|
+
expect(uut.getTotalJobs()).to.eql(uut.globalJobLimit);
|
|
225
|
+
|
|
226
|
+
await uut.init();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('local job limit', async function () {
|
|
230
|
+
let jobRejectedEvents = 0;
|
|
231
|
+
uut.addObserver('jobRejected', () => {
|
|
232
|
+
jobRejectedEvents += 1;
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const maxJobs = 4;
|
|
236
|
+
const throttled = 'throttled';
|
|
237
|
+
const nonThrottled = 'non-throttled';
|
|
238
|
+
expect(uut.globalJobLimit).to.be.at.least(2 * maxJobs + 1);
|
|
239
|
+
|
|
240
|
+
uut.registerHandler(throttled, () => {}, {
|
|
241
|
+
maxJobsTotal: maxJobs,
|
|
242
|
+
});
|
|
243
|
+
uut.registerHandler(nonThrottled, () => {});
|
|
244
|
+
|
|
245
|
+
await uut.init();
|
|
246
|
+
for (let i = 0; i < maxJobs; i += 1) {
|
|
247
|
+
await uut.registerJob(someJob(throttled));
|
|
248
|
+
expect(jobRejectedEvents).to.eql(0);
|
|
249
|
+
expect(uut.stats.jobRegistered).to.eql(i + 1);
|
|
250
|
+
expect(uut.stats.jobRejected).to.eql(0);
|
|
251
|
+
expect(uut.getTotalJobs()).to.eql(i + 1);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// now the limit is reached and jobs will be rejected
|
|
255
|
+
await uut.registerJob(someJob(throttled));
|
|
256
|
+
expect(jobRejectedEvents).to.eql(1);
|
|
257
|
+
expect(uut.stats.jobRegistered).to.eql(maxJobs);
|
|
258
|
+
expect(uut.stats.jobRejected).to.eql(1);
|
|
259
|
+
expect(uut.getTotalJobs()).to.eql(maxJobs);
|
|
260
|
+
expect(uut.getTotalJobsOfType(throttled)).to.eql(maxJobs);
|
|
261
|
+
expect(uut.getTotalJobsOfType(nonThrottled)).to.eql(0);
|
|
262
|
+
|
|
263
|
+
// but must still allow to register other jobs
|
|
264
|
+
jobRejectedEvents = 0;
|
|
265
|
+
for (let i = 0; i < maxJobs + 1; i += 1) {
|
|
266
|
+
await uut.registerJob(someJob(nonThrottled));
|
|
267
|
+
}
|
|
268
|
+
expect(jobRejectedEvents).to.eql(0);
|
|
269
|
+
expect(uut.getTotalJobsOfType(nonThrottled)).to.eql(maxJobs + 1);
|
|
270
|
+
|
|
271
|
+
await uut.init();
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe('when initialized', function () {
|
|
276
|
+
it('should pass self checks', async function () {
|
|
277
|
+
await uut.init();
|
|
278
|
+
await passesSelfChecks();
|
|
279
|
+
await clock.runAllAsync();
|
|
280
|
+
await passesSelfChecks();
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('should allow to trigger processPendingJobs when the queue is empty', async function () {
|
|
284
|
+
await uut.init();
|
|
285
|
+
await uut.processPendingJobs();
|
|
286
|
+
await passesSelfChecks();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('should allow to trigger parallel processPendingJobs when the queue is empty', async function () {
|
|
290
|
+
await uut.init();
|
|
291
|
+
await Promise.all([
|
|
292
|
+
uut.processPendingJobs(),
|
|
293
|
+
uut.processPendingJobs(),
|
|
294
|
+
uut.processPendingJobs(),
|
|
295
|
+
]);
|
|
296
|
+
await passesSelfChecks();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('should allow to processPendingJobs when the queue, but jobs are registered', async function () {
|
|
300
|
+
uut.registerHandler('dummy', () => expect.fail('should never be called'));
|
|
301
|
+
await uut.init();
|
|
302
|
+
await uut.processPendingJobs();
|
|
303
|
+
await passesSelfChecks();
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should allow to run one job', async function () {
|
|
307
|
+
let numJobsExecuted = 0;
|
|
308
|
+
uut.registerHandler(someJob().type, async () => {
|
|
309
|
+
numJobsExecuted += 1;
|
|
310
|
+
});
|
|
311
|
+
await uut.init();
|
|
312
|
+
|
|
313
|
+
expect(uut._describeJobs().queueLength).to.eql(0);
|
|
314
|
+
expect(uut.stats.jobRegistered).to.eql(0);
|
|
315
|
+
expect(uut.stats.jobStarted).to.eql(0);
|
|
316
|
+
expect(uut.stats.jobSucceeded).to.eql(0);
|
|
317
|
+
expect(uut.stats.jobFailed).to.eql(0);
|
|
318
|
+
expect(uut.stats.jobRejected).to.eql(0);
|
|
319
|
+
|
|
320
|
+
await uut.registerJob(someJob());
|
|
321
|
+
expect(uut._describeJobs().queueLength).to.eql(1);
|
|
322
|
+
expect(uut.stats.jobRegistered).to.eql(1);
|
|
323
|
+
expect(uut.stats.jobStarted).to.eql(0);
|
|
324
|
+
expect(uut.stats.jobSucceeded).to.eql(0);
|
|
325
|
+
expect(uut.stats.jobFailed).to.eql(0);
|
|
326
|
+
expect(uut.stats.jobRejected).to.eql(0);
|
|
327
|
+
|
|
328
|
+
await uut.processPendingJobs();
|
|
329
|
+
expect(numJobsExecuted).to.eql(1);
|
|
330
|
+
expect(uut._describeJobs().queueLength).to.eql(0);
|
|
331
|
+
expect(uut.stats.jobRegistered).to.eql(1);
|
|
332
|
+
expect(uut.stats.jobStarted).to.eql(1);
|
|
333
|
+
expect(uut.stats.jobSucceeded).to.eql(1);
|
|
334
|
+
expect(uut.stats.jobFailed).to.eql(0);
|
|
335
|
+
expect(uut.stats.jobRejected).to.eql(0);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
describe('should allow to run multiple, different jobs of same priority', async function () {
|
|
339
|
+
for (const { types, jobsPerType } of [
|
|
340
|
+
{ types: ['testjob1', 'testjob2'], jobsPerType: 1 },
|
|
341
|
+
{ types: ['testjob1', 'testjob2'], jobsPerType: 3 },
|
|
342
|
+
{ types: ['testjob1', 'testjob2', 'testjob3'], jobsPerType: 5 },
|
|
343
|
+
]) {
|
|
344
|
+
it(`- types=${types} and ${jobsPerType} jobs per type`, async function () {
|
|
345
|
+
const jobs = [];
|
|
346
|
+
const numJobsExecuted = {};
|
|
347
|
+
|
|
348
|
+
for (const type of types) {
|
|
349
|
+
numJobsExecuted[type] = 0;
|
|
350
|
+
uut.registerHandler(type, async () => {
|
|
351
|
+
numJobsExecuted[type] += 1;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
for (let i = 0; i < jobsPerType; i += 1) {
|
|
355
|
+
for (const type of types) {
|
|
356
|
+
jobs.push(someJob(type));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
await uut.init();
|
|
361
|
+
await uut.registerJobs(jobs);
|
|
362
|
+
expect(uut._describeJobs().queueLength).to.eql(jobs.length);
|
|
363
|
+
await passesSelfChecks();
|
|
364
|
+
|
|
365
|
+
await uut.processPendingJobs();
|
|
366
|
+
for (const type of types) {
|
|
367
|
+
expect(numJobsExecuted[type]).to.eql(jobsPerType);
|
|
368
|
+
}
|
|
369
|
+
expect(uut._describeJobs().queueLength).to.eql(0);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe('should allow to run multiple, different jobs of different priorities', async function () {
|
|
376
|
+
for (const { types, jobsPerType } of [
|
|
377
|
+
{ types: ['testjob1', 'testjob2'], jobsPerType: 1 },
|
|
378
|
+
{ types: ['testjob1', 'testjob2'], jobsPerType: 3 },
|
|
379
|
+
{ types: ['testjob1', 'testjob2', 'testjob3'], jobsPerType: 5 },
|
|
380
|
+
]) {
|
|
381
|
+
it(`- types=${types} and ${jobsPerType} jobs per type`, async function () {
|
|
382
|
+
const jobs = [];
|
|
383
|
+
const realExecutionOrder = [];
|
|
384
|
+
|
|
385
|
+
const priorityMap = {
|
|
386
|
+
testjob1: 3, // should run first
|
|
387
|
+
testjob2: 2,
|
|
388
|
+
testjob3: 1,
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
for (const type of types) {
|
|
392
|
+
const config = { priority: priorityMap[type] };
|
|
393
|
+
uut.registerHandler(
|
|
394
|
+
type,
|
|
395
|
+
() => realExecutionOrder.push(type),
|
|
396
|
+
config,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
for (let i = 0; i < jobsPerType; i += 1) {
|
|
400
|
+
for (const type of types) {
|
|
401
|
+
jobs.push(someJob(type));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
await uut.init();
|
|
406
|
+
await uut.registerJobs(jobs);
|
|
407
|
+
expect(uut._describeJobs().queueLength).to.eql(jobs.length);
|
|
408
|
+
await passesSelfChecks();
|
|
409
|
+
|
|
410
|
+
await uut.processPendingJobs();
|
|
411
|
+
expect(uut._describeJobs().queueLength).to.eql(0);
|
|
412
|
+
|
|
413
|
+
const expectedExecutionOrder = jobs
|
|
414
|
+
.map(({ type }) => type)
|
|
415
|
+
.sort((x, y) => priorityMap[y] - priorityMap[x]);
|
|
416
|
+
expect(realExecutionOrder).to.eql(expectedExecutionOrder);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
describe('should allow jobs to spawn other jobs', function () {
|
|
422
|
+
it('spawning jobs of the same type', async function () {
|
|
423
|
+
const type = 'testjob';
|
|
424
|
+
let nextJobId = 0;
|
|
425
|
+
const newJob = ({ jobsToSpawn }) => {
|
|
426
|
+
const jobId = nextJobId;
|
|
427
|
+
nextJobId += 1;
|
|
428
|
+
return { type, args: { jobsToSpawn, jobId } };
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const finishedJobs = new Set();
|
|
432
|
+
const markJobAsDone = (job) => {
|
|
433
|
+
const { jobId } = job.args;
|
|
434
|
+
expect(finishedJobs).not.to.include(jobId);
|
|
435
|
+
finishedJobs.add(jobId);
|
|
436
|
+
};
|
|
437
|
+
const expectJobIsDone = (job) => {
|
|
438
|
+
expect(finishedJobs).to.include(job.args.jobId);
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
uut.registerHandler(type, async (job) => {
|
|
442
|
+
const { jobsToSpawn } = job.args;
|
|
443
|
+
markJobAsDone(job);
|
|
444
|
+
return jobsToSpawn;
|
|
445
|
+
});
|
|
446
|
+
await uut.init();
|
|
447
|
+
|
|
448
|
+
// job1 -> null (does not spawn)
|
|
449
|
+
const job1 = newJob({ jobsToSpawn: null });
|
|
450
|
+
await uut.registerJob(job1);
|
|
451
|
+
await uut.processPendingJobs();
|
|
452
|
+
expectJobIsDone(job1);
|
|
453
|
+
expect(uut.stats.jobRegistered).to.equal(1);
|
|
454
|
+
expect(uut.stats.jobSucceeded).to.eql(1);
|
|
455
|
+
|
|
456
|
+
// job2 -> undefined (does not spawn)
|
|
457
|
+
const job2 = newJob({ jobsToSpawn: undefined });
|
|
458
|
+
await uut.registerJob(job2);
|
|
459
|
+
await uut.processPendingJobs();
|
|
460
|
+
expectJobIsDone(job2);
|
|
461
|
+
expect(uut.stats.jobRegistered).to.equal(2);
|
|
462
|
+
expect(uut.stats.jobSucceeded).to.eql(2);
|
|
463
|
+
|
|
464
|
+
// job3 -> [] (does not spawn)
|
|
465
|
+
const job3 = newJob({ jobsToSpawn: [] });
|
|
466
|
+
await uut.registerJob(job3);
|
|
467
|
+
await uut.processPendingJobs();
|
|
468
|
+
expectJobIsDone(job3);
|
|
469
|
+
expect(uut.stats.jobRegistered).to.equal(3);
|
|
470
|
+
expect(uut.stats.jobSucceeded).to.eql(3);
|
|
471
|
+
|
|
472
|
+
// job4 -> job5
|
|
473
|
+
// -> job6 -> job7
|
|
474
|
+
const job7 = newJob({ jobsToSpawn: [] });
|
|
475
|
+
const job6 = newJob({ jobsToSpawn: [job7] });
|
|
476
|
+
const job5 = newJob({ jobsToSpawn: [] });
|
|
477
|
+
const job4 = newJob({ jobsToSpawn: [job5, job6] });
|
|
478
|
+
await uut.registerJob(job4);
|
|
479
|
+
await uut.processPendingJobs();
|
|
480
|
+
expectJobIsDone(job4);
|
|
481
|
+
expectJobIsDone(job5);
|
|
482
|
+
expectJobIsDone(job6);
|
|
483
|
+
expectJobIsDone(job7);
|
|
484
|
+
expect(uut.stats.jobRegistered).to.equal(7);
|
|
485
|
+
expect(uut.stats.jobSucceeded).to.eql(7);
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
describe('after an extension restart', function () {
|
|
490
|
+
it('should restore jobs', async function () {
|
|
491
|
+
await uut.init();
|
|
492
|
+
expect(uut._describeJobs().queueLength).to.eql(0);
|
|
493
|
+
|
|
494
|
+
await simulateRestart();
|
|
495
|
+
await uut.init();
|
|
496
|
+
expect(uut._describeJobs().queueLength).to.eql(0);
|
|
497
|
+
await clock.runAllAsync();
|
|
498
|
+
|
|
499
|
+
await uut.registerJob(someJob());
|
|
500
|
+
expect(uut._describeJobs().queueLength).to.eql(1);
|
|
501
|
+
|
|
502
|
+
await uut.sync();
|
|
503
|
+
await clock.runAllAsync();
|
|
504
|
+
|
|
505
|
+
await simulateRestart();
|
|
506
|
+
await uut.init();
|
|
507
|
+
await uut.sync();
|
|
508
|
+
expect(uut._describeJobs().queueLength).to.eql(1);
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
describe('[scripted scenarios]', function () {
|
|
513
|
+
describe('[cooldown]', function () {
|
|
514
|
+
it('two jobs with a one second cooldown', async function () {
|
|
515
|
+
const jobs = [{ type: 'testjob1', config: { cooldownInMs: 1000 } }];
|
|
516
|
+
const events = [
|
|
517
|
+
{ run: ['await-all'] },
|
|
518
|
+
{ run: ['new', 'testjob1'], times: 2 },
|
|
519
|
+
{ assume: { queueLength: 2 } },
|
|
520
|
+
{ run: ['process'] },
|
|
521
|
+
{ assume: { queueLength: 1 } },
|
|
522
|
+
{ run: ['process'] },
|
|
523
|
+
{ assume: { queueLength: 1 } },
|
|
524
|
+
{ run: ['wait', 2000] },
|
|
525
|
+
{ run: ['process'] },
|
|
526
|
+
{ assume: { queueLength: 0 } },
|
|
527
|
+
];
|
|
528
|
+
await runScriptedScenario({ jobs, events });
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* To guarantee that jobs will not time out.
|
|
535
|
+
*/
|
|
536
|
+
function configWithoutTimeouts() {
|
|
537
|
+
return {
|
|
538
|
+
maxWait: 1 * HOUR,
|
|
539
|
+
minTTL: 1000 * YEAR,
|
|
540
|
+
maxTTL: 1000 * YEAR,
|
|
541
|
+
minReadyIn: 0,
|
|
542
|
+
mayReadyIn: 2 * SECOND,
|
|
543
|
+
maxCooldown: 2 * SECOND,
|
|
544
|
+
maxClockJump: 2 * SECOND,
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* To guarantee that jobs will not be dropped due quotes.
|
|
550
|
+
*/
|
|
551
|
+
function configWithoutQuotas() {
|
|
552
|
+
return {
|
|
553
|
+
maxJobsPerType: 10000000,
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function arbitraryScenario({
|
|
558
|
+
minEvents = 1,
|
|
559
|
+
maxEvents = 500,
|
|
560
|
+
maxWait = 7 * DAY,
|
|
561
|
+
maxRepeat = 10, // optionally adds "times" attributes to repeatible jobs
|
|
562
|
+
maxJobTypes = 20,
|
|
563
|
+
minReadyIn = 0,
|
|
564
|
+
maxReadyIn = 1 * MONTH,
|
|
565
|
+
minTTL = 1 * SECOND,
|
|
566
|
+
maxTTL = 12 * MONTH,
|
|
567
|
+
maxJobsPerType = 10000,
|
|
568
|
+
maxCooldown = 24 * HOUR,
|
|
569
|
+
maxAutoRetriesAfterError = 100,
|
|
570
|
+
maxClockJump = 6 * MONTH, // also simulates if a machines goes to sleep
|
|
571
|
+
} = {}) {
|
|
572
|
+
const numToJobType = (x) => `testjob${x}`;
|
|
573
|
+
const arbitraryJobType = () => fc.nat(maxJobTypes - 1).map(numToJobType);
|
|
574
|
+
const arbitraryJobArgs = () => fc.array(fc.nat());
|
|
575
|
+
const arbitraryJobConfig = () => {
|
|
576
|
+
return fc.record({
|
|
577
|
+
priority: fc.option(fc.integer()),
|
|
578
|
+
ttlInMs: fc.option(fc.integer({ min: minTTL, max: maxTTL })),
|
|
579
|
+
readyIn: fc.option(
|
|
580
|
+
fc.record({
|
|
581
|
+
min: fc.nat(minReadyIn),
|
|
582
|
+
max: fc.option(fc.integer({ min: minReadyIn, max: maxReadyIn })),
|
|
583
|
+
}),
|
|
584
|
+
),
|
|
585
|
+
maxJobsTotal: fc.option(fc.nat(fc.nat(1, maxJobsPerType))),
|
|
586
|
+
cooldownInMs: fc.option(fc.nat(maxCooldown)),
|
|
587
|
+
autoRetryAfterError: fc.option(fc.boolean()),
|
|
588
|
+
maxAutoRetriesAfterError: fc.option(fc.nat(maxAutoRetriesAfterError)),
|
|
589
|
+
});
|
|
590
|
+
};
|
|
591
|
+
const arbitraryJob = () => {
|
|
592
|
+
return fc
|
|
593
|
+
.tuple(
|
|
594
|
+
arbitraryJobType(),
|
|
595
|
+
fc.option(arbitraryJobArgs()),
|
|
596
|
+
fc.option(arbitraryJobConfig()),
|
|
597
|
+
)
|
|
598
|
+
.map(([type, args, config]) => ({
|
|
599
|
+
type,
|
|
600
|
+
args,
|
|
601
|
+
config,
|
|
602
|
+
}));
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
const arbitraryTimes = () => fc.option(fc.nat(maxRepeat));
|
|
606
|
+
|
|
607
|
+
const arbitraryEvent = () => {
|
|
608
|
+
return fc.oneof(
|
|
609
|
+
// ["new", <job>]:
|
|
610
|
+
fc
|
|
611
|
+
.tuple(arbitraryJob(), arbitraryTimes())
|
|
612
|
+
.map(([job, times]) => ({ run: ['new', job], times })),
|
|
613
|
+
|
|
614
|
+
// ["await-all"]:
|
|
615
|
+
fc.constant({ run: ['await-all'] }),
|
|
616
|
+
|
|
617
|
+
// ["wait", <delay>]:
|
|
618
|
+
fc.nat(maxWait).map((delay) => ({ run: ['wait', delay] })),
|
|
619
|
+
|
|
620
|
+
// ["process"]:
|
|
621
|
+
fc.constant({ run: ['process'] }),
|
|
622
|
+
|
|
623
|
+
// ["restart"]:
|
|
624
|
+
fc.constant({ run: ['restart'] }),
|
|
625
|
+
|
|
626
|
+
// ["suspend", <clock-jump>]:
|
|
627
|
+
fc.nat(maxClockJump).map((time) => ({ run: ['suspend', time] })),
|
|
628
|
+
);
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
return fc
|
|
632
|
+
.record({
|
|
633
|
+
events: fc.array(arbitraryEvent(), {
|
|
634
|
+
minLength: minEvents,
|
|
635
|
+
maxLength: maxEvents,
|
|
636
|
+
}),
|
|
637
|
+
configs: fc.infiniteStream(arbitraryJobConfig()),
|
|
638
|
+
runPeriodicSelfChecks: fc.boolean(),
|
|
639
|
+
})
|
|
640
|
+
.map(({ events, configs, runPeriodicSelfChecks }) => {
|
|
641
|
+
const seenTypes = new Set();
|
|
642
|
+
const jobs = [];
|
|
643
|
+
for (const ev of events) {
|
|
644
|
+
if (ev.run && ev.run[0] === 'new') {
|
|
645
|
+
const { type } = ev.run[1];
|
|
646
|
+
if (!seenTypes.has(type)) {
|
|
647
|
+
seenTypes.add(type);
|
|
648
|
+
const config = configs.next().value;
|
|
649
|
+
jobs.push({ type, config });
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return {
|
|
654
|
+
jobs,
|
|
655
|
+
events,
|
|
656
|
+
runPeriodicSelfChecks,
|
|
657
|
+
};
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
describe('[property based testing]', function () {
|
|
662
|
+
it('should not throw in random scenarios', async function () {
|
|
663
|
+
this.timeout(20 * SECOND);
|
|
664
|
+
await fc.assert(
|
|
665
|
+
fc
|
|
666
|
+
.asyncProperty(arbitraryScenario(), runScriptedScenario)
|
|
667
|
+
.beforeEach(initMocks)
|
|
668
|
+
.afterEach(tearDown),
|
|
669
|
+
);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it('should eventually complete all jobs it runs consistently', async function () {
|
|
673
|
+
this.timeout(20 * SECOND);
|
|
674
|
+
await fc.assert(
|
|
675
|
+
fc
|
|
676
|
+
.asyncProperty(
|
|
677
|
+
arbitraryScenario({
|
|
678
|
+
...configWithoutTimeouts(),
|
|
679
|
+
...configWithoutQuotas(),
|
|
680
|
+
}),
|
|
681
|
+
async (scenario) => {
|
|
682
|
+
scenario.events.push({ assume: { jobExpired: 0 } });
|
|
683
|
+
await runScriptedScenario(scenario);
|
|
684
|
+
},
|
|
685
|
+
)
|
|
686
|
+
.beforeEach(initMocks)
|
|
687
|
+
.afterEach(tearDown),
|
|
688
|
+
);
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
});
|