1 const { expect
} = require('expect')
2 const { FixedClusterPool
} = require('../../../lib/index')
3 const { WorkerFunctions
} = require('../../test-types')
4 const TestUtils
= require('../../test-utils')
6 describe('Fixed cluster pool test suite', () => {
7 const numberOfWorkers
= 6
8 const pool
= new FixedClusterPool(
10 './tests/worker-files/cluster/testWorker.js',
12 errorHandler
: e
=> console
.error(e
)
15 const emptyPool
= new FixedClusterPool(
17 './tests/worker-files/cluster/emptyWorker.js',
18 { exitHandler
: () => console
.log('empty pool worker exited') }
20 const echoPool
= new FixedClusterPool(
22 './tests/worker-files/cluster/echoWorker.js'
24 const errorPool
= new FixedClusterPool(
26 './tests/worker-files/cluster/errorWorker.js',
28 errorHandler
: e
=> console
.error(e
)
31 const asyncErrorPool
= new FixedClusterPool(
33 './tests/worker-files/cluster/asyncErrorWorker.js',
35 errorHandler
: e
=> console
.error(e
)
38 const asyncPool
= new FixedClusterPool(
40 './tests/worker-files/cluster/asyncWorker.js'
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()
52 it('Verify that the function is executed in a worker cluster', async () => {
53 let result
= await pool
.execute({
54 function: WorkerFunctions
.fibonacci
56 expect(result
).toBe(false)
57 result
= await pool
.execute({
58 function: WorkerFunctions
.factorial
60 expect(result
).toBe(false)
63 it('Verify that is possible to invoke the execute method without input', async () => {
64 const result
= await pool
.execute()
65 expect(result
).toBe(false)
68 it('Verify that busy event is emitted', async () => {
70 pool
.emitter
.on('busy', () => poolBusy
++)
71 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
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)
79 it('Verify that is possible to have a worker that return undefined', async () => {
80 const result
= await emptyPool
.execute()
81 expect(result
).toBeUndefined()
84 it('Verify that data are sent to the worker correctly', async () => {
85 const data
= { f
: 10 }
86 const result
= await echoPool
.execute(data
)
87 expect(result
).toStrictEqual(data
)
90 it('Verify that error handling is working properly:sync', async () => {
91 const data
= { f
: 10 }
94 await errorPool
.execute(data
)
98 expect(inError
).toBeDefined()
99 expect(typeof inError
=== 'string').toBe(true)
100 expect(inError
).toBe('Error Message from ClusterWorker')
103 it('Verify that error handling is working properly:async', async () => {
104 const data
= { f
: 10 }
107 await asyncErrorPool
.execute(data
)
111 expect(inError
).toBeDefined()
112 expect(typeof inError
=== 'string').toBe(true)
113 expect(inError
).toBe('Error Message from ClusterWorker:async')
116 it('Verify that async function is working properly', async () => {
117 const data
= { f
: 10 }
118 const startTime
= Date
.now()
119 const result
= await asyncPool
.execute(data
)
120 const usedTime
= Date
.now() - startTime
121 expect(result
).toStrictEqual(data
)
122 expect(usedTime
).toBeGreaterThanOrEqual(2000)
125 it('Shutdown test', async () => {
126 const exitPromise
= TestUtils
.waitExits(pool
, numberOfWorkers
)
128 const numberOfExitEvents
= await exitPromise
129 expect(numberOfExitEvents
).toBe(numberOfWorkers
)
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 }
142 expect(pool1
.opts
.env
).toStrictEqual({ TEST
: 'test' })
143 expect(pool1
.opts
.settings
).toStrictEqual({
144 args
: ['--use', 'http'],
147 expect({ ...pool1
.opts
.settings
, exec
: workerFilePath
}).toStrictEqual({
148 args
: ['--use', 'http'],
152 await pool1
.destroy()
155 it('Should work even without opts in input', async () => {
156 const pool1
= new FixedClusterPool(
158 './tests/worker-files/cluster/testWorker.js'
160 const res
= await pool1
.execute()
161 expect(res
).toBe(false)
162 // We need to clean up the resources after our test
163 await pool1
.destroy()
166 it('Verify that a pool with zero worker fails', async () => {
169 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
170 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))