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