1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const TestUtils
= require('../../test-utils')
4 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'
33 describe('Fixed thread pool test suite', () => {
34 after('Destroy all pools', async () => {
35 // We need to clean up the resources after our test
36 await echoPool
.destroy()
37 await asyncPool
.destroy()
38 await errorPool
.destroy()
39 await emptyPool
.destroy()
42 it('Choose worker round robin test', async () => {
43 const results
= new Set()
44 for (let i
= 0; i
< numberOfThreads
; i
++) {
45 results
.add(pool
.chooseWorker().threadId
)
47 expect(results
.size
).toBe(numberOfThreads
)
50 it('Verify that the function is executed in a worker thread', async () => {
51 const result
= await pool
.execute({ test
: 'test' })
52 expect(result
).toBeDefined()
53 expect(result
).toBeFalsy()
56 it('Verify that is possible to invoke the execute method without input', async () => {
57 const result
= await pool
.execute()
58 expect(result
).toBeDefined()
59 expect(result
).toBeFalsy()
62 it('Verify that is possible to have a worker that return undefined', async () => {
63 const result
= await emptyPool
.execute()
64 expect(result
).toBeFalsy()
67 it('Verify that data are sent to the worker correctly', async () => {
68 const data
= { f
: 10 }
69 const result
= await echoPool
.execute(data
)
70 expect(result
).toBeTruthy()
71 expect(result
.f
).toBe(data
.f
)
74 it('Verify that error handling is working properly', async () => {
75 const data
= { f
: 10 }
78 await errorPool
.execute(data
)
82 expect(inError
).toBeTruthy()
83 expect(inError
instanceof Error
).toBeTruthy()
84 expect(inError
.message
).toBeTruthy()
87 it('Verify that async function is working properly', async () => {
88 const data
= { f
: 10 }
89 const startTime
= new Date().getTime()
90 const result
= await asyncPool
.execute(data
)
91 const usedTime
= new Date().getTime() - startTime
92 expect(result
).toBeTruthy()
93 expect(result
.f
).toBe(data
.f
)
94 expect(usedTime
).toBeGreaterThanOrEqual(2000)
97 it('Shutdown test', async () => {
98 const exitPromise
= TestUtils
.waitExits(pool
, numberOfThreads
)
100 const res
= await exitPromise
101 expect(res
).toBe(numberOfThreads
)
104 it('Should work even without opts in input', async () => {
105 const pool1
= new FixedThreadPool(
107 './tests/worker-files/thread/testWorker.js'
109 const res
= await pool1
.execute({ test
: 'test' })
110 expect(res
).toBeFalsy()
111 // We need to clean up the resources after our test
112 await pool1
.destroy()
115 it('Verify that a pool with zero worker fails', async () => {
117 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
118 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))