1 const { WorkerFunctions
} = require('./test-types')
4 static async
waitWorkerExits (pool
, numberOfExitEventsToWait
) {
5 return new Promise(resolve
=> {
7 if (numberOfExitEventsToWait
=== 0) {
10 for (const workerNode
of pool
.workerNodes
) {
11 workerNode
.worker
.on('exit', () => {
13 if (exitEvents
=== numberOfExitEventsToWait
) {
21 static async
waitPoolEvents (pool
, poolEvent
, numberOfEventsToWait
) {
22 return new Promise(resolve
=> {
24 if (numberOfEventsToWait
=== 0) {
27 pool
.emitter
.on(poolEvent
, () => {
29 if (events
=== numberOfEventsToWait
) {
36 static async
sleep (ms
) {
37 return new Promise(resolve
=> setTimeout(resolve
, ms
))
40 static async
sleepWorkerFunction (
46 return new Promise((resolve
, reject
) => {
50 ? reject(new Error(rejectionMessage
))
57 static generateRandomInteger (max
= Number
.MAX_SAFE_INTEGER
, min
= 0) {
58 if (max
< min
|| max
< 0 || min
< 0) {
59 throw new RangeError('Invalid interval')
62 if (min
!= null && min
!== 0) {
64 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
66 return Math
.floor(Math
.random() * (max
+ 1))
69 static jsonIntegerSerialization (n
) {
70 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 static fibonacci (n
) {
85 return TestUtils
.fibonacci(n
- 1) + TestUtils
.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 static factorial (n
) {
97 return TestUtils
.factorial(n
- 1) * n
100 static executeWorkerFunction (data
) {
101 switch (data
.function) {
102 case WorkerFunctions
.jsonIntegerSerialization
:
103 return TestUtils
.jsonIntegerSerialization(data
.n
|| 100)
104 case WorkerFunctions
.fibonacci
:
105 return TestUtils
.fibonacci(data
.n
|| 25)
106 case WorkerFunctions
.factorial
:
107 return TestUtils
.factorial(data
.n
|| 100)
109 throw new Error('Unknown worker function')
114 module
.exports
= TestUtils