1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const numberOfThreads
= 10
5 const pool
= new FixedThreadPool(
7 './tests/worker-files/thread/testWorker.js',
9 errorHandler
: e
=> console
.error(e
),
10 onlineHandler
: () => console
.log('worker is online')
13 const emptyPool
= new FixedThreadPool(
15 './tests/worker-files/thread/emptyWorker.js'
17 const echoPool
= new FixedThreadPool(
19 './tests/worker-files/thread/echoWorker.js'
21 const errorPool
= new FixedThreadPool(
23 './tests/worker-files/thread/errorWorker.js',
25 errorHandler
: e
=> console
.error(e
),
26 onlineHandler
: () => console
.log('worker is online')
29 const asyncPool
= new FixedThreadPool(
31 './tests/worker-files/thread/asyncWorker.js',
32 { maxTasks
: maxTasks
}
35 describe('Fixed thread pool test suite ', () => {
36 it('Choose worker round robin test', async () => {
37 const results
= new Set()
38 for (let i
= 0; i
< numberOfThreads
; i
++) {
39 results
.add(pool
.chooseWorker().threadId
)
41 expect(results
.size
).toBe(numberOfThreads
)
44 it('Verify that the function is executed in a worker thread', async () => {
45 const result
= await pool
.execute({ test
: 'test' })
46 expect(result
).toBeDefined()
47 expect(result
).toBeFalsy()
50 it('Verify that is possible to invoke the execute method without input', async () => {
51 const result
= await pool
.execute()
52 expect(result
).toBeDefined()
53 expect(result
).toBeFalsy()
56 it('Verify that is possible to have a worker that return undefined', async () => {
57 const result
= await emptyPool
.execute()
58 expect(result
).toBeFalsy()
61 it('Verify that data are sent to the worker correctly', async () => {
62 const data
= { f
: 10 }
63 const result
= await echoPool
.execute(data
)
64 expect(result
).toBeTruthy()
65 expect(result
.f
).toBe(data
.f
)
68 it('Verify that error handling is working properly', async () => {
69 const data
= { f
: 10 }
72 await errorPool
.execute(data
)
76 expect(inError
).toBeTruthy()
77 expect(inError
instanceof Error
).toBeTruthy()
78 expect(inError
.message
).toBeTruthy()
81 it('Verify that async function is working properly', async () => {
82 const data
= { f
: 10 }
83 const startTime
= new Date().getTime()
84 const result
= await asyncPool
.execute(data
)
85 const usedTime
= new Date().getTime() - startTime
86 expect(result
).toBeTruthy()
87 expect(result
.f
).toBe(data
.f
)
88 expect(usedTime
).toBeGreaterThanOrEqual(2000)
91 it('Verify that maxTasks is set properly', async () => {
92 const worker
= asyncPool
.chooseWorker()
93 expect(worker
.port2
.getMaxListeners()).toBe(maxTasks
)
96 it('Shutdown test', async () => {
98 pool
.workers
.forEach(w
=> {
104 expect(closedThreads
).toBe(numberOfThreads
)
107 it('Validations test', () => {
110 const pool1
= new FixedThreadPool()
115 expect(error
).toBeTruthy()
116 expect(error
.message
).toBeTruthy()
119 it('Should work even without opts in input', async () => {
120 const pool1
= new FixedThreadPool(
122 './tests/worker-files/thread/testWorker.js'
124 const res
= await pool1
.execute({ test
: 'test' })
125 expect(res
).toBeFalsy()