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