Merge branch 'master' into add-worker-test
[poolifier.git] / tests / pools / cluster / fixed.test.js
1 const expect = require('expect')
2 const { FixedClusterPool } = require('../../../lib/index')
3 const numberOfWorkers = 10
4 const maxTasks = 500
5 const pool = new FixedClusterPool(
6 numberOfWorkers,
7 './tests/worker-files/cluster/testWorker.js',
8 {
9 errorHandler: e => console.error(e)
10 }
11 )
12 const emptyPool = new FixedClusterPool(
13 1,
14 './tests/worker-files/cluster/emptyWorker.js'
15 )
16 const echoPool = new FixedClusterPool(
17 1,
18 './tests/worker-files/cluster/echoWorker.js'
19 )
20 const errorPool = new FixedClusterPool(
21 1,
22 './tests/worker-files/cluster/errorWorker.js',
23 {
24 errorHandler: e => console.error(e)
25 }
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 it('Choose worker round robin test', async () => {
45 const results = new Set()
46 for (let i = 0; i < numberOfWorkers; i++) {
47 results.add(pool.chooseWorker().id)
48 }
49 expect(results.size).toBe(numberOfWorkers)
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
112 it('Verify that maxTasks is set properly', async () => {
113 const worker = asyncPool.chooseWorker()
114 expect(worker.getMaxListeners()).toBe(maxTasks)
115 })
116
117 it('Shutdown test', async () => {
118 let closedWorkers = 0
119 pool.workers.forEach(w => {
120 w.on('exit', () => {
121 closedWorkers++
122 })
123 })
124 await pool.destroy()
125 await new Promise(resolve => setTimeout(resolve, 500))
126 expect(closedWorkers).toBe(numberOfWorkers)
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,
144 './tests/worker-files/cluster/testWorker.js'
145 )
146 const res = await pool1.execute({ test: 'test' })
147 expect(res).toBeFalsy()
148 })
149 })