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',
15 { exitHandler
: () => console
.log('WORKER EXITED') }
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'
34 describe('Fixed thread pool test suite', () => {
35 after('Destroy all pools', async () => {
36 // We need to clean up the resources after our test
37 await echoPool
.destroy()
38 await asyncPool
.destroy()
39 await errorPool
.destroy()
40 await emptyPool
.destroy()
43 it('Choose worker round robin test', async () => {
44 const results
= new Set()
45 for (let i
= 0; i
< numberOfThreads
; i
++) {
46 results
.add(pool
.chooseWorker().threadId
)
48 expect(results
.size
).toBe(numberOfThreads
)
51 it('Verify that the function is executed in a worker thread', async () => {
52 const result
= await pool
.execute({ test
: 'test' })
53 expect(result
).toBeDefined()
54 expect(result
).toBeFalsy()
57 it('Verify that is possible to invoke the execute method without input', async () => {
58 const result
= await pool
.execute()
59 expect(result
).toBeDefined()
60 expect(result
).toBeFalsy()
63 it('Verify that is possible to have a worker that return undefined', async () => {
64 const result
= await emptyPool
.execute()
65 expect(result
).toBeFalsy()
68 it('Verify that data are sent to the worker correctly', async () => {
69 const data
= { f
: 10 }
70 const result
= await echoPool
.execute(data
)
71 expect(result
).toBeTruthy()
72 expect(result
.f
).toBe(data
.f
)
75 it('Verify that error handling is working properly', async () => {
76 const data
= { f
: 10 }
79 await errorPool
.execute(data
)
83 expect(inError
).toBeTruthy()
84 expect(inError
instanceof Error
).toBeTruthy()
85 expect(inError
.message
).toBeTruthy()
88 it('Verify that async function is working properly', async () => {
89 const data
= { f
: 10 }
90 const startTime
= new Date().getTime()
91 const result
= await asyncPool
.execute(data
)
92 const usedTime
= new Date().getTime() - startTime
93 expect(result
).toBeTruthy()
94 expect(result
.f
).toBe(data
.f
)
95 expect(usedTime
).toBeGreaterThanOrEqual(2000)
98 it('Shutdown test', async () => {
99 const exitPromise
= TestUtils
.waitExits(pool
, numberOfThreads
)
101 const res
= await exitPromise
102 expect(res
).toBe(numberOfThreads
)
105 it('Should work even without opts in input', async () => {
106 const pool1
= new FixedThreadPool(
108 './tests/worker-files/thread/testWorker.js'
110 const res
= await pool1
.execute({ test
: 'test' })
111 expect(res
).toBeFalsy()
112 // We need to clean up the resources after our test
113 await pool1
.destroy()
116 it('Verify that a pool with zero worker fails', async () => {
118 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
119 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))