Commit | Line | Data |
---|---|---|
85a3f8a7 APA |
1 | class TestUtils { |
2 | static async waitExits (pool, numberOfExitEventsToWait) { | |
3 | let exitEvents = 0 | |
4 | return new Promise(function (resolve, reject) { | |
5 | pool.workers.forEach(w => { | |
6 | w.on('exit', () => { | |
7 | exitEvents++ | |
8 | if (exitEvents === numberOfExitEventsToWait) { | |
9 | resolve(exitEvents) | |
10 | } | |
11 | }) | |
12 | }) | |
13 | }) | |
14 | } | |
15 | ||
16 | static async sleep (ms) { | |
17 | return new Promise(resolve => setTimeout(resolve, ms)) | |
18 | } | |
bdacc2d2 JB |
19 | |
20 | static async workerSleepFunction (data, ms) { | |
cf597bc5 | 21 | return new Promise(resolve => { |
bdacc2d2 JB |
22 | setTimeout(() => resolve(data), ms) |
23 | }) | |
24 | } | |
25 | ||
26 | static jsonIntegerSerialization (n) { | |
27 | for (let i = 0; i < n; i++) { | |
28 | const o = { | |
29 | a: i | |
30 | } | |
31 | JSON.stringify(o) | |
32 | } | |
33 | } | |
34 | ||
35 | /** | |
36 | * Intentionally inefficient implementation. | |
37 | * | |
bdaf31cd | 38 | * @param {number} n |
bdacc2d2 JB |
39 | * @returns {number} |
40 | */ | |
41 | static fibonacci (n) { | |
42 | if (n <= 1) return 1 | |
43 | return TestUtils.fibonacci(n - 1) + TestUtils.fibonacci(n - 2) | |
44 | } | |
45 | ||
46 | /** | |
47 | * Intentionally inefficient implementation. | |
48 | * | |
bdaf31cd | 49 | * @param {number} n |
bdacc2d2 JB |
50 | * @returns {number} |
51 | */ | |
52 | static factorial (n) { | |
53 | if (n === 0) { | |
54 | return 1 | |
55 | } else { | |
56 | return TestUtils.factorial(n - 1) * n | |
57 | } | |
58 | } | |
85a3f8a7 APA |
59 | } |
60 | ||
61 | module.exports = TestUtils |