Commit | Line | Data |
---|---|---|
2d2e32c2 | 1 | const { WorkerFunctions } = require('./test-types') |
6db75ad9 | 2 | |
85a3f8a7 | 3 | class TestUtils { |
4f0b85b3 | 4 | static async waitWorkerExits (pool, numberOfExitEventsToWait) { |
d94bee6f | 5 | return new Promise(resolve => { |
f821a841 | 6 | let exitEvents = 0 |
de4e1d69 JB |
7 | if (numberOfExitEventsToWait === 0) { |
8 | resolve(exitEvents) | |
9 | } | |
f06e48d8 JB |
10 | for (const workerNode of pool.workerNodes) { |
11 | workerNode.worker.on('exit', () => { | |
ffcbbad8 | 12 | ++exitEvents |
85a3f8a7 APA |
13 | if (exitEvents === numberOfExitEventsToWait) { |
14 | resolve(exitEvents) | |
15 | } | |
16 | }) | |
99683499 | 17 | } |
85a3f8a7 APA |
18 | }) |
19 | } | |
20 | ||
4f0b85b3 JB |
21 | static async waitPoolEvents (pool, poolEvent, numberOfEventsToWait) { |
22 | return new Promise(resolve => { | |
23 | let events = 0 | |
24 | if (numberOfEventsToWait === 0) { | |
25 | resolve(events) | |
26 | } | |
27 | pool.emitter.on(poolEvent, () => { | |
28 | ++events | |
29 | if (events === numberOfEventsToWait) { | |
30 | resolve(events) | |
31 | } | |
32 | }) | |
33 | }) | |
34 | } | |
35 | ||
85a3f8a7 APA |
36 | static async sleep (ms) { |
37 | return new Promise(resolve => setTimeout(resolve, ms)) | |
38 | } | |
bdacc2d2 | 39 | |
6db75ad9 | 40 | static async sleepWorkerFunction ( |
15d56315 JB |
41 | data, |
42 | ms, | |
43 | rejection = false, | |
44 | rejectionMessage = '' | |
45 | ) { | |
46 | return new Promise((resolve, reject) => { | |
47 | setTimeout( | |
48 | () => | |
49 | rejection === true | |
50 | ? reject(new Error(rejectionMessage)) | |
51 | : resolve(data), | |
52 | ms | |
53 | ) | |
bdacc2d2 JB |
54 | }) |
55 | } | |
56 | ||
9bc18a6d | 57 | static generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { |
548140e6 | 58 | if (max < min || max < 0 || min < 0) { |
4af5c11a JB |
59 | throw new RangeError('Invalid interval') |
60 | } | |
f931db5c | 61 | max = Math.floor(max) |
4af5c11a | 62 | if (min != null && min !== 0) { |
f931db5c JB |
63 | min = Math.ceil(min) |
64 | return Math.floor(Math.random() * (max - min + 1)) + min | |
65 | } | |
66 | return Math.floor(Math.random() * (max + 1)) | |
67 | } | |
68 | ||
bdacc2d2 JB |
69 | static jsonIntegerSerialization (n) { |
70 | for (let i = 0; i < n; i++) { | |
71 | const o = { | |
72 | a: i | |
73 | } | |
74 | JSON.stringify(o) | |
75 | } | |
76 | } | |
77 | ||
78 | /** | |
79 | * Intentionally inefficient implementation. | |
7d82d90e JB |
80 | * @param {number} n - The number of fibonacci numbers to generate. |
81 | * @returns {number} - The nth fibonacci number. | |
bdacc2d2 JB |
82 | */ |
83 | static fibonacci (n) { | |
84 | if (n <= 1) return 1 | |
85 | return TestUtils.fibonacci(n - 1) + TestUtils.fibonacci(n - 2) | |
86 | } | |
87 | ||
88 | /** | |
89 | * Intentionally inefficient implementation. | |
7d82d90e JB |
90 | * @param {number} n - The number to calculate the factorial of. |
91 | * @returns {number} - The factorial of n. | |
bdacc2d2 JB |
92 | */ |
93 | static factorial (n) { | |
94 | if (n === 0) { | |
95 | return 1 | |
bdacc2d2 | 96 | } |
0762fbb1 | 97 | return TestUtils.factorial(n - 1) * n |
bdacc2d2 | 98 | } |
6db75ad9 JB |
99 | |
100 | static executeWorkerFunction (data) { | |
101 | switch (data.function) { | |
102 | case WorkerFunctions.jsonIntegerSerialization: | |
103 | return TestUtils.jsonIntegerSerialization(data.n || 100) | |
104 | case WorkerFunctions.fibonacci: | |
105 | return TestUtils.fibonacci(data.n || 25) | |
106 | case WorkerFunctions.factorial: | |
107 | return TestUtils.factorial(data.n || 100) | |
108 | default: | |
109 | throw new Error('Unknown worker function') | |
110 | } | |
111 | } | |
85a3f8a7 APA |
112 | } |
113 | ||
114 | module.exports = TestUtils |