08476e093d4b7051040ab84879152a869b254749
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
)
29 const asyncErrorPool
= new FixedClusterPool(
31 './tests/worker-files/cluster/asyncErrorWorker.js',
33 onlineHandler
: () => console
.log('worker is online')
36 const asyncPool
= new FixedClusterPool(
38 './tests/worker-files/cluster/asyncWorker.js',
44 describe('Fixed cluster pool test suite ', () => {
45 it('Choose worker round robin test', async () => {
46 const results
= new Set()
47 for (let i
= 0; i
< numberOfWorkers
; i
++) {
48 results
.add(pool
.chooseWorker().id
)
50 expect(results
.size
).toBe(numberOfWorkers
)
53 it('Verify that the function is executed in a worker cluster', async () => {
54 const result
= await pool
.execute({ test
: 'test' })
55 expect(result
).toBeDefined()
56 expect(result
).toBeFalsy()
59 it('Verify that is possible to invoke the execute method without input', async () => {
60 const result
= await pool
.execute()
61 expect(result
).toBeDefined()
62 expect(result
).toBeFalsy()
65 it('Verify that is possible to have a worker that return undefined', async () => {
66 const result
= await emptyPool
.execute()
67 expect(result
).toBeFalsy()
70 it('Verify that data are sent to the worker correctly', async () => {
71 const data
= { f
: 10 }
72 const result
= await echoPool
.execute(data
)
73 expect(result
).toBeTruthy()
74 expect(result
.f
).toBe(data
.f
)
77 it('Verify that error handling is working properly:sync', async () => {
78 const data
= { f
: 10 }
81 await errorPool
.execute(data
)
85 expect(inError
).toBeDefined()
86 expect(typeof inError
=== 'string').toBeTruthy()
87 expect(inError
).toBe('Error Message from ClusterWorker')
90 it('Verify that error handling is working properly:async', async () => {
91 const data
= { f
: 10 }
94 await asyncErrorPool
.execute(data
)
98 expect(inError
).toBeDefined()
99 expect(typeof inError
=== 'string').toBeTruthy()
100 expect(inError
).toBe('Error Message from ClusterWorker:async')
103 it('Verify that async function is working properly', async () => {
104 const data
= { f
: 10 }
105 const startTime
= new Date().getTime()
106 const result
= await asyncPool
.execute(data
)
107 const usedTime
= new Date().getTime() - startTime
108 expect(result
).toBeTruthy()
109 expect(result
.f
).toBe(data
.f
)
110 expect(usedTime
).toBeGreaterThanOrEqual(2000)
113 it('Verify that maxTasks is set properly', async () => {
114 const worker
= asyncPool
.chooseWorker()
115 expect(worker
.getMaxListeners()).toBe(maxTasks
)
118 it('Shutdown test', async () => {
119 const exitPromise
= TestUtils
.waitExits(pool
, numberOfWorkers
)
121 const res
= await exitPromise
122 expect(res
).toBe(numberOfWorkers
)
125 it('Should work even without opts in input', async () => {
126 const pool1
= new FixedClusterPool(
128 './tests/worker-files/cluster/testWorker.js'
130 const res
= await pool1
.execute({ test
: 'test' })
131 expect(res
).toBeFalsy()