1 const expect
= require('expect')
2 const { FixedClusterPool
} = require('../../../lib/index')
3 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
)
28 const asyncErrorPool
= new FixedClusterPool(
30 './tests/worker-files/cluster/asyncErrorWorker.js',
32 onlineHandler
: () => console
.log('worker is online')
35 const asyncPool
= new FixedClusterPool(
37 './tests/worker-files/cluster/asyncWorker.js',
43 describe('Fixed cluster pool test suite ', () => {
44 it('Choose worker round robin test', async () => {
45 const results
= new Set()
46 for (let i
= 0; i
< numberOfWorkers
; i
++) {
47 results
.add(pool
.chooseWorker().id
)
49 expect(results
.size
).toBe(numberOfWorkers
)
52 it('Verify that the function is executed in a worker cluster', async () => {
53 const result
= await pool
.execute({ test
: 'test' })
54 expect(result
).toBeDefined()
55 expect(result
).toBeFalsy()
58 it('Verify that is possible to invoke the execute method without input', async () => {
59 const result
= await pool
.execute()
60 expect(result
).toBeDefined()
61 expect(result
).toBeFalsy()
64 it('Verify that is possible to have a worker that return undefined', async () => {
65 const result
= await emptyPool
.execute()
66 expect(result
).toBeFalsy()
69 it('Verify that data are sent to the worker correctly', async () => {
70 const data
= { f
: 10 }
71 const result
= await echoPool
.execute(data
)
72 expect(result
).toBeTruthy()
73 expect(result
.f
).toBe(data
.f
)
76 it('Verify that error handling is working properly:sync', async () => {
77 const data
= { f
: 10 }
80 await errorPool
.execute(data
)
84 expect(inError
).toBeDefined()
85 expect(typeof inError
=== 'string').toBeTruthy()
86 expect(inError
).toBe('Error Message from ClusterWorker')
89 it('Verify that error handling is working properly:async', async () => {
90 const data
= { f
: 10 }
93 await asyncErrorPool
.execute(data
)
97 expect(inError
).toBeDefined()
98 expect(typeof inError
=== 'string').toBeTruthy()
99 expect(inError
).toBe('Error Message from ClusterWorker:async')
102 it('Verify that async function is working properly', async () => {
103 const data
= { f
: 10 }
104 const startTime
= new Date().getTime()
105 const result
= await asyncPool
.execute(data
)
106 const usedTime
= new Date().getTime() - startTime
107 expect(result
).toBeTruthy()
108 expect(result
.f
).toBe(data
.f
)
109 expect(usedTime
).toBeGreaterThanOrEqual(2000)
112 it('Verify that maxTasks is set properly', async () => {
113 const worker
= asyncPool
.chooseWorker()
114 expect(worker
.getMaxListeners()).toBe(maxTasks
)
117 it('Shutdown test', async () => {
118 let closedWorkers
= 0
119 pool
.workers
.forEach(w
=> {
125 await
new Promise(resolve
=> setTimeout(resolve
, 500))
126 expect(closedWorkers
).toBe(numberOfWorkers
)
129 it('Validations test', () => {
132 const pool1
= new FixedClusterPool()
137 expect(error
).toBeTruthy()
138 expect(error
.message
).toBeTruthy()
141 it('Should work even without opts in input', async () => {
142 const pool1
= new FixedClusterPool(
144 './tests/worker-files/cluster/testWorker.js'
146 const res
= await pool1
.execute({ test
: 'test' })
147 expect(res
).toBeFalsy()