refactor: add PoolEvents/PoolEvent types
[poolifier.git] / tests / pools / cluster / fixed.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
aee46736 2const { FixedClusterPool, PoolEvents } = require('../../../lib/index')
2d2e32c2 3const { WorkerFunctions } = require('../../test-types')
85a3f8a7 4const TestUtils = require('../../test-utils')
325f50bc 5
a35560ba 6describe('Fixed cluster pool test suite', () => {
e1ffb94f
JB
7 const numberOfWorkers = 6
8 const pool = new FixedClusterPool(
9 numberOfWorkers,
10 './tests/worker-files/cluster/testWorker.js',
11 {
12 errorHandler: e => console.error(e)
13 }
14 )
15 const emptyPool = new FixedClusterPool(
16 numberOfWorkers,
17 './tests/worker-files/cluster/emptyWorker.js',
18 { exitHandler: () => console.log('empty pool worker exited') }
19 )
20 const echoPool = new FixedClusterPool(
21 numberOfWorkers,
22 './tests/worker-files/cluster/echoWorker.js'
23 )
24 const errorPool = new FixedClusterPool(
25 numberOfWorkers,
26 './tests/worker-files/cluster/errorWorker.js',
27 {
28 errorHandler: e => console.error(e)
29 }
30 )
31 const asyncErrorPool = new FixedClusterPool(
32 numberOfWorkers,
33 './tests/worker-files/cluster/asyncErrorWorker.js',
34 {
35 errorHandler: e => console.error(e)
36 }
37 )
38 const asyncPool = new FixedClusterPool(
39 numberOfWorkers,
40 './tests/worker-files/cluster/asyncWorker.js'
41 )
42
8bc77620
APA
43 after('Destroy all pools', async () => {
44 // We need to clean up the resources after our test
45 await echoPool.destroy()
46 await asyncPool.destroy()
47 await errorPool.destroy()
48 await asyncErrorPool.destroy()
49 await emptyPool.destroy()
50 })
51
325f50bc 52 it('Verify that the function is executed in a worker cluster', async () => {
6db75ad9
JB
53 let result = await pool.execute({
54 function: WorkerFunctions.fibonacci
55 })
56 expect(result).toBe(false)
57 result = await pool.execute({
58 function: WorkerFunctions.factorial
59 })
60 expect(result).toBe(false)
325f50bc
S
61 })
62
63 it('Verify that is possible to invoke the execute method without input', async () => {
64 const result = await pool.execute()
6db75ad9 65 expect(result).toBe(false)
325f50bc
S
66 })
67
aee46736 68 it("Verify that 'busy' event is emitted", async () => {
7c0ba920 69 let poolBusy = 0
aee46736 70 pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
7c0ba920 71 for (let i = 0; i < numberOfWorkers * 2; i++) {
8cbb82eb 72 pool.execute()
7c0ba920 73 }
14916bf9
JB
74 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
75 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
76 expect(poolBusy).toBe(numberOfWorkers + 1)
7c0ba920
JB
77 })
78
325f50bc
S
79 it('Verify that is possible to have a worker that return undefined', async () => {
80 const result = await emptyPool.execute()
6db75ad9 81 expect(result).toBeUndefined()
325f50bc
S
82 })
83
84 it('Verify that data are sent to the worker correctly', async () => {
85 const data = { f: 10 }
86 const result = await echoPool.execute(data)
e1ffb94f 87 expect(result).toStrictEqual(data)
325f50bc
S
88 })
89
90 it('Verify that error handling is working properly:sync', async () => {
91 const data = { f: 10 }
92 let inError
93 try {
94 await errorPool.execute(data)
95 } catch (e) {
96 inError = e
97 }
98 expect(inError).toBeDefined()
8620fb25 99 expect(typeof inError === 'string').toBe(true)
325f50bc
S
100 expect(inError).toBe('Error Message from ClusterWorker')
101 })
102
103 it('Verify that error handling is working properly:async', async () => {
104 const data = { f: 10 }
105 let inError
106 try {
107 await asyncErrorPool.execute(data)
108 } catch (e) {
109 inError = e
110 }
111 expect(inError).toBeDefined()
8620fb25 112 expect(typeof inError === 'string').toBe(true)
325f50bc
S
113 expect(inError).toBe('Error Message from ClusterWorker:async')
114 })
115
116 it('Verify that async function is working properly', async () => {
117 const data = { f: 10 }
3f7eb773 118 const startTime = Date.now()
325f50bc 119 const result = await asyncPool.execute(data)
3f7eb773 120 const usedTime = Date.now() - startTime
e1ffb94f 121 expect(result).toStrictEqual(data)
325f50bc
S
122 expect(usedTime).toBeGreaterThanOrEqual(2000)
123 })
124
125 it('Shutdown test', async () => {
85a3f8a7 126 const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
45dbbb14 127 await pool.destroy()
bdacc2d2
JB
128 const numberOfExitEvents = await exitPromise
129 expect(numberOfExitEvents).toBe(numberOfWorkers)
325f50bc
S
130 })
131
1a76932b
JB
132 it('Verify that cluster pool options are checked', async () => {
133 const workerFilePath = './tests/worker-files/cluster/testWorker.js'
134 let pool1 = new FixedClusterPool(numberOfWorkers, workerFilePath)
135 expect(pool1.opts.env).toBeUndefined()
136 expect(pool1.opts.settings).toBeUndefined()
137 await pool1.destroy()
138 pool1 = new FixedClusterPool(numberOfWorkers, workerFilePath, {
139 env: { TEST: 'test' },
140 settings: { args: ['--use', 'http'], silent: true }
141 })
142 expect(pool1.opts.env).toStrictEqual({ TEST: 'test' })
143 expect(pool1.opts.settings).toStrictEqual({
144 args: ['--use', 'http'],
145 silent: true
146 })
147 expect({ ...pool1.opts.settings, exec: workerFilePath }).toStrictEqual({
148 args: ['--use', 'http'],
149 silent: true,
150 exec: workerFilePath
151 })
152 await pool1.destroy()
153 })
154
325f50bc
S
155 it('Should work even without opts in input', async () => {
156 const pool1 = new FixedClusterPool(
e1ffb94f 157 numberOfWorkers,
76b1e974 158 './tests/worker-files/cluster/testWorker.js'
325f50bc 159 )
6db75ad9
JB
160 const res = await pool1.execute()
161 expect(res).toBe(false)
8bc77620
APA
162 // We need to clean up the resources after our test
163 await pool1.destroy()
325f50bc 164 })
8d3782fa
JB
165
166 it('Verify that a pool with zero worker fails', async () => {
167 expect(
168 () =>
169 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
170 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
171 })
325f50bc 172})