1 const expect
= require('expect')
2 const { DynamicThreadPool
} = require('../../../lib/index')
3 const TestUtils
= require('../../test-utils')
6 const pool
= new DynamicThreadPool(
9 './tests/worker-files/thread/testWorker.js',
11 errorHandler
: e
=> console
.error(e
)
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 () => {
25 pool
.emitter
.on('busy', () => busy
++)
26 for (let i
= 0; i
< max
* 2; i
++) {
27 promises
.push(pool
.execute({ test
: 'test' }))
29 expect(pool
.workers
.length
).toBe(max
)
30 expect(busy
> 1).toBeTruthy()
31 const res
= await TestUtils
.waitExits(pool
, max
- min
)
32 expect(res
).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 TestUtils
.waitExits(pool
, max
- min
)
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 TestUtils
.waitExits(pool
, max
- min
)
48 expect(pool
.workers
.length
).toBe(min
)
51 it('Shutdown test', async () => {
53 pool
.workers
.forEach(w
=> {
59 expect(closedThreads
).toBe(min
)
62 it('Validation of inputs test', () => {
63 expect(() => new DynamicThreadPool(min
)).toThrowError(
64 new Error('Please specify a file with a worker implementation')
68 it('Should work even without opts in input', async () => {
69 const pool1
= new DynamicThreadPool(
72 './tests/worker-files/thread/testWorker.js'
74 const res
= await pool1
.execute({ test
: 'test' })
75 expect(res
).toBeFalsy()
76 // We need to clean up the resources after our test
80 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
81 const longRunningPool
= new DynamicThreadPool(
84 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
86 errorHandler
: e
=> console
.error(e
),
87 onlineHandler
: () => console
.log('worker is online')
90 expect(longRunningPool
.workers
.length
).toBe(min
)
91 for (let i
= 0; i
< max
* 10; i
++) {
92 longRunningPool
.execute({ test
: 'test' })
94 expect(longRunningPool
.workers
.length
).toBe(max
)
95 await TestUtils
.waitExits(longRunningPool
, max
- min
)
96 expect(longRunningPool
.workers
.length
).toBe(min
)
97 // We need to clean up the resources after our test
98 await longRunningPool
.destroy()
101 it('Verify scale thread up and down is working when long running task is used:soft', async () => {
102 const longRunningPool
= new DynamicThreadPool(
105 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
107 errorHandler
: e
=> console
.error(e
),
108 onlineHandler
: () => console
.log('worker is online')
111 expect(longRunningPool
.workers
.length
).toBe(min
)
112 for (let i
= 0; i
< max
* 10; i
++) {
113 longRunningPool
.execute({ test
: 'test' })
115 expect(longRunningPool
.workers
.length
).toBe(max
)
116 await TestUtils
.sleep(1500)
117 // Here we expect the workers to be at the max size since that the task is still running
118 expect(longRunningPool
.workers
.length
).toBe(max
)
119 // We need to clean up the resources after our test
120 await longRunningPool
.destroy()
123 it('Verify that a pool with zero worker can be instantiated', async () => {
124 const pool
= new DynamicThreadPool(
127 './tests/worker-files/thread/testWorker.js'
129 expect(pool
).toBeInstanceOf(DynamicThreadPool
)
130 // We need to clean up the resources after our test