1cbfadf5b63ad3e029b61d47e24f6d6d9a445467
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
),
10 onlineHandler
: () => console
.log('worker is online')
13 const emptyPool
= new FixedClusterPool(
15 './tests/worker-files/cluster/emptyWorker.js'
17 const echoPool
= new FixedClusterPool(
19 './tests/worker-files/cluster/echoWorker.js'
21 const errorPool
= new FixedClusterPool(
23 './tests/worker-files/cluster/errorWorker.js',
25 errorHandler
: e
=> console
.error(e
),
26 onlineHandler
: () => console
.log('worker is online')
30 const asyncErrorPool
= new FixedClusterPool(
32 './tests/worker-files/cluster/asyncErrorWorker.js',
34 onlineHandler
: () => console
.log('worker is online')
37 const asyncPool
= new FixedClusterPool(
39 './tests/worker-files/cluster/asyncWorker.js',
45 describe('Fixed cluster pool test suite ', () => {
46 it('Choose worker round robin test', async () => {
47 const results
= new Set()
48 for (let i
= 0; i
< numberOfWorkers
; i
++) {
49 results
.add(pool
.chooseWorker().id
)
51 expect(results
.size
).toBe(numberOfWorkers
)
54 it('Verify that the function is executed in a worker cluster', async () => {
55 const result
= await pool
.execute({ test
: 'test' })
56 expect(result
).toBeDefined()
57 expect(result
).toBeFalsy()
60 it('Verify that is possible to invoke the execute method without input', async () => {
61 const result
= await pool
.execute()
62 expect(result
).toBeDefined()
63 expect(result
).toBeFalsy()
66 it('Verify that is possible to have a worker that return undefined', async () => {
67 const result
= await emptyPool
.execute()
68 expect(result
).toBeFalsy()
71 it('Verify that data are sent to the worker correctly', async () => {
72 const data
= { f
: 10 }
73 const result
= await echoPool
.execute(data
)
74 expect(result
).toBeTruthy()
75 expect(result
.f
).toBe(data
.f
)
78 it('Verify that error handling is working properly:sync', async () => {
79 const data
= { f
: 10 }
82 await errorPool
.execute(data
)
86 expect(inError
).toBeDefined()
87 expect(typeof inError
=== 'string').toBeTruthy()
88 expect(inError
).toBe('Error Message from ClusterWorker')
91 it('Verify that error handling is working properly:async', async () => {
92 const data
= { f
: 10 }
95 await asyncErrorPool
.execute(data
)
99 expect(inError
).toBeDefined()
100 expect(typeof inError
=== 'string').toBeTruthy()
101 expect(inError
).toBe('Error Message from ClusterWorker:async')
104 it('Verify that async function is working properly', async () => {
105 const data
= { f
: 10 }
106 const startTime
= new Date().getTime()
107 const result
= await asyncPool
.execute(data
)
108 const usedTime
= new Date().getTime() - startTime
109 expect(result
).toBeTruthy()
110 expect(result
.f
).toBe(data
.f
)
111 expect(usedTime
).toBeGreaterThanOrEqual(2000)
114 it('Verify that maxTasks is set properly', async () => {
115 const worker
= asyncPool
.chooseWorker()
116 expect(worker
.getMaxListeners()).toBe(maxTasks
)
119 it('Shutdown test', async () => {
120 let closedWorkers
= 0
121 pool
.workers
.forEach(w
=> {
127 await
new Promise(resolve
=> setTimeout(resolve
, 200))
128 expect(closedWorkers
).toBe(numberOfWorkers
)
131 it('Validations test', () => {
134 const pool1
= new FixedClusterPool()
139 expect(error
).toBeTruthy()
140 expect(error
.message
).toBeTruthy()
143 it('Should work even without opts in input', async () => {
144 const pool1
= new FixedClusterPool(
146 './tests/worker-files/cluster/testWorker.js'
148 const res
= await pool1
.execute({ test
: 'test' })
149 expect(res
).toBeFalsy()