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', () => poolBusy
++)
26 for (let i
= 0; i
< max
* 2; i
++) {
27 promises
.push(pool
.execute({ test
: 'test' }))
29 expect(pool
.workers
.length
).toBe(max
)
30 // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
31 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
32 expect(poolBusy
).toBe(max
+ 1)
33 const numberOfExitEvents
= await TestUtils
.waitExits(pool
, max
- min
)
34 expect(numberOfExitEvents
).toBe(max
- min
)
37 it('Verify scale thread up and down is working', async () => {
38 expect(pool
.workers
.length
).toBe(min
)
39 for (let i
= 0; i
< max
* 10; i
++) {
40 pool
.execute({ test
: 'test' })
42 expect(pool
.workers
.length
).toBe(max
)
43 await TestUtils
.waitExits(pool
, max
- min
)
44 expect(pool
.workers
.length
).toBe(min
)
45 for (let i
= 0; i
< max
* 10; i
++) {
46 pool
.execute({ test
: 'test' })
48 expect(pool
.workers
.length
).toBe(max
)
49 await TestUtils
.waitExits(pool
, max
- min
)
50 expect(pool
.workers
.length
).toBe(min
)
53 it('Shutdown test', async () => {
54 const exitPromise
= TestUtils
.waitExits(pool
, min
)
56 const numberOfExitEvents
= await exitPromise
57 expect(numberOfExitEvents
).toBe(min
)
60 it('Validation of inputs test', () => {
61 expect(() => new DynamicThreadPool(min
)).toThrowError(
62 new Error('Please specify a file with a worker implementation')
66 it('Should work even without opts in input', async () => {
67 const pool1
= new DynamicThreadPool(
70 './tests/worker-files/thread/testWorker.js'
72 const res
= await pool1
.execute({ test
: 'test' })
73 expect(res
).toBeDefined()
74 expect(res
).toBeFalsy()
75 // We need to clean up the resources after our test
79 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
80 const longRunningPool
= new DynamicThreadPool(
83 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
85 errorHandler
: e
=> console
.error(e
),
86 onlineHandler
: () => console
.log('long running worker is online'),
87 exitHandler
: () => console
.log('long running worker exited')
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('long running worker is online'),
109 exitHandler
: () => console
.log('long running worker exited')
112 expect(longRunningPool
.workers
.length
).toBe(min
)
113 for (let i
= 0; i
< max
* 10; i
++) {
114 longRunningPool
.execute({ test
: 'test' })
116 expect(longRunningPool
.workers
.length
).toBe(max
)
117 await TestUtils
.sleep(1500)
118 // Here we expect the workers to be at the max size since that the task is still running
119 expect(longRunningPool
.workers
.length
).toBe(max
)
120 // We need to clean up the resources after our test
121 await longRunningPool
.destroy()
124 it('Verify that a pool with zero worker can be instantiated', async () => {
125 const pool
= new DynamicThreadPool(
128 './tests/worker-files/thread/testWorker.js'
130 expect(pool
).toBeInstanceOf(DynamicThreadPool
)
131 // We need to clean up the resources after our test