d59b98587c4f6c225ba9d0f6038b1a54bc6d2f33
1 const expect
= require('expect')
2 const { DynamicThreadPool
} = require('../../../lib/index')
5 const pool
= new DynamicThreadPool(
8 './tests/worker/thread/testWorker.js',
10 errorHandler
: e
=> console
.error(e
),
11 onlineHandler
: () => console
.log('worker is online')
15 describe('Dynamic thread pool test suite ', () => {
16 it('Verify that the function is executed in a worker thread', async () => {
17 const result
= await pool
.execute({ test
: 'test' })
18 expect(result
).toBeDefined()
19 expect(result
).toBeFalsy()
22 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
26 pool
.emitter
.on('FullPool', () => fullPool
++)
27 for (let i
= 0; i
< max
* 2; i
++) {
28 promises
.push(pool
.execute({ test
: 'test' }))
30 expect(pool
.workers
.length
).toBe(max
)
31 pool
.workers
.forEach(w
=> {
36 expect(fullPool
> 1).toBeTruthy()
37 await
new Promise(resolve
=> setTimeout(resolve
, 2000))
38 expect(closedThreads
).toBe(max
- min
)
41 it('Verify scale thread up and down is working', async () => {
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
)
49 for (let i
= 0; i
< max
* 10; i
++) {
50 pool
.execute({ test
: 'test' })
52 expect(pool
.workers
.length
).toBe(max
)
53 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
54 expect(pool
.workers
.length
).toBe(min
)
57 it('Shutdown test', async () => {
59 pool
.workers
.forEach(w
=> {
65 expect(closedThreads
).toBe(min
)
68 it('Validations test', () => {
71 const pool1
= new DynamicThreadPool()
76 expect(error
).toBeTruthy()
77 expect(error
.message
).toBeTruthy()
80 it('Should work even without opts in input', async () => {
81 const pool1
= new DynamicThreadPool(
84 './tests/worker/thread/testWorker.js'
86 const res
= await pool1
.execute({ test
: 'test' })
87 expect(res
).toBeFalsy()
90 it('Verify scale thread up and down is working when long running task is used', async () => {
91 const longRunningPool
= new DynamicThreadPool(
94 './tests/worker/thread/longRunningWorker.js',
96 errorHandler
: e
=> console
.error(e
),
97 onlineHandler
: () => console
.log('worker is online')
100 expect(longRunningPool
.workers
.length
).toBe(min
)
101 for (let i
= 0; i
< max
* 10; i
++) {
102 longRunningPool
.execute({ test
: 'test' })
104 expect(longRunningPool
.workers
.length
).toBe(max
)
105 await
new Promise(resolve
=> setTimeout(resolve
, 1000))
106 // Here we expect the workers to be at the max size since that the task is still running
107 expect(longRunningPool
.workers
.length
).toBe(max
)