cea5502508261b85bf96cc680ab4021e81498ad7
1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const TestUtils
= require('../../test-utils')
4 const numberOfThreads
= 10
6 const pool
= new FixedThreadPool(
8 './tests/worker-files/thread/testWorker.js',
10 errorHandler
: e
=> console
.error(e
)
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 after('Destroy all pools', async () => {
37 // We need to clean up the resources after our test
38 await echoPool
.destroy()
39 await asyncPool
.destroy()
40 await errorPool
.destroy()
41 await emptyPool
.destroy()
44 it('Choose worker round robin test', async () => {
45 const results
= new Set()
46 for (let i
= 0; i
< numberOfThreads
; i
++) {
47 results
.add(pool
.chooseWorker().threadId
)
49 expect(results
.size
).toBe(numberOfThreads
)
52 it('Verify that the function is executed in a worker thread', async () => {
53 const result
= await pool
.execute({ test
: 'test' })
54 expect(result
).toBeDefined()
55 expect(result
).toBeFalsy()
58 it('Verify that is possible to invoke the execute method without input', async () => {
59 const result
= await pool
.execute()
60 expect(result
).toBeDefined()
61 expect(result
).toBeFalsy()
64 it('Verify that is possible to have a worker that return undefined', async () => {
65 const result
= await emptyPool
.execute()
66 expect(result
).toBeFalsy()
69 it('Verify that data are sent to the worker correctly', async () => {
70 const data
= { f
: 10 }
71 const result
= await echoPool
.execute(data
)
72 expect(result
).toBeTruthy()
73 expect(result
.f
).toBe(data
.f
)
76 it('Verify that error handling is working properly', async () => {
77 const data
= { f
: 10 }
80 await errorPool
.execute(data
)
84 expect(inError
).toBeTruthy()
85 expect(inError
instanceof Error
).toBeTruthy()
86 expect(inError
.message
).toBeTruthy()
89 it('Verify that async function is working properly', async () => {
90 const data
= { f
: 10 }
91 const startTime
= new Date().getTime()
92 const result
= await asyncPool
.execute(data
)
93 const usedTime
= new Date().getTime() - startTime
94 expect(result
).toBeTruthy()
95 expect(result
.f
).toBe(data
.f
)
96 expect(usedTime
).toBeGreaterThanOrEqual(2000)
99 it('Verify that maxTasks is set properly', async () => {
100 const worker
= asyncPool
.chooseWorker()
101 expect(worker
.port2
.getMaxListeners()).toBe(maxTasks
)
104 it('Shutdown test', async () => {
105 const exitPromise
= TestUtils
.waitExits(pool
, numberOfThreads
)
107 const res
= await exitPromise
108 expect(res
).toBe(numberOfThreads
)
111 it('Should work even without opts in input', async () => {
112 const pool1
= new FixedThreadPool(
114 './tests/worker-files/thread/testWorker.js'
116 const res
= await pool1
.execute({ test
: 'test' })
117 expect(res
).toBeFalsy()
118 // We need to clean up the resources after our test
119 await pool1
.destroy()