1 const expect
= require('expect')
2 const FixedThreadPool
= require('../lib/fixed')
4 const pool
= new FixedThreadPool(numThreads
, './tests/workers/testWorker.js', {
5 errorHandler
: e
=> console
.error(e
),
6 onlineHandler
: () => console
.log('worker is online')
8 const emptyPool
= new FixedThreadPool(1, './tests/workers/emptyWorker.js')
9 const echoPool
= new FixedThreadPool(1, './tests/workers/echoWorker.js')
10 const errorPool
= new FixedThreadPool(1, './tests/workers/errorWorker.js', {
11 errorHandler
: e
=> console
.error(e
),
12 onlineHandler
: () => console
.log('worker is online')
14 const asyncPool
= new FixedThreadPool(1, './tests/workers/asyncWorker.js')
16 describe('Fixed thread pool test suite ', () => {
17 it('Choose worker round robin test', async () => {
18 const results
= new Set()
19 for (let i
= 0; i
< numThreads
; i
++) {
20 results
.add(pool
._chooseWorker().threadId
)
22 expect(results
.size
).toBe(numThreads
)
25 it('Verify that the function is executed in a worker thread', async () => {
26 const result
= await pool
.execute({ test
: 'test' })
27 expect(result
).toBeDefined()
28 expect(result
).toBeFalsy()
31 it('Verify that is possible to invoke the execute method without input', async () => {
32 const result
= await pool
.execute()
33 expect(result
).toBeDefined()
34 expect(result
).toBeFalsy()
37 it('Verify that is possible to have a worker that return undefined', async () => {
38 const result
= await emptyPool
.execute()
39 expect(result
).toBeFalsy()
42 it('Verify that data are sent to the worker correctly', async () => {
43 const data
= { f
: 10 }
44 const result
= await echoPool
.execute(data
)
45 expect(result
).toBeTruthy()
46 expect(result
.f
).toBe(data
.f
)
49 it('Verify that error handling is working properly', async () => {
50 const data
= { f
: 10 }
53 await errorPool
.execute(data
)
57 expect(inError
).toBeTruthy()
58 expect(inError
instanceof Error
).toBeTruthy()
59 expect(inError
.message
).toBeTruthy()
62 it('Verify that async function is working properly', async () => {
63 const data
= { f
: 10 }
64 const startTime
= new Date().getTime()
65 const result
= await asyncPool
.execute(data
)
66 const usedTime
= new Date().getTime() - startTime
67 expect(result
).toBeTruthy()
68 expect(result
.f
).toBe(data
.f
)
69 expect(usedTime
).toBeGreaterThanOrEqual(2000)
72 it('Shutdown test', async () => {
74 pool
.workers
.forEach(w
=> {
80 expect(closedThreads
).toBe(numThreads
)
83 it('Validations test', () => {
86 const pool1
= new FixedThreadPool()
91 expect(error
).toBeTruthy()
92 expect(error
.message
).toBeTruthy()
95 it('Should work even without opts in input', async () => {
96 const pool1
= new FixedThreadPool(1, './tests/workers/testWorker.js')
97 const res
= await pool1
.execute({ test
: 'test' })
98 expect(res
).toBeFalsy()