1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const TestUtils
= require('../../test-utils')
4 const numberOfThreads
= 10
5 const pool
= new FixedThreadPool(
7 './tests/worker-files/thread/testWorker.js',
9 errorHandler
: e
=> console
.error(e
)
12 const emptyPool
= new FixedThreadPool(
14 './tests/worker-files/thread/emptyWorker.js',
15 { exitHandler
: () => console
.log('empty pool worker exited') }
17 const echoPool
= new FixedThreadPool(
19 './tests/worker-files/thread/echoWorker.js'
21 const errorPool
= new FixedThreadPool(
23 './tests/worker-files/thread/errorWorker.js',
25 errorHandler
: e
=> console
.error(e
)
28 const asyncErrorPool
= new FixedThreadPool(
30 './tests/worker-files/thread/asyncErrorWorker.js',
32 errorHandler
: e
=> console
.error(e
)
35 const asyncPool
= new FixedThreadPool(
37 './tests/worker-files/thread/asyncWorker.js'
40 describe('Fixed thread pool test suite', () => {
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()
46 await asyncErrorPool
.destroy()
47 await emptyPool
.destroy()
50 it('Choose worker round robin test', async () => {
51 const results
= new Set()
52 for (let i
= 0; i
< numberOfThreads
; i
++) {
53 results
.add(pool
.chooseWorker().threadId
)
55 expect(results
.size
).toBe(numberOfThreads
)
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()
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()
70 it('Verify that busy event is emitted', async () => {
73 pool
.emitter
.on('busy', () => poolBusy
++)
74 for (let i
= 0; i
< numberOfThreads
* 2; i
++) {
75 promises
.push(pool
.execute({ test
: 'test' }))
77 expect(poolBusy
).toBe(numberOfThreads
)
80 it('Verify that is possible to have a worker that return undefined', async () => {
81 const result
= await emptyPool
.execute()
82 expect(result
).toBeFalsy()
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
)
92 it('Verify that error handling is working properly:sync', async () => {
93 const data
= { f
: 10 }
96 await errorPool
.execute(data
)
100 expect(inError
).toBeDefined()
101 expect(inError
).toBeInstanceOf(Error
)
102 expect(inError
.message
).toBeDefined()
103 expect(typeof inError
.message
=== 'string').toBe(true)
106 it('Verify that error handling is working properly:async', async () => {
107 const data
= { f
: 10 }
110 await asyncErrorPool
.execute(data
)
114 expect(inError
).toBeDefined()
115 expect(inError
).toBeInstanceOf(Error
)
116 expect(inError
.message
).toBeDefined()
117 expect(typeof inError
.message
=== 'string').toBe(true)
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
)
127 expect(usedTime
).toBeGreaterThanOrEqual(2000)
130 it('Shutdown test', async () => {
131 const exitPromise
= TestUtils
.waitExits(pool
, numberOfThreads
)
133 const res
= await exitPromise
134 expect(res
).toBe(numberOfThreads
)
137 it('Should work even without opts in input', async () => {
138 const pool1
= new FixedThreadPool(
140 './tests/worker-files/thread/testWorker.js'
142 const res
= await pool1
.execute({ test
: 'test' })
143 expect(res
).toBeFalsy()
144 // We need to clean up the resources after our test
145 await pool1
.destroy()
148 it('Verify that a pool with zero worker fails', async () => {
150 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
151 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))