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