a5ec3f0a8a63e720b1c789e9a23249a1ca866a4f
1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
3 const numberOfThreads
= 10
5 const pool
= new FixedThreadPool(
7 './tests/worker/thread/testWorker.js',
9 errorHandler
: e
=> console
.error(e
),
10 onlineHandler
: () => console
.log('worker is online')
13 const emptyPool
= new FixedThreadPool(1, './tests/worker/thread/emptyWorker.js')
14 const echoPool
= new FixedThreadPool(1, './tests/worker/thread/echoWorker.js')
15 const errorPool
= new FixedThreadPool(
17 './tests/worker/thread/errorWorker.js',
19 errorHandler
: e
=> console
.error(e
),
20 onlineHandler
: () => console
.log('worker is online')
23 const asyncPool
= new FixedThreadPool(
25 './tests/worker/thread/asyncWorker.js',
26 { maxTasks
: maxTasks
}
29 describe('Fixed thread pool test suite ', () => {
30 it('Choose worker round robin test', async () => {
31 const results
= new Set()
32 for (let i
= 0; i
< numberOfThreads
; i
++) {
33 results
.add(pool
.chooseWorker().threadId
)
35 expect(results
.size
).toBe(numberOfThreads
)
38 it('Verify that the function is executed in a worker thread', async () => {
39 const result
= await pool
.execute({ test
: 'test' })
40 expect(result
).toBeDefined()
41 expect(result
).toBeFalsy()
44 it('Verify that is possible to invoke the execute method without input', async () => {
45 const result
= await pool
.execute()
46 expect(result
).toBeDefined()
47 expect(result
).toBeFalsy()
50 it('Verify that is possible to have a worker that return undefined', async () => {
51 const result
= await emptyPool
.execute()
52 expect(result
).toBeFalsy()
55 it('Verify that data are sent to the worker correctly', async () => {
56 const data
= { f
: 10 }
57 const result
= await echoPool
.execute(data
)
58 expect(result
).toBeTruthy()
59 expect(result
.f
).toBe(data
.f
)
62 it('Verify that error handling is working properly', async () => {
63 const data
= { f
: 10 }
66 await errorPool
.execute(data
)
70 expect(inError
).toBeTruthy()
71 expect(inError
instanceof Error
).toBeTruthy()
72 expect(inError
.message
).toBeTruthy()
75 it('Verify that async function is working properly', async () => {
76 const data
= { f
: 10 }
77 const startTime
= new Date().getTime()
78 const result
= await asyncPool
.execute(data
)
79 const usedTime
= new Date().getTime() - startTime
80 expect(result
).toBeTruthy()
81 expect(result
.f
).toBe(data
.f
)
82 expect(usedTime
).toBeGreaterThanOrEqual(2000)
85 it('Verify that maxTasks is set properly', async () => {
86 const worker
= asyncPool
.chooseWorker()
87 expect(worker
.port2
.getMaxListeners()).toBe(maxTasks
)
90 it('Shutdown test', async () => {
92 pool
.workers
.forEach(w
=> {
98 expect(closedThreads
).toBe(numberOfThreads
)
101 it('Validations test', () => {
104 const pool1
= new FixedThreadPool()
109 expect(error
).toBeTruthy()
110 expect(error
.message
).toBeTruthy()
113 it('Should work even without opts in input', async () => {
114 const pool1
= new FixedThreadPool(1, './tests/worker/thread/testWorker.js')
115 const res
= await pool1
.execute({ test
: 'test' })
116 expect(res
).toBeFalsy()