1 const expect
= require('expect')
2 const FixedThreadPool
= require('../lib/fixed')
4 const pool
= new FixedThreadPool(numThreads
,
5 './tests/workers/testWorker.js',
6 { errorHandler
: (e
) => console
.error(e
), onlineHandler
: () => console
.log('worker is online') })
7 const emptyPool
= new FixedThreadPool(1, './tests/workers/emptyWorker.js')
8 const echoPool
= new FixedThreadPool(1, './tests/workers/echoWorker.js')
9 const errorPool
= new FixedThreadPool(1, './tests/workers/errorWorker.js', { errorHandler
: (e
) => console
.error(e
), onlineHandler
: () => console
.log('worker is online') })
11 describe('Fixed thread pool test suite ', () => {
12 it('Choose worker round robin test', async () => {
13 const results
= new Set()
14 for (let i
= 0; i
< numThreads
; i
++) {
15 results
.add(pool
._chooseWorker().threadId
)
17 expect(results
.size
).toBe(numThreads
)
20 it('Verify that the function is executed in a worker thread', async () => {
21 const result
= await pool
.execute({ test
: 'test' })
22 expect(result
).toBeDefined()
23 expect(result
).toBeFalsy()
26 it('Verify that is possible to invoke the execute method without input', async () => {
27 const result
= await pool
.execute()
28 expect(result
).toBeDefined()
29 expect(result
).toBeFalsy()
32 it('Verify that is possible to have a worker that return undefined', async () => {
33 const result
= await emptyPool
.execute()
34 expect(result
).toBeFalsy()
37 it('Verify that data are sent to the worker correctly', async () => {
38 const data
= { f
: 10 }
39 const result
= await echoPool
.execute(data
)
40 expect(result
).toBeTruthy()
41 expect(result
.f
).toBe(data
.f
)
44 it('Verify that error handling is working properly', async () => {
45 const data
= { f
: 10 }
48 await errorPool
.execute(data
)
52 expect(inError
).toBeTruthy()
53 expect(inError
instanceof Error
).toBeTruthy()
54 expect(inError
.message
).toBeTruthy()
57 it('Shutdown test', async () => {
59 pool
.workers
.forEach(w
=> {
65 expect(closedThreads
).toBe(numThreads
)
68 it('Validations test', () => {
71 const pool1
= new FixedThreadPool()
76 expect(error
).toBeTruthy()
77 expect(error
.message
).toBeTruthy()
80 it('Should work even without opts in input', async () => {
81 const pool1
= new FixedThreadPool(1, './tests/workers/testWorker.js')
82 const res
= await pool1
.execute({ test
: 'test' })
83 expect(res
).toBeFalsy()