1 const expect
= require('expect')
2 const { FixedClusterPool
} = require('../../../lib/index')
4 const pool
= new FixedClusterPool(
6 './tests/worker/cluster/testWorker.js',
8 errorHandler
: e
=> console
.error(e
),
9 onlineHandler
: () => console
.log('worker is online')
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
),
22 onlineHandler
: () => console
.log('worker is online')
26 const asyncErrorPool
= new FixedClusterPool(
28 './tests/worker/cluster/asyncErrorWorker.js',
30 errorHandler
: e
=> console
.error(e
),
31 onlineHandler
: () => console
.log('worker is online')
34 const asyncPool
= new FixedClusterPool(
36 './tests/worker/cluster/asyncWorker.js'
39 describe('Fixed cluster pool test suite ', () => {
40 it('Choose worker round robin test', async () => {
41 const results
= new Set()
42 for (let i
= 0; i
< numWorkers
; i
++) {
43 results
.add(pool
.chooseWorker().id
)
45 expect(results
.size
).toBe(numWorkers
)
48 it('Verify that the function is executed in a worker cluster', async () => {
49 const result
= await pool
.execute({ test
: 'test' })
50 expect(result
).toBeDefined()
51 expect(result
).toBeFalsy()
54 it('Verify that is possible to invoke the execute method without input', async () => {
55 const result
= await pool
.execute()
56 expect(result
).toBeDefined()
57 expect(result
).toBeFalsy()
60 it('Verify that is possible to have a worker that return undefined', async () => {
61 const result
= await emptyPool
.execute()
62 expect(result
).toBeFalsy()
65 it('Verify that data are sent to the worker correctly', async () => {
66 const data
= { f
: 10 }
67 const result
= await echoPool
.execute(data
)
68 expect(result
).toBeTruthy()
69 expect(result
.f
).toBe(data
.f
)
72 it('Verify that error handling is working properly:sync', async () => {
73 const data
= { f
: 10 }
76 await errorPool
.execute(data
)
80 expect(inError
).toBeDefined()
81 expect(typeof inError
=== 'string').toBeTruthy()
82 expect(inError
).toBe('Error Message from ClusterWorker')
85 it('Verify that error handling is working properly:async', async () => {
86 const data
= { f
: 10 }
89 await asyncErrorPool
.execute(data
)
93 expect(inError
).toBeDefined()
94 expect(typeof inError
=== 'string').toBeTruthy()
95 expect(inError
).toBe('Error Message from ClusterWorker:async')
98 it('Verify that async function is working properly', async () => {
99 const data
= { f
: 10 }
100 const startTime
= new Date().getTime()
101 const result
= await asyncPool
.execute(data
)
102 const usedTime
= new Date().getTime() - startTime
103 expect(result
).toBeTruthy()
104 expect(result
.f
).toBe(data
.f
)
105 expect(usedTime
).toBeGreaterThanOrEqual(2000)
108 it('Shutdown test', async () => {
109 let closedWorkers
= 0
110 pool
.workers
.forEach(w
=> {
116 await
new Promise(resolve
=> setTimeout(resolve
, 200))
117 expect(closedWorkers
).toBe(numWorkers
)
120 it('Validations test', () => {
123 const pool1
= new FixedClusterPool()
128 expect(error
).toBeTruthy()
129 expect(error
.message
).toBeTruthy()
132 it('Should work even without opts in input', async () => {
133 const pool1
= new FixedClusterPool(
135 './tests/worker/cluster/testWorker.js'
137 const res
= await pool1
.execute({ test
: 'test' })
138 expect(res
).toBeFalsy()