a3affb61c91899d187dd91e97f2693cfa7340d0d
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
, min
= 0) {
43 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
45 return Math
.floor(Math
.random() * (max
+ 1))
48 static jsonIntegerSerialization (n
) {
49 for (let i
= 0; i
< n
; i
++) {
58 * Intentionally inefficient implementation.
60 * @param {number} n - The number of fibonacci numbers to generate.
61 * @returns {number} - The nth fibonacci number.
63 static fibonacci (n
) {
65 return TestUtils
.fibonacci(n
- 1) + TestUtils
.fibonacci(n
- 2)
69 * Intentionally inefficient implementation.
71 * @param {number} n - The number to calculate the factorial of.
72 * @returns {number} - The factorial of n.
74 static factorial (n
) {
78 return TestUtils
.factorial(n
- 1) * n
82 static executeWorkerFunction (data
) {
83 switch (data
.function) {
84 case WorkerFunctions
.jsonIntegerSerialization
:
85 return TestUtils
.jsonIntegerSerialization(data
.n
|| 100)
86 case WorkerFunctions
.fibonacci
:
87 return TestUtils
.fibonacci(data
.n
|| 25)
88 case WorkerFunctions
.factorial
:
89 return TestUtils
.factorial(data
.n
|| 100)
91 throw new Error('Unknown worker function')
96 module
.exports
= TestUtils