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('Choose worker round robin test', async () => {
53 const results
= new Set()
54 for (let i
= 0; i
< numberOfWorkers
; i
++) {
55 results
.add(pool
.chooseWorker().id
)
57 expect(results
.size
).toBe(numberOfWorkers
)
60 it('Verify that the function is executed in a worker cluster', async () => {
61 let result
= await pool
.execute({
62 function: WorkerFunctions
.fibonacci
64 expect(result
).toBe(false)
65 result
= await pool
.execute({
66 function: WorkerFunctions
.factorial
68 expect(result
).toBe(false)
71 it('Verify that is possible to invoke the execute method without input', async () => {
72 const result
= await pool
.execute()
73 expect(result
).toBe(false)
76 it('Verify that busy event is emitted', async () => {
78 pool
.emitter
.on('busy', () => poolBusy
++)
79 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
82 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
83 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
84 expect(poolBusy
).toBe(numberOfWorkers
+ 1)
87 it('Verify that is possible to have a worker that return undefined', async () => {
88 const result
= await emptyPool
.execute()
89 expect(result
).toBeUndefined()
92 it('Verify that data are sent to the worker correctly', async () => {
93 const data
= { f
: 10 }
94 const result
= await echoPool
.execute(data
)
95 expect(result
).toStrictEqual(data
)
98 it('Verify that error handling is working properly:sync', async () => {
99 const data
= { f
: 10 }
102 await errorPool
.execute(data
)
106 expect(inError
).toBeDefined()
107 expect(typeof inError
=== 'string').toBe(true)
108 expect(inError
).toBe('Error Message from ClusterWorker')
111 it('Verify that error handling is working properly:async', async () => {
112 const data
= { f
: 10 }
115 await asyncErrorPool
.execute(data
)
119 expect(inError
).toBeDefined()
120 expect(typeof inError
=== 'string').toBe(true)
121 expect(inError
).toBe('Error Message from ClusterWorker:async')
124 it('Verify that async function is working properly', async () => {
125 const data
= { f
: 10 }
126 const startTime
= Date
.now()
127 const result
= await asyncPool
.execute(data
)
128 const usedTime
= Date
.now() - startTime
129 expect(result
).toStrictEqual(data
)
130 expect(usedTime
).toBeGreaterThanOrEqual(2000)
133 it('Shutdown test', async () => {
134 const exitPromise
= TestUtils
.waitExits(pool
, numberOfWorkers
)
136 const numberOfExitEvents
= await exitPromise
137 expect(numberOfExitEvents
).toBe(numberOfWorkers
)
140 it('Verify that cluster pool options are checked', async () => {
141 const workerFilePath
= './tests/worker-files/cluster/testWorker.js'
142 let pool1
= new FixedClusterPool(numberOfWorkers
, workerFilePath
)
143 expect(pool1
.opts
.env
).toBeUndefined()
144 expect(pool1
.opts
.settings
).toBeUndefined()
145 await pool1
.destroy()
146 pool1
= new FixedClusterPool(numberOfWorkers
, workerFilePath
, {
147 env
: { TEST
: 'test' },
148 settings
: { args
: ['--use', 'http'], silent
: true }
150 expect(pool1
.opts
.env
).toStrictEqual({ TEST
: 'test' })
151 expect(pool1
.opts
.settings
).toStrictEqual({
152 args
: ['--use', 'http'],
155 expect({ ...pool1
.opts
.settings
, exec
: workerFilePath
}).toStrictEqual({
156 args
: ['--use', 'http'],
160 await pool1
.destroy()
163 it('Should work even without opts in input', async () => {
164 const pool1
= new FixedClusterPool(
166 './tests/worker-files/cluster/testWorker.js'
168 const res
= await pool1
.execute()
169 expect(res
).toBe(false)
170 // We need to clean up the resources after our test
171 await pool1
.destroy()
174 it('Verify that a pool with zero worker fails', async () => {
177 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
178 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))