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(
7 './tests/worker-files/cluster/testWorker.js',
9 errorHandler
: e
=> console
.error(e
)
12 const emptyPool
= new FixedClusterPool(
14 './tests/worker-files/cluster/emptyWorker.js'
16 const echoPool
= new FixedClusterPool(
18 './tests/worker-files/cluster/echoWorker.js'
20 const errorPool
= new FixedClusterPool(
22 './tests/worker-files/cluster/errorWorker.js',
24 errorHandler
: e
=> console
.error(e
)
27 const asyncErrorPool
= new FixedClusterPool(
29 './tests/worker-files/cluster/asyncErrorWorker.js',
31 onlineHandler
: () => console
.log('worker is online')
34 const asyncPool
= new FixedClusterPool(
36 './tests/worker-files/cluster/asyncWorker.js'
39 describe('Fixed cluster pool test suite', () => {
40 after('Destroy all pools', async () => {
41 // We need to clean up the resources after our test
42 await echoPool
.destroy()
43 await asyncPool
.destroy()
44 await errorPool
.destroy()
45 await asyncErrorPool
.destroy()
46 await emptyPool
.destroy()
49 it('Choose worker round robin test', async () => {
50 const results
= new Set()
51 for (let i
= 0; i
< numberOfWorkers
; i
++) {
52 results
.add(pool
.chooseWorker().id
)
54 expect(results
.size
).toBe(numberOfWorkers
)
57 it('Verify that the function is executed in a worker cluster', async () => {
58 const result
= await pool
.execute({ test
: 'test' })
59 expect(result
).toBeDefined()
60 expect(result
).toBeFalsy()
63 it('Verify that is possible to invoke the execute method without input', async () => {
64 const result
= await pool
.execute()
65 expect(result
).toBeDefined()
66 expect(result
).toBeFalsy()
69 it('Verify that is possible to have a worker that return undefined', async () => {
70 const result
= await emptyPool
.execute()
71 expect(result
).toBeFalsy()
74 it('Verify that data are sent to the worker correctly', async () => {
75 const data
= { f
: 10 }
76 const result
= await echoPool
.execute(data
)
77 expect(result
).toBeTruthy()
78 expect(result
.f
).toBe(data
.f
)
81 it('Verify that error handling is working properly:sync', async () => {
82 const data
= { f
: 10 }
85 await errorPool
.execute(data
)
89 expect(inError
).toBeDefined()
90 expect(typeof inError
=== 'string').toBeTruthy()
91 expect(inError
).toBe('Error Message from ClusterWorker')
94 it('Verify that error handling is working properly:async', async () => {
95 const data
= { f
: 10 }
98 await asyncErrorPool
.execute(data
)
102 expect(inError
).toBeDefined()
103 expect(typeof inError
=== 'string').toBeTruthy()
104 expect(inError
).toBe('Error Message from ClusterWorker:async')
107 it('Verify that async function is working properly', async () => {
108 const data
= { f
: 10 }
109 const startTime
= new Date().getTime()
110 const result
= await asyncPool
.execute(data
)
111 const usedTime
= new Date().getTime() - startTime
112 expect(result
).toBeTruthy()
113 expect(result
.f
).toBe(data
.f
)
114 expect(usedTime
).toBeGreaterThanOrEqual(2000)
117 it('Shutdown test', async () => {
118 const exitPromise
= TestUtils
.waitExits(pool
, numberOfWorkers
)
120 const res
= await exitPromise
121 expect(res
).toBe(numberOfWorkers
)
124 it('Should work even without opts in input', async () => {
125 const pool1
= new FixedClusterPool(
127 './tests/worker-files/cluster/testWorker.js'
129 const res
= await pool1
.execute({ test
: 'test' })
130 expect(res
).toBeFalsy()
131 // We need to clean up the resources after our test
132 await pool1
.destroy()
135 it('Verify that a pool with zero worker fails', async () => {
138 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
139 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))