1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const numberOfThreads
= 10
5 const pool
= new FixedThreadPool(
7 './tests/worker/thread/testWorker.js',
9 errorHandler
: e
=> console
.error(e
)
12 const emptyPool
= new FixedThreadPool(1, './tests/worker/thread/emptyWorker.js')
13 const echoPool
= new FixedThreadPool(1, './tests/worker/thread/echoWorker.js')
14 const errorPool
= new FixedThreadPool(
16 './tests/worker/thread/errorWorker.js',
18 errorHandler
: e
=> console
.error(e
),
19 onlineHandler
: () => console
.log('worker is online')
22 const asyncPool
= new FixedThreadPool(
24 './tests/worker/thread/asyncWorker.js',
25 { maxTasks
: maxTasks
}
28 describe('Fixed thread pool test suite ', () => {
29 it('Choose worker round robin test', async () => {
30 const results
= new Set()
31 for (let i
= 0; i
< numberOfThreads
; i
++) {
32 results
.add(pool
.chooseWorker().threadId
)
34 expect(results
.size
).toBe(numberOfThreads
)
37 it('Verify that the function is executed in a worker thread', async () => {
38 const result
= await pool
.execute({ test
: 'test' })
39 expect(result
).toBeDefined()
40 expect(result
).toBeFalsy()
43 it('Verify that is possible to invoke the execute method without input', async () => {
44 const result
= await pool
.execute()
45 expect(result
).toBeDefined()
46 expect(result
).toBeFalsy()
49 it('Verify that is possible to have a worker that return undefined', async () => {
50 const result
= await emptyPool
.execute()
51 expect(result
).toBeFalsy()
54 it('Verify that data are sent to the worker correctly', async () => {
55 const data
= { f
: 10 }
56 const result
= await echoPool
.execute(data
)
57 expect(result
).toBeTruthy()
58 expect(result
.f
).toBe(data
.f
)
61 it('Verify that error handling is working properly', async () => {
62 const data
= { f
: 10 }
65 await errorPool
.execute(data
)
69 expect(inError
).toBeTruthy()
70 expect(inError
instanceof Error
).toBeTruthy()
71 expect(inError
.message
).toBeTruthy()
74 it('Verify that async function is working properly', async () => {
75 const data
= { f
: 10 }
76 const startTime
= new Date().getTime()
77 const result
= await asyncPool
.execute(data
)
78 const usedTime
= new Date().getTime() - startTime
79 expect(result
).toBeTruthy()
80 expect(result
.f
).toBe(data
.f
)
81 expect(usedTime
).toBeGreaterThanOrEqual(2000)
84 it('Verify that maxTasks is set properly', async () => {
85 const worker
= asyncPool
.chooseWorker()
86 expect(worker
.port2
.getMaxListeners()).toBe(maxTasks
)
89 it('Shutdown test', async () => {
91 pool
.workers
.forEach(w
=> {
97 expect(closedThreads
).toBe(numberOfThreads
)
100 it('Validations test', () => {
103 const pool1
= new FixedThreadPool()
108 expect(error
).toBeTruthy()
109 expect(error
.message
).toBeTruthy()
112 it('Should work even without opts in input', async () => {
113 const pool1
= new FixedThreadPool(1, './tests/worker/thread/testWorker.js')
114 const res
= await pool1
.execute({ test
: 'test' })
115 expect(res
).toBeFalsy()