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
)
12 const emptyPool
= new FixedThreadPool(
14 './tests/worker-files/thread/emptyWorker.js'
16 const echoPool
= new FixedThreadPool(
18 './tests/worker-files/thread/echoWorker.js'
20 const errorPool
= new FixedThreadPool(
22 './tests/worker-files/thread/errorWorker.js',
24 errorHandler
: e
=> console
.error(e
),
25 onlineHandler
: () => console
.log('worker is online')
28 const asyncPool
= new FixedThreadPool(
30 './tests/worker-files/thread/asyncWorker.js',
31 { maxTasks
: maxTasks
}
34 describe('Fixed thread pool test suite ', () => {
35 it('Choose worker round robin test', async () => {
36 const results
= new Set()
37 for (let i
= 0; i
< numberOfThreads
; i
++) {
38 results
.add(pool
.chooseWorker().threadId
)
40 expect(results
.size
).toBe(numberOfThreads
)
43 it('Verify that the function is executed in a worker thread', async () => {
44 const result
= await pool
.execute({ test
: 'test' })
45 expect(result
).toBeDefined()
46 expect(result
).toBeFalsy()
49 it('Verify that is possible to invoke the execute method without input', async () => {
50 const result
= await pool
.execute()
51 expect(result
).toBeDefined()
52 expect(result
).toBeFalsy()
55 it('Verify that is possible to have a worker that return undefined', async () => {
56 const result
= await emptyPool
.execute()
57 expect(result
).toBeFalsy()
60 it('Verify that data are sent to the worker correctly', async () => {
61 const data
= { f
: 10 }
62 const result
= await echoPool
.execute(data
)
63 expect(result
).toBeTruthy()
64 expect(result
.f
).toBe(data
.f
)
67 it('Verify that error handling is working properly', async () => {
68 const data
= { f
: 10 }
71 await errorPool
.execute(data
)
75 expect(inError
).toBeTruthy()
76 expect(inError
instanceof Error
).toBeTruthy()
77 expect(inError
.message
).toBeTruthy()
80 it('Verify that async function is working properly', async () => {
81 const data
= { f
: 10 }
82 const startTime
= new Date().getTime()
83 const result
= await asyncPool
.execute(data
)
84 const usedTime
= new Date().getTime() - startTime
85 expect(result
).toBeTruthy()
86 expect(result
.f
).toBe(data
.f
)
87 expect(usedTime
).toBeGreaterThanOrEqual(2000)
90 it('Verify that maxTasks is set properly', async () => {
91 const worker
= asyncPool
.chooseWorker()
92 expect(worker
.port2
.getMaxListeners()).toBe(maxTasks
)
95 it('Shutdown test', async () => {
97 pool
.workers
.forEach(w
=> {
103 expect(closedThreads
).toBe(numberOfThreads
)
106 it('Validations test', () => {
109 const pool1
= new FixedThreadPool()
114 expect(error
).toBeTruthy()
115 expect(error
.message
).toBeTruthy()
118 it('Should work even without opts in input', async () => {
119 const pool1
= new FixedThreadPool(
121 './tests/worker-files/thread/testWorker.js'
123 const res
= await pool1
.execute({ test
: 'test' })
124 expect(res
).toBeFalsy()