Improve jsdoc comments (#175)
[poolifier.git] / tests / pools / cluster / fixed.test.js
CommitLineData
325f50bc
S
1const expect = require('expect')
2const { FixedClusterPool } = require('../../../lib/index')
5c5a1fb7 3const numberOfWorkers = 10
515e5da7 4const maxTasks = 500
325f50bc 5const pool = new FixedClusterPool(
5c5a1fb7 6 numberOfWorkers,
76b1e974 7 './tests/worker-files/cluster/testWorker.js',
325f50bc 8 {
e5177d86 9 errorHandler: e => console.error(e)
325f50bc
S
10 }
11)
12const emptyPool = new FixedClusterPool(
13 1,
76b1e974
S
14 './tests/worker-files/cluster/emptyWorker.js'
15)
16const echoPool = new FixedClusterPool(
17 1,
18 './tests/worker-files/cluster/echoWorker.js'
325f50bc 19)
325f50bc
S
20const errorPool = new FixedClusterPool(
21 1,
76b1e974 22 './tests/worker-files/cluster/errorWorker.js',
325f50bc 23 {
e5177d86 24 errorHandler: e => console.error(e)
325f50bc
S
25 }
26)
27
28const asyncErrorPool = new FixedClusterPool(
29 1,
76b1e974 30 './tests/worker-files/cluster/asyncErrorWorker.js',
325f50bc 31 {
325f50bc
S
32 onlineHandler: () => console.log('worker is online')
33 }
34)
35const asyncPool = new FixedClusterPool(
36 1,
76b1e974 37 './tests/worker-files/cluster/asyncWorker.js',
515e5da7
APA
38 {
39 maxTasks: maxTasks
40 }
325f50bc
S
41)
42
43describe('Fixed cluster pool test suite ', () => {
44 it('Choose worker round robin test', async () => {
45 const results = new Set()
5c5a1fb7 46 for (let i = 0; i < numberOfWorkers; i++) {
325f50bc
S
47 results.add(pool.chooseWorker().id)
48 }
5c5a1fb7 49 expect(results.size).toBe(numberOfWorkers)
325f50bc
S
50 })
51
52 it('Verify that the function is executed in a worker cluster', async () => {
53 const result = await pool.execute({ test: 'test' })
54 expect(result).toBeDefined()
55 expect(result).toBeFalsy()
56 })
57
58 it('Verify that is possible to invoke the execute method without input', async () => {
59 const result = await pool.execute()
60 expect(result).toBeDefined()
61 expect(result).toBeFalsy()
62 })
63
64 it('Verify that is possible to have a worker that return undefined', async () => {
65 const result = await emptyPool.execute()
66 expect(result).toBeFalsy()
67 })
68
69 it('Verify that data are sent to the worker correctly', async () => {
70 const data = { f: 10 }
71 const result = await echoPool.execute(data)
72 expect(result).toBeTruthy()
73 expect(result.f).toBe(data.f)
74 })
75
76 it('Verify that error handling is working properly:sync', async () => {
77 const data = { f: 10 }
78 let inError
79 try {
80 await errorPool.execute(data)
81 } catch (e) {
82 inError = e
83 }
84 expect(inError).toBeDefined()
85 expect(typeof inError === 'string').toBeTruthy()
86 expect(inError).toBe('Error Message from ClusterWorker')
87 })
88
89 it('Verify that error handling is working properly:async', async () => {
90 const data = { f: 10 }
91 let inError
92 try {
93 await asyncErrorPool.execute(data)
94 } catch (e) {
95 inError = e
96 }
97 expect(inError).toBeDefined()
98 expect(typeof inError === 'string').toBeTruthy()
99 expect(inError).toBe('Error Message from ClusterWorker:async')
100 })
101
102 it('Verify that async function is working properly', async () => {
103 const data = { f: 10 }
104 const startTime = new Date().getTime()
105 const result = await asyncPool.execute(data)
106 const usedTime = new Date().getTime() - startTime
107 expect(result).toBeTruthy()
108 expect(result.f).toBe(data.f)
109 expect(usedTime).toBeGreaterThanOrEqual(2000)
110 })
111
515e5da7
APA
112 it('Verify that maxTasks is set properly', async () => {
113 const worker = asyncPool.chooseWorker()
114 expect(worker.getMaxListeners()).toBe(maxTasks)
115 })
116
325f50bc
S
117 it('Shutdown test', async () => {
118 let closedWorkers = 0
119 pool.workers.forEach(w => {
120 w.on('exit', () => {
121 closedWorkers++
122 })
123 })
45dbbb14 124 await pool.destroy()
5efc2a90 125 await new Promise(resolve => setTimeout(resolve, 500))
5c5a1fb7 126 expect(closedWorkers).toBe(numberOfWorkers)
325f50bc
S
127 })
128
129 it('Validations test', () => {
130 let error
131 try {
132 const pool1 = new FixedClusterPool()
133 console.log(pool1)
134 } catch (e) {
135 error = e
136 }
137 expect(error).toBeTruthy()
138 expect(error.message).toBeTruthy()
139 })
140
141 it('Should work even without opts in input', async () => {
142 const pool1 = new FixedClusterPool(
143 1,
76b1e974 144 './tests/worker-files/cluster/testWorker.js'
325f50bc
S
145 )
146 const res = await pool1.execute({ test: 'test' })
147 expect(res).toBeFalsy()
148 })
149})