a0fd08c8038d9455dabe67b0891d7907b93d98d5
1 const { expect
} = require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const { WorkerFunctions
} = require('../../test-types')
4 const TestUtils
= require('../../test-utils')
6 describe('Fixed thread pool test suite', () => {
7 const numberOfThreads
= 6
8 const pool
= new FixedThreadPool(
10 './tests/worker-files/thread/testWorker.js',
12 errorHandler
: e
=> console
.error(e
)
15 const emptyPool
= new FixedThreadPool(
17 './tests/worker-files/thread/emptyWorker.js',
18 { exitHandler
: () => console
.log('empty pool worker exited') }
20 const echoPool
= new FixedThreadPool(
22 './tests/worker-files/thread/echoWorker.js'
24 const errorPool
= new FixedThreadPool(
26 './tests/worker-files/thread/errorWorker.js',
28 errorHandler
: e
=> console
.error(e
)
31 const asyncErrorPool
= new FixedThreadPool(
33 './tests/worker-files/thread/asyncErrorWorker.js',
35 errorHandler
: e
=> console
.error(e
)
38 const asyncPool
= new FixedThreadPool(
40 './tests/worker-files/thread/asyncWorker.js'
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()
48 await asyncErrorPool
.destroy()
49 await emptyPool
.destroy()
52 it('Choose worker round robin test', async () => {
53 const results
= new Set()
54 for (let i
= 0; i
< numberOfThreads
; i
++) {
55 results
.add(pool
.chooseWorker().threadId
)
57 expect(results
.size
).toBe(numberOfThreads
)
60 it('Verify that the function is executed in a worker thread', async () => {
61 let result
= await pool
.execute({
62 function: WorkerFunctions
.fibonacci
64 expect(result
).toBe(false)
65 result
= await pool
.execute({
66 function: WorkerFunctions
.factorial
68 expect(result
).toBe(false)
71 it('Verify that is possible to invoke the execute method without input', async () => {
72 const result
= await pool
.execute()
73 expect(result
).toBe(false)
76 it('Verify that busy event is emitted', async () => {
78 pool
.emitter
.on('busy', () => poolBusy
++)
79 for (let i
= 0; i
< numberOfThreads
* 2; i
++) {
82 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
83 // So in total numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool.
84 expect(poolBusy
).toBe(numberOfThreads
+ 1)
87 it('Verify that is possible to have a worker that return undefined', async () => {
88 const result
= await emptyPool
.execute()
89 expect(result
).toBeUndefined()
92 it('Verify that data are sent to the worker correctly', async () => {
93 const data
= { f
: 10 }
94 const result
= await echoPool
.execute(data
)
95 expect(result
).toStrictEqual(data
)
98 it('Verify that error handling is working properly:sync', async () => {
99 const data
= { f
: 10 }
102 await errorPool
.execute(data
)
106 expect(inError
).toBeDefined()
107 expect(inError
).toBeInstanceOf(Error
)
108 expect(inError
.message
).toBeDefined()
109 expect(typeof inError
.message
=== 'string').toBe(true)
110 expect(inError
.message
).toBe('Error Message from ThreadWorker')
113 it('Verify that error handling is working properly:async', async () => {
114 const data
= { f
: 10 }
117 await asyncErrorPool
.execute(data
)
121 expect(inError
).toBeDefined()
122 expect(inError
).toBeInstanceOf(Error
)
123 expect(inError
.message
).toBeDefined()
124 expect(typeof inError
.message
=== 'string').toBe(true)
125 expect(inError
.message
).toBe('Error Message from ThreadWorker:async')
128 it('Verify that async function is working properly', async () => {
129 const data
= { f
: 10 }
130 const startTime
= Date
.now()
131 const result
= await asyncPool
.execute(data
)
132 const usedTime
= Date
.now() - startTime
133 expect(result
).toStrictEqual(data
)
134 expect(usedTime
).toBeGreaterThanOrEqual(2000)
137 it('Shutdown test', async () => {
138 const exitPromise
= TestUtils
.waitExits(pool
, numberOfThreads
)
140 const numberOfExitEvents
= await exitPromise
141 expect(numberOfExitEvents
).toBe(numberOfThreads
)
144 it('Should work even without opts in input', async () => {
145 const pool1
= new FixedThreadPool(
147 './tests/worker-files/thread/testWorker.js'
149 const res
= await pool1
.execute()
150 expect(res
).toBe(false)
151 // We need to clean up the resources after our test
152 await pool1
.destroy()
155 it('Verify that a pool with zero worker fails', async () => {
157 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
158 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))