1 const expect
= require('expect')
2 const FixedThreadPool
= require('../lib/fixed')
4 const pool
= new FixedThreadPool(numThreads
,
5 './tests/testWorker.js',
6 { errorHandler
: (e
) => console
.error(e
), onlineHandler
: () => console
.log('worker is online') })
8 describe('Fixed thread pool test suite ', () => {
9 it('Choose worker round robin test', async () => {
10 const results
= new Set()
11 for (let i
= 0; i
< numThreads
; i
++) {
12 results
.add(pool
._chooseWorker().threadId
)
14 expect(results
.size
).toBe(numThreads
)
17 it('Verify that the function is executed in a worker thread', async () => {
18 const result
= await pool
.execute({ test
: 'test' })
19 expect(result
).toBeDefined()
20 expect(result
).toBeFalsy()
23 it('Shutdown test', async () => {
25 pool
.workers
.forEach(w
=> {
31 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
32 expect(closedThreads
).toBe(numThreads
)
35 it('Validations test', () => {
38 const pool1
= new FixedThreadPool()
43 expect(error
).toBeTruthy()
44 expect(error
.message
).toBeTruthy()
47 it('Should work even without opts in input', async () => {
48 const pool1
= new FixedThreadPool(1, './tests/testWorker.js')
49 const res
= await pool1
.execute({ test
: 'test' })
50 expect(res
).toBeFalsy()