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