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') })
10 const asyncPool
= new FixedThreadPool(1, './tests/workers/asyncWorker.js')
12 describe('Fixed thread pool test suite ', () => {
13 it('Choose worker round robin test', async () => {
14 const results
= new Set()
15 for (let i
= 0; i
< numThreads
; i
++) {
16 results
.add(pool
._chooseWorker().threadId
)
18 expect(results
.size
).toBe(numThreads
)
21 it('Verify that the function is executed in a worker thread', async () => {
22 const result
= await pool
.execute({ test
: 'test' })
23 expect(result
).toBeDefined()
24 expect(result
).toBeFalsy()
27 it('Verify that is possible to invoke the execute method without input', async () => {
28 const result
= await pool
.execute()
29 expect(result
).toBeDefined()
30 expect(result
).toBeFalsy()
33 it('Verify that is possible to have a worker that return undefined', async () => {
34 const result
= await emptyPool
.execute()
35 expect(result
).toBeFalsy()
38 it('Verify that data are sent to the worker correctly', async () => {
39 const data
= { f
: 10 }
40 const result
= await echoPool
.execute(data
)
41 expect(result
).toBeTruthy()
42 expect(result
.f
).toBe(data
.f
)
45 it('Verify that error handling is working properly', async () => {
46 const data
= { f
: 10 }
49 await errorPool
.execute(data
)
53 expect(inError
).toBeTruthy()
54 expect(inError
instanceof Error
).toBeTruthy()
55 expect(inError
.message
).toBeTruthy()
58 it('Verify that async function is working properly', async () => {
59 const data
= { f
: 10 }
60 const startTime
= new Date().getTime()
61 const result
= await asyncPool
.execute(data
)
62 const usedTime
= new Date().getTime() - startTime
63 expect(result
).toBeTruthy()
64 expect(result
.f
).toBe(data
.f
)
65 expect(usedTime
).toBeGreaterThanOrEqual(2000)
68 it('Shutdown test', async () => {
70 pool
.workers
.forEach(w
=> {
76 expect(closedThreads
).toBe(numThreads
)
79 it('Validations test', () => {
82 const pool1
= new FixedThreadPool()
87 expect(error
).toBeTruthy()
88 expect(error
.message
).toBeTruthy()
91 it('Should work even without opts in input', async () => {
92 const pool1
= new FixedThreadPool(1, './tests/workers/testWorker.js')
93 const res
= await pool1
.execute({ test
: 'test' })
94 expect(res
).toBeFalsy()