Commit | Line | Data |
---|---|---|
dbca3be9 | 1 | const { TaskFunctions } = require('./test-types') |
6db75ad9 | 2 | |
bac873bd | 3 | const waitWorkerEvents = async (pool, workerEvent, numberOfEventsToWait) => { |
8ebe6c30 | 4 | return new Promise((resolve) => { |
bac873bd JB |
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) => { | |
8ebe6c30 | 21 | return new Promise((resolve) => { |
bac873bd JB |
22 | let events = 0 |
23 | if (numberOfEventsToWait === 0) { | |
24 | resolve(events) | |
25 | } | |
393e2d28 | 26 | pool.emitter?.on(poolEvent, () => { |
bac873bd JB |
27 | ++events |
28 | if (events === numberOfEventsToWait) { | |
29 | resolve(events) | |
30 | } | |
4f0b85b3 | 31 | }) |
bac873bd JB |
32 | }) |
33 | } | |
4f0b85b3 | 34 | |
8ebe6c30 JB |
35 | const sleep = async (ms) => { |
36 | return new Promise((resolve) => setTimeout(resolve, ms)) | |
bac873bd | 37 | } |
bdacc2d2 | 38 | |
dbca3be9 | 39 | const sleepTaskFunction = async ( |
bac873bd JB |
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 | |
8ebe6c30 | 68 | const jsonIntegerSerialization = (n) => { |
bac873bd JB |
69 | for (let i = 0; i < n; i++) { |
70 | const o = { | |
71 | a: i | |
bdacc2d2 | 72 | } |
bac873bd | 73 | JSON.stringify(o) |
bdacc2d2 | 74 | } |
30b963d4 | 75 | return { ok: 1 } |
bac873bd | 76 | } |
bdacc2d2 | 77 | |
bac873bd JB |
78 | /** |
79 | * Intentionally inefficient implementation. | |
80 | * @param {number} n - The number of fibonacci numbers to generate. | |
81 | * @returns {number} - The nth fibonacci number. | |
82 | */ | |
8ebe6c30 | 83 | const fibonacci = (n) => { |
bac873bd JB |
84 | if (n <= 1) return n |
85 | return fibonacci(n - 1) + fibonacci(n - 2) | |
86 | } | |
bdacc2d2 | 87 | |
bac873bd JB |
88 | /** |
89 | * Intentionally inefficient implementation. | |
90 | * @param {number} n - The number to calculate the factorial of. | |
91 | * @returns {number} - The factorial of n. | |
92 | */ | |
8ebe6c30 | 93 | const factorial = (n) => { |
bac873bd JB |
94 | if (n === 0) { |
95 | return 1 | |
bdacc2d2 | 96 | } |
bac873bd JB |
97 | return factorial(n - 1) * n |
98 | } | |
6db75ad9 | 99 | |
8ebe6c30 | 100 | const executeTaskFunction = (data) => { |
bac873bd | 101 | switch (data.function) { |
dbca3be9 | 102 | case TaskFunctions.jsonIntegerSerialization: |
bac873bd | 103 | return jsonIntegerSerialization(data.n || 100) |
dbca3be9 | 104 | case TaskFunctions.fibonacci: |
bac873bd | 105 | return fibonacci(data.n || 25) |
dbca3be9 | 106 | case TaskFunctions.factorial: |
bac873bd JB |
107 | return factorial(data.n || 100) |
108 | default: | |
109 | throw new Error('Unknown worker function') | |
6db75ad9 | 110 | } |
85a3f8a7 APA |
111 | } |
112 | ||
bac873bd | 113 | module.exports = { |
dbca3be9 | 114 | executeTaskFunction, |
bac873bd JB |
115 | factorial, |
116 | fibonacci, | |
117 | generateRandomInteger, | |
118 | jsonIntegerSerialization, | |
119 | sleep, | |
dbca3be9 | 120 | sleepTaskFunction, |
4262afeb JB |
121 | waitPoolEvents, |
122 | waitWorkerEvents | |
bac873bd | 123 | } |