78218e2668c34bbfef44e680e1e9676eccf7b7cd
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 jsonIntegerSerialization (n
) {
40 for (let i
= 0; i
< n
; i
++) {
49 * Intentionally inefficient implementation.
51 * @param {number} n - The number of fibonacci numbers to generate.
52 * @returns {number} - The nth fibonacci number.
54 static fibonacci (n
) {
56 return TestUtils
.fibonacci(n
- 1) + TestUtils
.fibonacci(n
- 2)
60 * Intentionally inefficient implementation.
62 * @param {number} n - The number to calculate the factorial of.
63 * @returns {number} - The factorial of n.
65 static factorial (n
) {
69 return TestUtils
.factorial(n
- 1) * n
73 static executeWorkerFunction (data
) {
74 switch (data
.function) {
75 case WorkerFunctions
.jsonIntegerSerialization
:
76 return TestUtils
.jsonIntegerSerialization(data
.n
|| 100)
77 case WorkerFunctions
.fibonacci
:
78 return TestUtils
.fibonacci(data
.n
|| 25)
79 case WorkerFunctions
.factorial
:
80 return TestUtils
.factorial(data
.n
|| 100)
82 throw new Error('Unknown worker function')
87 module
.exports
= TestUtils