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