51501df0350dde87d73be94cc4ddebb7be66cee2
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
)
12 const emptyPool
= new FixedClusterPool(
14 './tests/worker/cluster/emptyWorker.js'
16 const echoPool
= new FixedClusterPool(1, './tests/worker/cluster/echoWorker.js')
17 const errorPool
= new FixedClusterPool(
19 './tests/worker/cluster/errorWorker.js',
21 errorHandler
: e
=> console
.error(e
)
25 const asyncErrorPool
= new FixedClusterPool(
27 './tests/worker/cluster/asyncErrorWorker.js',
29 onlineHandler
: () => console
.log('worker is online')
32 const asyncPool
= new FixedClusterPool(
34 './tests/worker/cluster/asyncWorker.js',
40 describe('Fixed cluster pool test suite ', () => {
41 it('Choose worker round robin test', async () => {
42 const results
= new Set()
43 for (let i
= 0; i
< numberOfWorkers
; i
++) {
44 results
.add(pool
.chooseWorker().id
)
46 expect(results
.size
).toBe(numberOfWorkers
)
49 it('Verify that the function is executed in a worker cluster', async () => {
50 const result
= await pool
.execute({ test
: 'test' })
51 expect(result
).toBeDefined()
52 expect(result
).toBeFalsy()
55 it('Verify that is possible to invoke the execute method without input', async () => {
56 const result
= await pool
.execute()
57 expect(result
).toBeDefined()
58 expect(result
).toBeFalsy()
61 it('Verify that is possible to have a worker that return undefined', async () => {
62 const result
= await emptyPool
.execute()
63 expect(result
).toBeFalsy()
66 it('Verify that data are sent to the worker correctly', async () => {
67 const data
= { f
: 10 }
68 const result
= await echoPool
.execute(data
)
69 expect(result
).toBeTruthy()
70 expect(result
.f
).toBe(data
.f
)
73 it('Verify that error handling is working properly:sync', async () => {
74 const data
= { f
: 10 }
77 await errorPool
.execute(data
)
81 expect(inError
).toBeDefined()
82 expect(typeof inError
=== 'string').toBeTruthy()
83 expect(inError
).toBe('Error Message from ClusterWorker')
86 it('Verify that error handling is working properly:async', async () => {
87 const data
= { f
: 10 }
90 await asyncErrorPool
.execute(data
)
94 expect(inError
).toBeDefined()
95 expect(typeof inError
=== 'string').toBeTruthy()
96 expect(inError
).toBe('Error Message from ClusterWorker:async')
99 it('Verify that async function is working properly', async () => {
100 const data
= { f
: 10 }
101 const startTime
= new Date().getTime()
102 const result
= await asyncPool
.execute(data
)
103 const usedTime
= new Date().getTime() - startTime
104 expect(result
).toBeTruthy()
105 expect(result
.f
).toBe(data
.f
)
106 expect(usedTime
).toBeGreaterThanOrEqual(2000)
109 it('Verify that maxTasks is set properly', async () => {
110 const worker
= asyncPool
.chooseWorker()
111 expect(worker
.getMaxListeners()).toBe(maxTasks
)
114 it('Shutdown test', async () => {
115 let closedWorkers
= 0
116 pool
.workers
.forEach(w
=> {
122 await
new Promise(resolve
=> setTimeout(resolve
, 200))
123 expect(closedWorkers
).toBe(numberOfWorkers
)
126 it('Validations test', () => {
129 const pool1
= new FixedClusterPool()
134 expect(error
).toBeTruthy()
135 expect(error
.message
).toBeTruthy()
138 it('Should work even without opts in input', async () => {
139 const pool1
= new FixedClusterPool(
141 './tests/worker/cluster/testWorker.js'
143 const res
= await pool1
.execute({ test
: 'test' })
144 expect(res
).toBeFalsy()