63399c6e4629ba4326080153cf5448ae3f2d7f40
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 after('Destroy all pools', async () => {
46 // We need to clean up the resources after our test
47 await echoPool
.destroy()
48 await asyncPool
.destroy()
49 await errorPool
.destroy()
50 await asyncErrorPool
.destroy()
51 await emptyPool
.destroy()
54 it('Choose worker round robin test', async () => {
55 const results
= new Set()
56 for (let i
= 0; i
< numberOfWorkers
; i
++) {
57 results
.add(pool
.chooseWorker().id
)
59 expect(results
.size
).toBe(numberOfWorkers
)
62 it('Verify that the function is executed in a worker cluster', async () => {
63 const result
= await pool
.execute({ test
: 'test' })
64 expect(result
).toBeDefined()
65 expect(result
).toBeFalsy()
68 it('Verify that is possible to invoke the execute method without input', async () => {
69 const result
= await pool
.execute()
70 expect(result
).toBeDefined()
71 expect(result
).toBeFalsy()
74 it('Verify that is possible to have a worker that return undefined', async () => {
75 const result
= await emptyPool
.execute()
76 expect(result
).toBeFalsy()
79 it('Verify that data are sent to the worker correctly', async () => {
80 const data
= { f
: 10 }
81 const result
= await echoPool
.execute(data
)
82 expect(result
).toBeTruthy()
83 expect(result
.f
).toBe(data
.f
)
86 it('Verify that error handling is working properly:sync', async () => {
87 const data
= { f
: 10 }
90 await errorPool
.execute(data
)
94 expect(inError
).toBeDefined()
95 expect(typeof inError
=== 'string').toBeTruthy()
96 expect(inError
).toBe('Error Message from ClusterWorker')
99 it('Verify that error handling is working properly:async', async () => {
100 const data
= { f
: 10 }
103 await asyncErrorPool
.execute(data
)
107 expect(inError
).toBeDefined()
108 expect(typeof inError
=== 'string').toBeTruthy()
109 expect(inError
).toBe('Error Message from ClusterWorker:async')
112 it('Verify that async function is working properly', async () => {
113 const data
= { f
: 10 }
114 const startTime
= new Date().getTime()
115 const result
= await asyncPool
.execute(data
)
116 const usedTime
= new Date().getTime() - startTime
117 expect(result
).toBeTruthy()
118 expect(result
.f
).toBe(data
.f
)
119 expect(usedTime
).toBeGreaterThanOrEqual(2000)
122 it('Verify that maxTasks is set properly', async () => {
123 const worker
= asyncPool
.chooseWorker()
124 expect(worker
.getMaxListeners()).toBe(maxTasks
)
127 it('Shutdown test', async () => {
128 const exitPromise
= TestUtils
.waitExits(pool
, numberOfWorkers
)
130 const res
= await exitPromise
131 expect(res
).toBe(numberOfWorkers
)
134 it('Should work even without opts in input', async () => {
135 const pool1
= new FixedClusterPool(
137 './tests/worker-files/cluster/testWorker.js'
139 const res
= await pool1
.execute({ test
: 'test' })
140 expect(res
).toBeFalsy()
141 // We need to clean up the resources after our test
142 await pool1
.destroy()