1 const { expect
} = require('expect')
2 const { DynamicThreadPool
, PoolEvents
} = require('../../../lib')
3 const { TaskFunctions
} = require('../../test-types')
4 const { sleep
, waitWorkerEvents
} = require('../../test-utils')
6 describe('Dynamic thread pool test suite', () => {
9 const pool
= new DynamicThreadPool(
12 './tests/worker-files/thread/testWorker.js',
14 errorHandler
: (e
) => console
.error(e
)
18 it('Verify that the function is executed in a worker thread', async () => {
19 let result
= await pool
.execute({
20 function: TaskFunctions
.fibonacci
22 expect(result
).toBe(75025)
23 result
= await pool
.execute({
24 function: TaskFunctions
.factorial
26 expect(result
).toBe(9.33262154439441e157
)
29 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
31 pool
.emitter
.on(PoolEvents
.busy
, () => ++poolBusy
)
32 for (let i
= 0; i
< max
* 2; i
++) {
35 expect(pool
.workerNodes
.length
).toBeLessThanOrEqual(max
)
36 expect(pool
.workerNodes
.length
).toBeGreaterThan(min
)
37 expect(poolBusy
).toBe(1)
38 const numberOfExitEvents
= await
waitWorkerEvents(pool
, 'exit', max
- min
)
39 expect(numberOfExitEvents
).toBe(max
- min
)
42 it('Verify scale thread up and down is working', async () => {
43 expect(pool
.workerNodes
.length
).toBe(min
)
44 for (let i
= 0; i
< max
* 2; i
++) {
47 expect(pool
.workerNodes
.length
).toBe(max
)
48 await
waitWorkerEvents(pool
, 'exit', max
- min
)
49 expect(pool
.workerNodes
.length
).toBe(min
)
50 for (let i
= 0; i
< max
* 2; i
++) {
53 expect(pool
.workerNodes
.length
).toBe(max
)
54 await
waitWorkerEvents(pool
, 'exit', max
- min
)
55 expect(pool
.workerNodes
.length
).toBe(min
)
58 it('Shutdown test', async () => {
59 const exitPromise
= waitWorkerEvents(pool
, 'exit', min
)
61 pool
.emitter
.on(PoolEvents
.destroy
, () => ++poolDestroy
)
63 const numberOfExitEvents
= await exitPromise
64 expect(pool
.started
).toBe(false)
65 expect(pool
.workerNodes
.length
).toBe(0)
66 expect(numberOfExitEvents
).toBe(min
)
67 expect(poolDestroy
).toBe(1)
70 it('Validation of inputs test', () => {
71 expect(() => new DynamicThreadPool(min
)).toThrowError(
72 'Please specify a file with a worker implementation'
76 it('Should work even without opts in input', async () => {
77 const pool
= new DynamicThreadPool(
80 './tests/worker-files/thread/testWorker.js'
82 const res
= await pool
.execute()
83 expect(res
).toStrictEqual({ ok
: 1 })
84 // We need to clean up the resources after our test
88 it('Verify scale thread up and down is working when long executing task is used:hard', async () => {
89 const longRunningPool
= new DynamicThreadPool(
92 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
94 errorHandler
: (e
) => console
.error(e
),
95 onlineHandler
: () => console
.info('long executing worker is online'),
96 exitHandler
: () => console
.info('long executing worker exited')
99 expect(longRunningPool
.workerNodes
.length
).toBe(min
)
100 for (let i
= 0; i
< max
* 2; i
++) {
101 longRunningPool
.execute()
103 expect(longRunningPool
.workerNodes
.length
).toBe(max
)
104 await
waitWorkerEvents(longRunningPool
, 'exit', max
- min
)
105 expect(longRunningPool
.workerNodes
.length
).toBe(min
)
107 longRunningPool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
108 longRunningPool
.workerChoiceStrategyContext
.workerChoiceStrategy
110 ).toBeLessThan(longRunningPool
.workerNodes
.length
)
111 // We need to clean up the resources after our test
112 await longRunningPool
.destroy()
115 it('Verify scale thread up and down is working when long executing task is used:soft', async () => {
116 const longRunningPool
= new DynamicThreadPool(
119 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
121 errorHandler
: (e
) => console
.error(e
),
122 onlineHandler
: () => console
.info('long executing worker is online'),
123 exitHandler
: () => console
.info('long executing worker exited')
126 expect(longRunningPool
.workerNodes
.length
).toBe(min
)
127 for (let i
= 0; i
< max
* 2; i
++) {
128 longRunningPool
.execute()
130 expect(longRunningPool
.workerNodes
.length
).toBe(max
)
132 // Here we expect the workerNodes to be at the max size since the task is still executing
133 expect(longRunningPool
.workerNodes
.length
).toBe(max
)
134 // We need to clean up the resources after our test
135 await longRunningPool
.destroy()
138 it('Verify that a pool with zero worker can be instantiated', async () => {
139 const pool
= new DynamicThreadPool(
142 './tests/worker-files/thread/testWorker.js'
144 expect(pool
).toBeInstanceOf(DynamicThreadPool
)
145 // We need to clean up the resources after our test