1 const { WorkerFunctions
} = require('./test-types')
4 static async
waitExits (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
sleep (ms
) {
22 return new Promise(resolve
=> setTimeout(resolve
, ms
))
25 static async
sleepWorkerFunction (
31 return new Promise((resolve
, reject
) => {
35 ? reject(new Error(rejectionMessage
))
42 static generateRandomInteger (max
= Number
.MAX_SAFE_INTEGER
, min
= 0) {
43 if (max
< min
|| max
< 0 || min
< 0) {
44 throw new RangeError('Invalid interval')
47 if (min
!= null && min
!== 0) {
49 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
51 return Math
.floor(Math
.random() * (max
+ 1))
54 static jsonIntegerSerialization (n
) {
55 for (let i
= 0; i
< n
; i
++) {
64 * Intentionally inefficient implementation.
65 * @param {number} n - The number of fibonacci numbers to generate.
66 * @returns {number} - The nth fibonacci number.
68 static fibonacci (n
) {
70 return TestUtils
.fibonacci(n
- 1) + TestUtils
.fibonacci(n
- 2)
74 * Intentionally inefficient implementation.
75 * @param {number} n - The number to calculate the factorial of.
76 * @returns {number} - The factorial of n.
78 static factorial (n
) {
82 return TestUtils
.factorial(n
- 1) * n
85 static executeWorkerFunction (data
) {
86 switch (data
.function) {
87 case WorkerFunctions
.jsonIntegerSerialization
:
88 return TestUtils
.jsonIntegerSerialization(data
.n
|| 100)
89 case WorkerFunctions
.fibonacci
:
90 return TestUtils
.fibonacci(data
.n
|| 25)
91 case WorkerFunctions
.factorial
:
92 return TestUtils
.factorial(data
.n
|| 100)
94 throw new Error('Unknown worker function')
99 module
.exports
= TestUtils