build(deps): bump poolifier
[poolifier.git] / tests / test-utils.cjs
CommitLineData
d35e5717 1const { TaskFunctions } = require('./test-types.cjs')
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
041dc05b 58const jsonIntegerSerialization = n => {
bac873bd
JB
59 for (let i = 0; i < n; i++) {
60 const o = {
61 a: i
bdacc2d2 62 }
bac873bd 63 JSON.stringify(o)
bdacc2d2 64 }
30b963d4 65 return { ok: 1 }
bac873bd 66}
bdacc2d2 67
bac873bd 68/**
bac873bd
JB
69 * @param {number} n - The number of fibonacci numbers to generate.
70 * @returns {number} - The nth fibonacci number.
71 */
041dc05b 72const fibonacci = n => {
66f0c14c
JB
73 let current = 1
74 let previous = 0
75 while (--n) {
76 const tmp = current
77 current += previous
78 previous = tmp
79 }
80 return current
bac873bd 81}
bdacc2d2 82
bac873bd 83/**
bac873bd
JB
84 * @param {number} n - The number to calculate the factorial of.
85 * @returns {number} - The factorial of n.
86 */
041dc05b 87const factorial = n => {
66f0c14c 88 if (n === 0 || n === 1) {
bac873bd 89 return 1
66f0c14c
JB
90 } else {
91 let factorial = 1
92 for (let i = 1; i <= n; i++) {
93 factorial *= i
94 }
95 return factorial
bdacc2d2 96 }
bac873bd 97}
6db75ad9 98
041dc05b 99const executeTaskFunction = data => {
bac873bd 100 switch (data.function) {
dbca3be9 101 case TaskFunctions.jsonIntegerSerialization:
bac873bd 102 return jsonIntegerSerialization(data.n || 100)
dbca3be9 103 case TaskFunctions.fibonacci:
66f0c14c 104 return fibonacci(data.n || 100)
dbca3be9 105 case TaskFunctions.factorial:
bac873bd
JB
106 return factorial(data.n || 100)
107 default:
108 throw new Error('Unknown worker function')
6db75ad9 109 }
85a3f8a7
APA
110}
111
bac873bd 112module.exports = {
dbca3be9 113 executeTaskFunction,
bac873bd
JB
114 factorial,
115 fibonacci,
bac873bd
JB
116 jsonIntegerSerialization,
117 sleep,
dbca3be9 118 sleepTaskFunction,
4262afeb
JB
119 waitPoolEvents,
120 waitWorkerEvents
bac873bd 121}