f4cb38b23cb1ca63c5c6e17109af45d59f66dc0d
1 const { WorkerFunctions
} = require('./test-types')
4 static async
waitExits (pool
, numberOfExitEventsToWait
) {
6 return new Promise(resolve
=> {
7 pool
.workers
.forEach(w
=> {
10 if (exitEvents
=== numberOfExitEventsToWait
) {
18 static async
sleep (ms
) {
19 return new Promise(resolve
=> setTimeout(resolve
, ms
))
22 static async
sleepWorkerFunction (
28 return new Promise((resolve
, reject
) => {
32 ? reject(new Error(rejectionMessage
))
39 static generateRandomInteger (max
= Number
.MAX_SAFE_INTEGER
, min
= 0) {
41 throw new RangeError('Invalid interval')
44 if (min
!= null && min
!== 0) {
45 if (max
< min
|| min
< 0) {
46 throw new RangeError('Invalid interval')
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.
66 * @param {number} n - The number of fibonacci numbers to generate.
67 * @returns {number} - The nth fibonacci number.
69 static fibonacci (n
) {
71 return TestUtils
.fibonacci(n
- 1) + TestUtils
.fibonacci(n
- 2)
75 * Intentionally inefficient implementation.
77 * @param {number} n - The number to calculate the factorial of.
78 * @returns {number} - The factorial of n.
80 static factorial (n
) {
84 return TestUtils
.factorial(n
- 1) * n
88 static executeWorkerFunction (data
) {
89 switch (data
.function) {
90 case WorkerFunctions
.jsonIntegerSerialization
:
91 return TestUtils
.jsonIntegerSerialization(data
.n
|| 100)
92 case WorkerFunctions
.fibonacci
:
93 return TestUtils
.fibonacci(data
.n
|| 25)
94 case WorkerFunctions
.factorial
:
95 return TestUtils
.factorial(data
.n
|| 100)
97 throw new Error('Unknown worker function')
102 module
.exports
= TestUtils