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 // await Promise.all(promises)
30 expect(pool
.workers
.length
).toBeLessThanOrEqual(max
)
31 expect(pool
.workers
.length
).toBeGreaterThan(min
)
32 // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
33 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
34 expect(poolBusy
).toBe(max
+ 1)
35 const numberOfExitEvents
= await TestUtils
.waitExits(pool
, max
- min
)
36 expect(numberOfExitEvents
).toBe(max
- min
)
39 it('Verify scale thread up and down is working', async () => {
40 expect(pool
.workers
.length
).toBe(min
)
41 for (let i
= 0; i
< max
* 10; i
++) {
42 pool
.execute({ test
: 'test' })
44 expect(pool
.workers
.length
).toBe(max
)
45 await TestUtils
.waitExits(pool
, max
- min
)
46 expect(pool
.workers
.length
).toBe(min
)
47 for (let i
= 0; i
< max
* 10; i
++) {
48 pool
.execute({ test
: 'test' })
50 expect(pool
.workers
.length
).toBe(max
)
51 await TestUtils
.waitExits(pool
, max
- min
)
52 expect(pool
.workers
.length
).toBe(min
)
55 it('Shutdown test', async () => {
56 const exitPromise
= TestUtils
.waitExits(pool
, min
)
58 const numberOfExitEvents
= await exitPromise
59 expect(numberOfExitEvents
).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
).toBeDefined()
76 expect(res
).toBeFalsy()
77 // We need to clean up the resources after our test
81 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
82 const longRunningPool
= new DynamicThreadPool(
85 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
87 errorHandler
: e
=> console
.error(e
),
88 onlineHandler
: () => console
.log('long running worker is online'),
89 exitHandler
: () => console
.log('long running worker exited')
92 expect(longRunningPool
.workers
.length
).toBe(min
)
93 for (let i
= 0; i
< max
* 10; i
++) {
94 longRunningPool
.execute({ test
: 'test' })
96 expect(longRunningPool
.workers
.length
).toBe(max
)
97 await TestUtils
.waitExits(longRunningPool
, max
- min
)
98 expect(longRunningPool
.workers
.length
).toBe(min
)
99 // We need to clean up the resources after our test
100 await longRunningPool
.destroy()
103 it('Verify scale thread up and down is working when long running task is used:soft', async () => {
104 const longRunningPool
= new DynamicThreadPool(
107 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
109 errorHandler
: e
=> console
.error(e
),
110 onlineHandler
: () => console
.log('long running worker is online'),
111 exitHandler
: () => console
.log('long running worker exited')
114 expect(longRunningPool
.workers
.length
).toBe(min
)
115 for (let i
= 0; i
< max
* 10; i
++) {
116 longRunningPool
.execute({ test
: 'test' })
118 expect(longRunningPool
.workers
.length
).toBe(max
)
119 await TestUtils
.sleep(1500)
120 // Here we expect the workers to be at the max size since that the task is still running
121 expect(longRunningPool
.workers
.length
).toBe(max
)
122 // We need to clean up the resources after our test
123 await longRunningPool
.destroy()
126 it('Verify that a pool with zero worker can be instantiated', async () => {
127 const pool
= new DynamicThreadPool(
130 './tests/worker-files/thread/testWorker.js'
132 expect(pool
).toBeInstanceOf(DynamicThreadPool
)
133 // We need to clean up the resources after our test