15195012be80f25db7750f80de1fbebdf5614765
1 const expect
= require('expect')
2 const DynamicThreadPool
= require('../lib/dynamic')
5 const pool
= new DynamicThreadPool(min
, max
,
6 './tests/workers/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 () => {
20 pool
.emitter
.on('FullPool', () => fullPool
++)
21 for (let i
= 0; i
< (max
* 2); i
++) {
22 promises
.push(pool
.execute({ test
: 'test' }))
24 expect(pool
.workers
.length
).toBe(max
)
25 pool
.workers
.forEach(w
=> {
30 expect(fullPool
> 1).toBeTruthy()
31 await
new Promise(resolve
=> setTimeout(resolve
, 2000))
32 expect(closedThreads
).toBe(max
- min
)
35 it('Verify scale thread up and down is working', async () => {
36 expect(pool
.workers
.length
).toBe(min
)
37 for (let i
= 0; i
< max
* 10; i
++) {
38 pool
.execute({ test
: 'test' })
40 expect(pool
.workers
.length
).toBe(max
)
41 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
42 expect(pool
.workers
.length
).toBe(min
)
43 for (let i
= 0; i
< max
* 10; i
++) {
44 pool
.execute({ test
: 'test' })
46 expect(pool
.workers
.length
).toBe(max
)
47 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
48 expect(pool
.workers
.length
).toBe(min
)
50 it('Shutdown test', async () => {
52 pool
.workers
.forEach(w
=> {
58 expect(closedThreads
).toBe(min
)
61 it('Validations test', () => {
64 const pool1
= new DynamicThreadPool()
69 expect(error
).toBeTruthy()
70 expect(error
.message
).toBeTruthy()
73 it('Should work even without opts in input', async () => {
74 const pool1
= new DynamicThreadPool(1, 1, './tests/workers/testWorker.js')
75 const res
= await pool1
.execute({ test
: 'test' })
76 expect(res
).toBeFalsy()