Report some code cleanups from work in progress PR
[poolifier.git] / tests / pools / cluster / fixed.test.js
1 const { expect } = require('expect')
2 const { FixedClusterPool } = require('../../../lib/index')
3 const TestUtils = require('../../test-utils')
4 const numberOfWorkers = 10
5 const pool = new FixedClusterPool(
6 numberOfWorkers,
7 './tests/worker-files/cluster/testWorker.js',
8 {
9 errorHandler: e => console.error(e)
10 }
11 )
12 const emptyPool = new FixedClusterPool(
13 1,
14 './tests/worker-files/cluster/emptyWorker.js',
15 { exitHandler: () => console.log('empty pool worker exited') }
16 )
17 const echoPool = new FixedClusterPool(
18 1,
19 './tests/worker-files/cluster/echoWorker.js'
20 )
21 const errorPool = new FixedClusterPool(
22 1,
23 './tests/worker-files/cluster/errorWorker.js',
24 {
25 errorHandler: e => console.error(e)
26 }
27 )
28 const asyncErrorPool = new FixedClusterPool(
29 1,
30 './tests/worker-files/cluster/asyncErrorWorker.js',
31 {
32 errorHandler: e => console.error(e)
33 }
34 )
35 const asyncPool = new FixedClusterPool(
36 1,
37 './tests/worker-files/cluster/asyncWorker.js'
38 )
39
40 describe('Fixed cluster pool test suite', () => {
41 after('Destroy all pools', async () => {
42 // We need to clean up the resources after our test
43 await echoPool.destroy()
44 await asyncPool.destroy()
45 await errorPool.destroy()
46 await asyncErrorPool.destroy()
47 await emptyPool.destroy()
48 })
49
50 it('Choose worker round robin test', async () => {
51 const results = new Set()
52 for (let i = 0; i < numberOfWorkers; i++) {
53 results.add(pool.chooseWorker().id)
54 }
55 expect(results.size).toBe(numberOfWorkers)
56 })
57
58 it('Verify that the function is executed in a worker cluster', async () => {
59 const result = await pool.execute({ test: 'test' })
60 expect(result).toBeDefined()
61 expect(result).toBeFalsy()
62 })
63
64 it('Verify that is possible to invoke the execute method without input', async () => {
65 const result = await pool.execute()
66 expect(result).toBeDefined()
67 expect(result).toBeFalsy()
68 })
69
70 it('Verify that busy event is emitted', async () => {
71 const promises = []
72 let poolBusy = 0
73 pool.emitter.on('busy', () => poolBusy++)
74 for (let i = 0; i < numberOfWorkers * 2; i++) {
75 promises.push(pool.execute({ test: 'test' }))
76 }
77 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
78 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
79 expect(poolBusy).toBe(numberOfWorkers + 1)
80 })
81
82 it('Verify that is possible to have a worker that return undefined', async () => {
83 const result = await emptyPool.execute()
84 expect(result).toBeFalsy()
85 })
86
87 it('Verify that data are sent to the worker correctly', async () => {
88 const data = { f: 10 }
89 const result = await echoPool.execute(data)
90 expect(result).toBeTruthy()
91 expect(result.f).toBe(data.f)
92 })
93
94 it('Verify that error handling is working properly:sync', async () => {
95 const data = { f: 10 }
96 let inError
97 try {
98 await errorPool.execute(data)
99 } catch (e) {
100 inError = e
101 }
102 expect(inError).toBeDefined()
103 expect(typeof inError === 'string').toBe(true)
104 expect(inError).toBe('Error Message from ClusterWorker')
105 })
106
107 it('Verify that error handling is working properly:async', async () => {
108 const data = { f: 10 }
109 let inError
110 try {
111 await asyncErrorPool.execute(data)
112 } catch (e) {
113 inError = e
114 }
115 expect(inError).toBeDefined()
116 expect(typeof inError === 'string').toBe(true)
117 expect(inError).toBe('Error Message from ClusterWorker:async')
118 })
119
120 it('Verify that async function is working properly', async () => {
121 const data = { f: 10 }
122 const startTime = new Date().getTime()
123 const result = await asyncPool.execute(data)
124 const usedTime = new Date().getTime() - startTime
125 expect(result).toBeTruthy()
126 expect(result.f).toBe(data.f)
127 expect(usedTime).toBeGreaterThanOrEqual(2000)
128 })
129
130 it('Shutdown test', async () => {
131 const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
132 await pool.destroy()
133 const numberOfExitEvents = await exitPromise
134 expect(numberOfExitEvents).toBe(numberOfWorkers)
135 })
136
137 it('Should work even without opts in input', async () => {
138 const pool1 = new FixedClusterPool(
139 1,
140 './tests/worker-files/cluster/testWorker.js'
141 )
142 const res = await pool1.execute({ test: 'test' })
143 expect(res).toBeFalsy()
144 // We need to clean up the resources after our test
145 await pool1.destroy()
146 })
147
148 it('Verify that a pool with zero worker fails', async () => {
149 expect(
150 () =>
151 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
152 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
153 })
154 })