Add eslint report to sonar
[poolifier.git] / tests / test-utils.js
1 class TestUtils {
2 static async waitExits (pool, numberOfExitEventsToWait) {
3 let exitEvents = 0
4 return new Promise(resolve => {
5 pool.workers.forEach(w => {
6 w.on('exit', () => {
7 exitEvents++
8 if (exitEvents === numberOfExitEventsToWait) {
9 resolve(exitEvents)
10 }
11 })
12 })
13 })
14 }
15
16 static async sleep (ms) {
17 return new Promise(resolve => setTimeout(resolve, ms))
18 }
19
20 static async workerSleepFunction (
21 data,
22 ms,
23 rejection = false,
24 rejectionMessage = ''
25 ) {
26 return new Promise((resolve, reject) => {
27 setTimeout(
28 () =>
29 rejection === true
30 ? reject(new Error(rejectionMessage))
31 : resolve(data),
32 ms
33 )
34 })
35 }
36
37 static jsonIntegerSerialization (n) {
38 for (let i = 0; i < n; i++) {
39 const o = {
40 a: i
41 }
42 JSON.stringify(o)
43 }
44 }
45
46 /**
47 * Intentionally inefficient implementation.
48 *
49 * @param {number} n - The number of fibonacci numbers to generate.
50 * @returns {number} - The nth fibonacci number.
51 */
52 static fibonacci (n) {
53 if (n <= 1) return 1
54 return TestUtils.fibonacci(n - 1) + TestUtils.fibonacci(n - 2)
55 }
56
57 /**
58 * Intentionally inefficient implementation.
59 *
60 * @param {number} n - The number to calculate the factorial of.
61 * @returns {number} - The factorial of n.
62 */
63 static factorial (n) {
64 if (n === 0) {
65 return 1
66 } else {
67 return TestUtils.factorial(n - 1) * n
68 }
69 }
70 }
71
72 module.exports = TestUtils