1 const expect
= require('expect')
2 const { FixedClusterPool
} = require('../../../lib/index')
3 const numberOfWorkers
= 10
5 const pool
= new FixedClusterPool(
7 './tests/worker/cluster/testWorker.js',
9 errorHandler
: e
=> console
.error(e
),
10 onlineHandler
: () => console
.log('worker is online')
13 const emptyPool
= new FixedClusterPool(
15 './tests/worker/cluster/emptyWorker.js'
17 const echoPool
= new FixedClusterPool(1, './tests/worker/cluster/echoWorker.js')
18 const errorPool
= new FixedClusterPool(
20 './tests/worker/cluster/errorWorker.js',
22 errorHandler
: e
=> console
.error(e
),
23 onlineHandler
: () => console
.log('worker is online')
27 const asyncErrorPool
= new FixedClusterPool(
29 './tests/worker/cluster/asyncErrorWorker.js',
31 onlineHandler
: () => console
.log('worker is online')
34 const asyncPool
= new FixedClusterPool(
36 './tests/worker/cluster/asyncWorker.js',
42 describe('Fixed cluster pool test suite ', () => {
43 it('Choose worker round robin test', async () => {
44 const results
= new Set()
45 for (let i
= 0; i
< numberOfWorkers
; i
++) {
46 results
.add(pool
.chooseWorker().id
)
48 expect(results
.size
).toBe(numberOfWorkers
)
51 it('Verify that the function is executed in a worker cluster', async () => {
52 const result
= await pool
.execute({ test
: 'test' })
53 expect(result
).toBeDefined()
54 expect(result
).toBeFalsy()
57 it('Verify that is possible to invoke the execute method without input', async () => {
58 const result
= await pool
.execute()
59 expect(result
).toBeDefined()
60 expect(result
).toBeFalsy()
63 it('Verify that is possible to have a worker that return undefined', async () => {
64 const result
= await emptyPool
.execute()
65 expect(result
).toBeFalsy()
68 it('Verify that data are sent to the worker correctly', async () => {
69 const data
= { f
: 10 }
70 const result
= await echoPool
.execute(data
)
71 expect(result
).toBeTruthy()
72 expect(result
.f
).toBe(data
.f
)
75 it('Verify that error handling is working properly:sync', async () => {
76 const data
= { f
: 10 }
79 await errorPool
.execute(data
)
83 expect(inError
).toBeDefined()
84 expect(typeof inError
=== 'string').toBeTruthy()
85 expect(inError
).toBe('Error Message from ClusterWorker')
88 it('Verify that error handling is working properly:async', async () => {
89 const data
= { f
: 10 }
92 await asyncErrorPool
.execute(data
)
96 expect(inError
).toBeDefined()
97 expect(typeof inError
=== 'string').toBeTruthy()
98 expect(inError
).toBe('Error Message from ClusterWorker:async')
101 it('Verify that async function is working properly', async () => {
102 const data
= { f
: 10 }
103 const startTime
= new Date().getTime()
104 const result
= await asyncPool
.execute(data
)
105 const usedTime
= new Date().getTime() - startTime
106 expect(result
).toBeTruthy()
107 expect(result
.f
).toBe(data
.f
)
108 expect(usedTime
).toBeGreaterThanOrEqual(2000)
111 it('Verify that maxTasks is set properly', async () => {
112 const worker
= asyncPool
.chooseWorker()
113 expect(worker
.getMaxListeners()).toBe(maxTasks
)
116 it('Shutdown test', async () => {
117 let closedWorkers
= 0
118 pool
.workers
.forEach(w
=> {
124 await
new Promise(resolve
=> setTimeout(resolve
, 200))
125 expect(closedWorkers
).toBe(numberOfWorkers
)
128 it('Validations test', () => {
131 const pool1
= new FixedClusterPool()
136 expect(error
).toBeTruthy()
137 expect(error
.message
).toBeTruthy()
140 it('Should work even without opts in input', async () => {
141 const pool1
= new FixedClusterPool(
143 './tests/worker/cluster/testWorker.js'
145 const res
= await pool1
.execute({ test
: 'test' })
146 expect(res
).toBeFalsy()