1 const expect
= require('expect')
2 const DynamicThreadPool
= require('../lib/dynamic')
5 const pool
= new DynamicThreadPool(min
, max
, './tests/workers/testWorker.js', {
6 errorHandler
: e
=> console
.error(e
),
7 onlineHandler
: () => console
.log('worker is online')
10 describe('Dynamic thread pool test suite ', () => {
11 it('Verify that the function is executed in a worker thread', async () => {
12 const result
= await pool
.execute({ test
: 'test' })
13 expect(result
).toBeDefined()
14 expect(result
).toBeFalsy()
17 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
21 pool
.emitter
.on('FullPool', () => fullPool
++)
22 for (let i
= 0; i
< max
* 2; i
++) {
23 promises
.push(pool
.execute({ test
: 'test' }))
25 expect(pool
.workers
.length
).toBe(max
)
26 pool
.workers
.forEach(w
=> {
31 expect(fullPool
> 1).toBeTruthy()
32 await
new Promise(resolve
=> setTimeout(resolve
, 2000))
33 expect(closedThreads
).toBe(max
- min
)
36 it('Verify scale thread up and down is working', async () => {
37 expect(pool
.workers
.length
).toBe(min
)
38 for (let i
= 0; i
< max
* 10; i
++) {
39 pool
.execute({ test
: 'test' })
41 expect(pool
.workers
.length
).toBe(max
)
42 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
43 expect(pool
.workers
.length
).toBe(min
)
44 for (let i
= 0; i
< max
* 10; i
++) {
45 pool
.execute({ test
: 'test' })
47 expect(pool
.workers
.length
).toBe(max
)
48 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
49 expect(pool
.workers
.length
).toBe(min
)
51 it('Shutdown test', async () => {
53 pool
.workers
.forEach(w
=> {
59 expect(closedThreads
).toBe(min
)
62 it('Validations test', () => {
65 const pool1
= new DynamicThreadPool()
70 expect(error
).toBeTruthy()
71 expect(error
.message
).toBeTruthy()
74 it('Should work even without opts in input', async () => {
75 const pool1
= new DynamicThreadPool(1, 1, './tests/workers/testWorker.js')
76 const res
= await pool1
.execute({ test
: 'test' })
77 expect(res
).toBeFalsy()