1 const { WorkerFunctions
} = require('./test-types')
3 const waitWorkerEvents
= async (pool
, workerEvent
, numberOfEventsToWait
) => {
4 return new Promise(resolve
=> {
6 if (numberOfEventsToWait
=== 0) {
9 for (const workerNode
of pool
.workerNodes
) {
10 workerNode
.worker
.on(workerEvent
, () => {
12 if (events
=== numberOfEventsToWait
) {
20 const waitPoolEvents
= async (pool
, poolEvent
, numberOfEventsToWait
) => {
21 return new Promise(resolve
=> {
23 if (numberOfEventsToWait
=== 0) {
26 pool
.emitter
?.on(poolEvent
, () => {
28 if (events
=== numberOfEventsToWait
) {
35 const sleep
= async ms
=> {
36 return new Promise(resolve
=> setTimeout(resolve
, ms
))
39 const sleepWorkerFunction
= async (
45 return new Promise((resolve
, reject
) => {
49 ? reject(new Error(rejectionMessage
))
56 const generateRandomInteger
= (max
= Number
.MAX_SAFE_INTEGER
, min
= 0) => {
57 if (max
< min
|| max
< 0 || min
< 0) {
58 throw new RangeError('Invalid interval')
61 if (min
!= null && min
!== 0) {
63 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
65 return Math
.floor(Math
.random() * (max
+ 1))
68 const jsonIntegerSerialization
= n
=> {
69 for (let i
= 0; i
< n
; i
++) {
79 * Intentionally inefficient implementation.
80 * @param {number} n - The number of fibonacci numbers to generate.
81 * @returns {number} - The nth fibonacci number.
83 const fibonacci
= n
=> {
85 return fibonacci(n
- 1) + fibonacci(n
- 2)
89 * Intentionally inefficient implementation.
90 * @param {number} n - The number to calculate the factorial of.
91 * @returns {number} - The factorial of n.
93 const factorial
= n
=> {
97 return factorial(n
- 1) * n
100 const executeWorkerFunction
= data
=> {
101 switch (data
.function) {
102 case WorkerFunctions
.jsonIntegerSerialization
:
103 return jsonIntegerSerialization(data
.n
|| 100)
104 case WorkerFunctions
.fibonacci
:
105 return fibonacci(data
.n
|| 25)
106 case WorkerFunctions
.factorial
:
107 return factorial(data
.n
|| 100)
109 throw new Error('Unknown worker function')
114 executeWorkerFunction
,
117 generateRandomInteger
,
118 jsonIntegerSerialization
,