perf: avoid branching on pool type
[poolifier.git] / tests / test-utils.js
CommitLineData
2eb14889 1const { TaskFunctions } = require('./test-types.js')
6db75ad9 2
bac873bd 3const waitWorkerEvents = async (pool, workerEvent, numberOfEventsToWait) => {
cde5b54e 4 return await new Promise(resolve => {
bac873bd
JB
5 let events = 0
6 if (numberOfEventsToWait === 0) {
7 resolve(events)
d41a44de 8 return
bac873bd
JB
9 }
10 for (const workerNode of pool.workerNodes) {
11 workerNode.worker.on(workerEvent, () => {
4f0b85b3
JB
12 ++events
13 if (events === numberOfEventsToWait) {
14 resolve(events)
15 }
16 })
bac873bd
JB
17 }
18 })
19}
20
21const waitPoolEvents = async (pool, poolEvent, numberOfEventsToWait) => {
cde5b54e 22 return await new Promise(resolve => {
bac873bd
JB
23 let events = 0
24 if (numberOfEventsToWait === 0) {
25 resolve(events)
d41a44de 26 return
bac873bd 27 }
393e2d28 28 pool.emitter?.on(poolEvent, () => {
bac873bd
JB
29 ++events
30 if (events === numberOfEventsToWait) {
31 resolve(events)
32 }
4f0b85b3 33 })
bac873bd
JB
34 })
35}
4f0b85b3 36
041dc05b 37const sleep = async ms => {
cde5b54e 38 return await new Promise(resolve => setTimeout(resolve, ms))
bac873bd 39}
bdacc2d2 40
dbca3be9 41const sleepTaskFunction = async (
bac873bd
JB
42 data,
43 ms,
44 rejection = false,
45 rejectionMessage = ''
46) => {
cde5b54e 47 return await new Promise((resolve, reject) => {
bac873bd
JB
48 setTimeout(
49 () =>
50 rejection === true
51 ? reject(new Error(rejectionMessage))
52 : resolve(data),
53 ms
54 )
55 })
56}
bdacc2d2 57
bac873bd
JB
58const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => {
59 if (max < min || max < 0 || min < 0) {
60 throw new RangeError('Invalid interval')
f931db5c 61 }
bac873bd
JB
62 max = Math.floor(max)
63 if (min != null && min !== 0) {
64 min = Math.ceil(min)
65 return Math.floor(Math.random() * (max - min + 1)) + min
66 }
67 return Math.floor(Math.random() * (max + 1))
68}
f931db5c 69
041dc05b 70const jsonIntegerSerialization = n => {
bac873bd
JB
71 for (let i = 0; i < n; i++) {
72 const o = {
73 a: i
bdacc2d2 74 }
bac873bd 75 JSON.stringify(o)
bdacc2d2 76 }
30b963d4 77 return { ok: 1 }
bac873bd 78}
bdacc2d2 79
bac873bd
JB
80/**
81 * Intentionally inefficient implementation.
82 * @param {number} n - The number of fibonacci numbers to generate.
83 * @returns {number} - The nth fibonacci number.
84 */
041dc05b 85const fibonacci = n => {
bac873bd
JB
86 if (n <= 1) return n
87 return fibonacci(n - 1) + fibonacci(n - 2)
88}
bdacc2d2 89
bac873bd
JB
90/**
91 * Intentionally inefficient implementation.
92 * @param {number} n - The number to calculate the factorial of.
93 * @returns {number} - The factorial of n.
94 */
041dc05b 95const factorial = n => {
bac873bd
JB
96 if (n === 0) {
97 return 1
bdacc2d2 98 }
bac873bd
JB
99 return factorial(n - 1) * n
100}
6db75ad9 101
041dc05b 102const executeTaskFunction = data => {
bac873bd 103 switch (data.function) {
dbca3be9 104 case TaskFunctions.jsonIntegerSerialization:
bac873bd 105 return jsonIntegerSerialization(data.n || 100)
dbca3be9 106 case TaskFunctions.fibonacci:
bac873bd 107 return fibonacci(data.n || 25)
dbca3be9 108 case TaskFunctions.factorial:
bac873bd
JB
109 return factorial(data.n || 100)
110 default:
111 throw new Error('Unknown worker function')
6db75ad9 112 }
85a3f8a7
APA
113}
114
bac873bd 115module.exports = {
dbca3be9 116 executeTaskFunction,
bac873bd
JB
117 factorial,
118 fibonacci,
119 generateRandomInteger,
120 jsonIntegerSerialization,
121 sleep,
dbca3be9 122 sleepTaskFunction,
4262afeb
JB
123 waitPoolEvents,
124 waitWorkerEvents
bac873bd 125}