b544452fce8351a9ed9f10136f45547b53b2b343
1 const { expect
} = require('expect')
2 const { DynamicThreadPool
} = require('../../../lib/index')
3 const { WorkerFunctions
} = require('../../test-types')
4 const TestUtils
= 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: WorkerFunctions
.fibonacci
22 expect(result
).toBe(false)
23 result
= await pool
.execute({
24 function: WorkerFunctions
.factorial
26 expect(result
).toBe(false)
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('busy', () => ++poolBusy
)
32 for (let i
= 0; i
< max
* 2; i
++) {
35 expect(pool
.workers
.size
).toBeLessThanOrEqual(max
)
36 expect(pool
.workers
.size
).toBeGreaterThan(min
)
37 // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
38 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
39 expect(poolBusy
).toBe(max
+ 1)
40 const numberOfExitEvents
= await TestUtils
.waitExits(pool
, max
- min
)
41 expect(numberOfExitEvents
).toBe(max
- min
)
44 it('Verify scale thread up and down is working', async () => {
45 expect(pool
.workers
.size
).toBe(min
)
46 for (let i
= 0; i
< max
* 10; i
++) {
49 expect(pool
.workers
.size
).toBe(max
)
50 await TestUtils
.waitExits(pool
, max
- min
)
51 expect(pool
.workers
.size
).toBe(min
)
52 for (let i
= 0; i
< max
* 10; i
++) {
55 expect(pool
.workers
.size
).toBe(max
)
56 await TestUtils
.waitExits(pool
, max
- min
)
57 expect(pool
.workers
.size
).toBe(min
)
60 it('Shutdown test', async () => {
61 const exitPromise
= TestUtils
.waitExits(pool
, min
)
63 const numberOfExitEvents
= await exitPromise
64 expect(numberOfExitEvents
).toBe(min
)
67 it('Validation of inputs test', () => {
68 expect(() => new DynamicThreadPool(min
)).toThrowError(
69 new Error('Please specify a file with a worker implementation')
73 it('Should work even without opts in input', async () => {
74 const pool1
= new DynamicThreadPool(
77 './tests/worker-files/thread/testWorker.js'
79 const res
= await pool1
.execute()
80 expect(res
).toBe(false)
81 // We need to clean up the resources after our test
85 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
86 const longRunningPool
= new DynamicThreadPool(
89 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
91 errorHandler
: e
=> console
.error(e
),
92 onlineHandler
: () => console
.log('long running worker is online'),
93 exitHandler
: () => console
.log('long running worker exited')
96 expect(longRunningPool
.workers
.size
).toBe(min
)
97 for (let i
= 0; i
< max
* 10; i
++) {
98 longRunningPool
.execute()
100 expect(longRunningPool
.workers
.size
).toBe(max
)
101 await TestUtils
.waitExits(longRunningPool
, max
- min
)
102 expect(longRunningPool
.workers
.size
).toBe(min
)
103 // We need to clean up the resources after our test
104 await longRunningPool
.destroy()
107 it('Verify scale thread up and down is working when long running task is used:soft', async () => {
108 const longRunningPool
= new DynamicThreadPool(
111 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
113 errorHandler
: e
=> console
.error(e
),
114 onlineHandler
: () => console
.log('long running worker is online'),
115 exitHandler
: () => console
.log('long running worker exited')
118 expect(longRunningPool
.workers
.size
).toBe(min
)
119 for (let i
= 0; i
< max
* 10; i
++) {
120 longRunningPool
.execute()
122 expect(longRunningPool
.workers
.size
).toBe(max
)
123 await TestUtils
.sleep(1500)
124 // Here we expect the workers to be at the max size since that the task is still running
125 expect(longRunningPool
.workers
.size
).toBe(max
)
126 // We need to clean up the resources after our test
127 await longRunningPool
.destroy()
130 it('Verify that a pool with zero worker can be instantiated', async () => {
131 const pool
= new DynamicThreadPool(
134 './tests/worker-files/thread/testWorker.js'
136 expect(pool
).toBeInstanceOf(DynamicThreadPool
)
137 // We need to clean up the resources after our test