Pool busy event emitting on all pool types (#241)
[poolifier.git] / tests / pools / thread / fixed.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { FixedThreadPool } = require('../../../lib/index')
85a3f8a7 3const TestUtils = require('../../test-utils')
5c5a1fb7 4const numberOfThreads = 10
325f50bc 5const pool = new FixedThreadPool(
5c5a1fb7 6 numberOfThreads,
76b1e974 7 './tests/worker-files/thread/testWorker.js',
325f50bc 8 {
e5177d86 9 errorHandler: e => console.error(e)
325f50bc
S
10 }
11)
76b1e974
S
12const emptyPool = new FixedThreadPool(
13 1,
7fc5cce6 14 './tests/worker-files/thread/emptyWorker.js',
7c0ba920 15 { exitHandler: () => console.log('empty pool worker exited') }
76b1e974
S
16)
17const echoPool = new FixedThreadPool(
18 1,
19 './tests/worker-files/thread/echoWorker.js'
20)
325f50bc
S
21const errorPool = new FixedThreadPool(
22 1,
76b1e974 23 './tests/worker-files/thread/errorWorker.js',
325f50bc
S
24 {
25 errorHandler: e => console.error(e),
26 onlineHandler: () => console.log('worker is online')
27 }
28)
7c0ba920
JB
29const asyncErrorPool = new FixedThreadPool(
30 1,
31 './tests/worker-files/thread/asyncErrorWorker.js',
32 {
33 errorHandler: e => console.error(e),
34 onlineHandler: () => console.log('worker is online')
35 }
36)
515e5da7
APA
37const asyncPool = new FixedThreadPool(
38 1,
1927ee67 39 './tests/worker-files/thread/asyncWorker.js'
515e5da7 40)
506c2a14 41
a35560ba 42describe('Fixed thread pool test suite', () => {
0e2503fc
JB
43 after('Destroy all pools', async () => {
44 // We need to clean up the resources after our test
45 await echoPool.destroy()
46 await asyncPool.destroy()
47 await errorPool.destroy()
7c0ba920 48 await asyncErrorPool.destroy()
0e2503fc
JB
49 await emptyPool.destroy()
50 })
51
506c2a14 52 it('Choose worker round robin test', async () => {
53 const results = new Set()
5c5a1fb7 54 for (let i = 0; i < numberOfThreads; i++) {
fa0f5b28 55 results.add(pool.chooseWorker().threadId)
506c2a14 56 }
5c5a1fb7 57 expect(results.size).toBe(numberOfThreads)
506c2a14 58 })
59
60 it('Verify that the function is executed in a worker thread', async () => {
61 const result = await pool.execute({ test: 'test' })
62 expect(result).toBeDefined()
63 expect(result).toBeFalsy()
64 })
65
106744f7 66 it('Verify that is possible to invoke the execute method without input', async () => {
67 const result = await pool.execute()
68 expect(result).toBeDefined()
69 expect(result).toBeFalsy()
70 })
71
7c0ba920
JB
72 it('Verify that busy event is emitted', async () => {
73 const promises = []
74 let poolBusy = 0
75 pool.emitter.on('busy', () => poolBusy++)
76 for (let i = 0; i < numberOfThreads * 2; i++) {
77 promises.push(pool.execute({ test: 'test' }))
78 }
79 expect(poolBusy).toEqual(numberOfThreads)
80 })
81
106744f7 82 it('Verify that is possible to have a worker that return undefined', async () => {
83 const result = await emptyPool.execute()
84 expect(result).toBeFalsy()
85 })
86
87 it('Verify that data are sent to the worker correctly', async () => {
88 const data = { f: 10 }
89 const result = await echoPool.execute(data)
90 expect(result).toBeTruthy()
91 expect(result.f).toBe(data.f)
92 })
93
7c0ba920 94 it('Verify that error handling is working properly:sync', async () => {
106744f7 95 const data = { f: 10 }
96 let inError
97 try {
98 await errorPool.execute(data)
99 } catch (e) {
100 inError = e
101 }
7c0ba920
JB
102 expect(inError).toBeDefined()
103 expect(inError).toBeInstanceOf(Error)
104 expect(inError.message).toBeDefined()
105 expect(typeof inError.message === 'string').toEqual(true)
106 })
107
108 it('Verify that error handling is working properly:async', async () => {
109 const data = { f: 10 }
110 let inError
111 try {
112 await asyncErrorPool.execute(data)
113 } catch (e) {
114 inError = e
115 }
116 expect(inError).toBeDefined()
117 expect(inError).toBeInstanceOf(Error)
118 expect(inError.message).toBeDefined()
119 expect(typeof inError.message === 'string').toEqual(true)
106744f7 120 })
121
7784f548 122 it('Verify that async function is working properly', async () => {
123 const data = { f: 10 }
124 const startTime = new Date().getTime()
125 const result = await asyncPool.execute(data)
126 const usedTime = new Date().getTime() - startTime
127 expect(result).toBeTruthy()
128 expect(result.f).toBe(data.f)
32d490eb 129 expect(usedTime).toBeGreaterThanOrEqual(2000)
7784f548 130 })
131
506c2a14 132 it('Shutdown test', async () => {
85a3f8a7 133 const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
1f9a5a44 134 await pool.destroy()
85a3f8a7
APA
135 const res = await exitPromise
136 expect(res).toBe(numberOfThreads)
506c2a14 137 })
138
139 it('Should work even without opts in input', async () => {
76b1e974
S
140 const pool1 = new FixedThreadPool(
141 1,
142 './tests/worker-files/thread/testWorker.js'
143 )
506c2a14 144 const res = await pool1.execute({ test: 'test' })
145 expect(res).toBeFalsy()
0e2503fc
JB
146 // We need to clean up the resources after our test
147 await pool1.destroy()
506c2a14 148 })
8d3782fa
JB
149
150 it('Verify that a pool with zero worker fails', async () => {
151 expect(
152 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
153 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
154 })
506c2a14 155})