63399c6e4629ba4326080153cf5448ae3f2d7f40
[poolifier.git] / tests / pools / cluster / fixed.test.js
1 const expect = require('expect')
2 const { FixedClusterPool } = require('../../../lib/index')
3 const TestUtils = require('../../test-utils')
4 const numberOfWorkers = 10
5 const maxTasks = 500
6 const pool = new FixedClusterPool(
7 numberOfWorkers,
8 './tests/worker-files/cluster/testWorker.js',
9 {
10 errorHandler: e => console.error(e)
11 }
12 )
13 const emptyPool = new FixedClusterPool(
14 1,
15 './tests/worker-files/cluster/emptyWorker.js'
16 )
17 const echoPool = new FixedClusterPool(
18 1,
19 './tests/worker-files/cluster/echoWorker.js'
20 )
21 const errorPool = new FixedClusterPool(
22 1,
23 './tests/worker-files/cluster/errorWorker.js',
24 {
25 errorHandler: e => console.error(e)
26 }
27 )
28
29 const asyncErrorPool = new FixedClusterPool(
30 1,
31 './tests/worker-files/cluster/asyncErrorWorker.js',
32 {
33 onlineHandler: () => console.log('worker is online')
34 }
35 )
36 const asyncPool = new FixedClusterPool(
37 1,
38 './tests/worker-files/cluster/asyncWorker.js',
39 {
40 maxTasks: maxTasks
41 }
42 )
43
44 describe('Fixed cluster pool test suite ', () => {
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
54 it('Choose worker round robin test', async () => {
55 const results = new Set()
56 for (let i = 0; i < numberOfWorkers; i++) {
57 results.add(pool.chooseWorker().id)
58 }
59 expect(results.size).toBe(numberOfWorkers)
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
122 it('Verify that maxTasks is set properly', async () => {
123 const worker = asyncPool.chooseWorker()
124 expect(worker.getMaxListeners()).toBe(maxTasks)
125 })
126
127 it('Shutdown test', async () => {
128 const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
129 await pool.destroy()
130 const res = await exitPromise
131 expect(res).toBe(numberOfWorkers)
132 })
133
134 it('Should work even without opts in input', async () => {
135 const pool1 = new FixedClusterPool(
136 1,
137 './tests/worker-files/cluster/testWorker.js'
138 )
139 const res = await pool1.execute({ test: 'test' })
140 expect(res).toBeFalsy()
141 // We need to clean up the resources after our test
142 await pool1.destroy()
143 })
144 })