1 const expect
= require('expect')
2 const { FixedClusterPool
} = require('../../../lib/index')
3 const TestUtils
= require('../../test-utils')
4 const numberOfWorkers
= 10
6 const pool
= new FixedClusterPool(
8 './tests/worker-files/cluster/testWorker.js',
10 errorHandler
: e
=> console
.error(e
)
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
)
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 after('Destroy all pools', async () => {
45 // We need to clean up the resources after our test
46 await echoPool
.destroy()
47 await asyncPool
.destroy()
48 await errorPool
.destroy()
49 await asyncErrorPool
.destroy()
50 await emptyPool
.destroy()
53 it('Choose worker round robin test', async () => {
54 const results
= new Set()
55 for (let i
= 0; i
< numberOfWorkers
; i
++) {
56 results
.add(pool
.chooseWorker().id
)
58 expect(results
.size
).toBe(numberOfWorkers
)
61 it('Verify that the function is executed in a worker cluster', async () => {
62 const result
= await pool
.execute({ test
: 'test' })
63 expect(result
).toBeDefined()
64 expect(result
).toBeFalsy()
67 it('Verify that is possible to invoke the execute method without input', async () => {
68 const result
= await pool
.execute()
69 expect(result
).toBeDefined()
70 expect(result
).toBeFalsy()
73 it('Verify that is possible to have a worker that return undefined', async () => {
74 const result
= await emptyPool
.execute()
75 expect(result
).toBeFalsy()
78 it('Verify that data are sent to the worker correctly', async () => {
79 const data
= { f
: 10 }
80 const result
= await echoPool
.execute(data
)
81 expect(result
).toBeTruthy()
82 expect(result
.f
).toBe(data
.f
)
85 it('Verify that error handling is working properly:sync', async () => {
86 const data
= { f
: 10 }
89 await errorPool
.execute(data
)
93 expect(inError
).toBeDefined()
94 expect(typeof inError
=== 'string').toBeTruthy()
95 expect(inError
).toBe('Error Message from ClusterWorker')
98 it('Verify that error handling is working properly:async', async () => {
99 const data
= { f
: 10 }
102 await asyncErrorPool
.execute(data
)
106 expect(inError
).toBeDefined()
107 expect(typeof inError
=== 'string').toBeTruthy()
108 expect(inError
).toBe('Error Message from ClusterWorker:async')
111 it('Verify that async function is working properly', async () => {
112 const data
= { f
: 10 }
113 const startTime
= new Date().getTime()
114 const result
= await asyncPool
.execute(data
)
115 const usedTime
= new Date().getTime() - startTime
116 expect(result
).toBeTruthy()
117 expect(result
.f
).toBe(data
.f
)
118 expect(usedTime
).toBeGreaterThanOrEqual(2000)
121 it('Verify that maxTasks is set properly', async () => {
122 const worker
= asyncPool
.chooseWorker()
123 expect(worker
.getMaxListeners()).toBe(maxTasks
)
126 it('Shutdown test', async () => {
127 const exitPromise
= TestUtils
.waitExits(pool
, numberOfWorkers
)
129 const res
= await exitPromise
130 expect(res
).toBe(numberOfWorkers
)
133 it('Should work even without opts in input', async () => {
134 const pool1
= new FixedClusterPool(
136 './tests/worker-files/cluster/testWorker.js'
138 const res
= await pool1
.execute({ test
: 'test' })
139 expect(res
).toBeFalsy()
140 // We need to clean up the resources after our test
141 await pool1
.destroy()