b0cf9e576517dfe9179747ed734f38544ea2c107
1 const expect
= require('expect')
2 const { FixedThreadPool
} = require('../../../lib/index')
4 const pool
= new FixedThreadPool(
6 './tests/worker/thread/testWorker.js',
8 errorHandler
: e
=> console
.error(e
),
9 onlineHandler
: () => console
.log('worker is online')
12 const emptyPool
= new FixedThreadPool(1, './tests/worker/thread/emptyWorker.js')
13 const echoPool
= new FixedThreadPool(1, './tests/worker/thread/echoWorker.js')
14 const errorPool
= new FixedThreadPool(
16 './tests/worker/thread/errorWorker.js',
18 errorHandler
: e
=> console
.error(e
),
19 onlineHandler
: () => console
.log('worker is online')
22 const asyncPool
= new FixedThreadPool(1, './tests/worker/thread/asyncWorker.js')
24 describe('Fixed thread pool test suite ', () => {
25 it('Choose worker round robin test', async () => {
26 const results
= new Set()
27 for (let i
= 0; i
< numThreads
; i
++) {
28 results
.add(pool
.chooseWorker().threadId
)
30 expect(results
.size
).toBe(numThreads
)
33 it('Verify that the function is executed in a worker thread', async () => {
34 const result
= await pool
.execute({ test
: 'test' })
35 expect(result
).toBeDefined()
36 expect(result
).toBeFalsy()
39 it('Verify that is possible to invoke the execute method without input', async () => {
40 const result
= await pool
.execute()
41 expect(result
).toBeDefined()
42 expect(result
).toBeFalsy()
45 it('Verify that is possible to have a worker that return undefined', async () => {
46 const result
= await emptyPool
.execute()
47 expect(result
).toBeFalsy()
50 it('Verify that data are sent to the worker correctly', async () => {
51 const data
= { f
: 10 }
52 const result
= await echoPool
.execute(data
)
53 expect(result
).toBeTruthy()
54 expect(result
.f
).toBe(data
.f
)
57 it('Verify that error handling is working properly', async () => {
58 const data
= { f
: 10 }
61 await errorPool
.execute(data
)
65 expect(inError
).toBeTruthy()
66 expect(inError
instanceof Error
).toBeTruthy()
67 expect(inError
.message
).toBeTruthy()
70 it('Verify that async function is working properly', async () => {
71 const data
= { f
: 10 }
72 const startTime
= new Date().getTime()
73 const result
= await asyncPool
.execute(data
)
74 const usedTime
= new Date().getTime() - startTime
75 expect(result
).toBeTruthy()
76 expect(result
.f
).toBe(data
.f
)
77 expect(usedTime
).toBeGreaterThanOrEqual(2000)
80 it('Shutdown test', async () => {
82 pool
.workers
.forEach(w
=> {
88 expect(closedThreads
).toBe(numThreads
)
91 it('Validations test', () => {
94 const pool1
= new FixedThreadPool()
99 expect(error
).toBeTruthy()
100 expect(error
.message
).toBeTruthy()
103 it('Should work even without opts in input', async () => {
104 const pool1
= new FixedThreadPool(1, './tests/worker/thread/testWorker.js')
105 const res
= await pool1
.execute({ test
: 'test' })
106 expect(res
).toBeFalsy()