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
++) {
78 * Intentionally inefficient implementation.
79 * @param {number} n - The number of fibonacci numbers to generate.
80 * @returns {number} - The nth fibonacci number.
82 const fibonacci
= n
=> {
84 return fibonacci(n
- 1) + fibonacci(n
- 2)
88 * Intentionally inefficient implementation.
89 * @param {number} n - The number to calculate the factorial of.
90 * @returns {number} - The factorial of n.
92 const factorial
= n
=> {
96 return factorial(n
- 1) * n
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)
108 throw new Error('Unknown worker function')
113 executeWorkerFunction
,
116 generateRandomInteger
,
117 jsonIntegerSerialization
,