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