0eb21d0716c32ac19bdb321a24253a31ed0eb44a
[poolifier.git] / tests / pools / cluster / fixed.test.js
1 const expect = require('expect')
2 const { FixedClusterPool } = require('../../../lib/index')
3 const TestUtils = require('../../test-utils')
4 const numberOfWorkers = 10
5 const maxTasks = 500
6 const pool = new FixedClusterPool(
7 numberOfWorkers,
8 './tests/worker-files/cluster/testWorker.js',
9 {
10 errorHandler: e => console.error(e)
11 }
12 )
13 const emptyPool = new FixedClusterPool(
14 1,
15 './tests/worker-files/cluster/emptyWorker.js'
16 )
17 const echoPool = new FixedClusterPool(
18 1,
19 './tests/worker-files/cluster/echoWorker.js'
20 )
21 const errorPool = new FixedClusterPool(
22 1,
23 './tests/worker-files/cluster/errorWorker.js',
24 {
25 errorHandler: e => console.error(e)
26 }
27 )
28 const asyncErrorPool = new FixedClusterPool(
29 1,
30 './tests/worker-files/cluster/asyncErrorWorker.js',
31 {
32 onlineHandler: () => console.log('worker is online')
33 }
34 )
35 const asyncPool = new FixedClusterPool(
36 1,
37 './tests/worker-files/cluster/asyncWorker.js',
38 {
39 maxTasks: maxTasks
40 }
41 )
42
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()
51 })
52
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)
57 }
58 expect(results.size).toBe(numberOfWorkers)
59 })
60
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()
65 })
66
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()
71 })
72
73 it('Verify that is possible to have a worker that return undefined', async () => {
74 const result = await emptyPool.execute()
75 expect(result).toBeFalsy()
76 })
77
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)
83 })
84
85 it('Verify that error handling is working properly:sync', async () => {
86 const data = { f: 10 }
87 let inError
88 try {
89 await errorPool.execute(data)
90 } catch (e) {
91 inError = e
92 }
93 expect(inError).toBeDefined()
94 expect(typeof inError === 'string').toBeTruthy()
95 expect(inError).toBe('Error Message from ClusterWorker')
96 })
97
98 it('Verify that error handling is working properly:async', async () => {
99 const data = { f: 10 }
100 let inError
101 try {
102 await asyncErrorPool.execute(data)
103 } catch (e) {
104 inError = e
105 }
106 expect(inError).toBeDefined()
107 expect(typeof inError === 'string').toBeTruthy()
108 expect(inError).toBe('Error Message from ClusterWorker:async')
109 })
110
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)
119 })
120
121 it('Verify that maxTasks is set properly', async () => {
122 const worker = asyncPool.chooseWorker()
123 expect(worker.getMaxListeners()).toBe(maxTasks)
124 })
125
126 it('Shutdown test', async () => {
127 const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
128 await pool.destroy()
129 const res = await exitPromise
130 expect(res).toBe(numberOfWorkers)
131 })
132
133 it('Should work even without opts in input', async () => {
134 const pool1 = new FixedClusterPool(
135 1,
136 './tests/worker-files/cluster/testWorker.js'
137 )
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()
142 })
143
144 it('Verify that a pool with zero worker fails', async () => {
145 expect(
146 () =>
147 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
148 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
149 })
150 })