7b0865bb9f951c9c038f2c0c80f6bf6440e81656
1 const expect
= require('expect')
2 const DynamicThreadPool
= require('../lib/dynamic')
5 const pool
= new DynamicThreadPool(min
, max
,
6 './tests/testWorker.js',
7 { errorHandler
: (e
) => console
.error(e
), onlineHandler
: () => console
.log('worker is online') })
9 describe('Dynamic thread pool test suite ', () => {
10 it('Verify that the function is executed in a worker thread', async () => {
11 const result
= await pool
.execute({ test
: 'test' })
12 expect(result
).toBeDefined()
13 expect(result
).toBeFalsy()
16 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
19 for (let i
= 0; i
< (max
* 3); i
++) {
20 promises
.push(pool
.execute({ test
: 'test' }))
22 expect(pool
.workers
.length
).toBe(max
)
23 pool
.workers
.forEach(w
=> {
28 await
new Promise(resolve
=> setTimeout(resolve
, 1000 * 2))
29 expect(closedThreads
).toBe(max
- min
)
32 it('Shutdown test', async () => {
34 pool
.workers
.forEach(w
=> {
40 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
41 expect(closedThreads
).toBe(min
)
44 it('Validations test', () => {
47 const pool1
= new DynamicThreadPool()
52 expect(error
).toBeTruthy()
53 expect(error
.message
).toBeTruthy()
56 it('Should work even without opts in input', async () => {
57 const pool1
= new DynamicThreadPool(1, 1, './tests/testWorker.js')
58 const res
= await pool1
.execute({ test
: 'test' })
59 expect(res
).toBeFalsy()