Enchance benchmarking code
[poolifier.git] / tests / pools / thread / dynamic.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
325f50bc 2const { DynamicThreadPool } = require('../../../lib/index')
2d2e32c2 3const { WorkerFunctions } = require('../../test-types')
85a3f8a7 4const TestUtils = require('../../test-utils')
506c2a14 5
a35560ba 6describe('Dynamic thread pool test suite', () => {
e1ffb94f
JB
7 const min = 1
8 const max = 3
9 const pool = new DynamicThreadPool(
10 min,
11 max,
12 './tests/worker-files/thread/testWorker.js',
13 {
14 errorHandler: e => console.error(e)
15 }
16 )
17
506c2a14 18 it('Verify that the function is executed in a worker thread', async () => {
6db75ad9
JB
19 let result = await pool.execute({
20 function: WorkerFunctions.fibonacci
21 })
22 expect(result).toBe(false)
23 result = await pool.execute({
24 function: WorkerFunctions.factorial
25 })
26 expect(result).toBe(false)
506c2a14 27 })
28
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 () => {
30 const promises = []
7c0ba920
JB
31 let poolBusy = 0
32 pool.emitter.on('busy', () => poolBusy++)
cf9aa6c3 33 for (let i = 0; i < max * 2; i++) {
6db75ad9 34 promises.push(pool.execute())
506c2a14 35 }
bdaf31cd
JB
36 expect(pool.workers.length).toBeLessThanOrEqual(max)
37 expect(pool.workers.length).toBeGreaterThan(min)
14916bf9
JB
38 // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
39 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
8620fb25 40 expect(poolBusy).toBe(max + 1)
bdacc2d2
JB
41 const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
42 expect(numberOfExitEvents).toBe(max - min)
506c2a14 43 })
44
bcf04003 45 it('Verify scale thread up and down is working', async () => {
46 expect(pool.workers.length).toBe(min)
47 for (let i = 0; i < max * 10; i++) {
6db75ad9 48 pool.execute()
bcf04003 49 }
50 expect(pool.workers.length).toBe(max)
85a3f8a7 51 await TestUtils.waitExits(pool, max - min)
bcf04003 52 expect(pool.workers.length).toBe(min)
53 for (let i = 0; i < max * 10; i++) {
6db75ad9 54 pool.execute()
bcf04003 55 }
56 expect(pool.workers.length).toBe(max)
85a3f8a7 57 await TestUtils.waitExits(pool, max - min)
bcf04003 58 expect(pool.workers.length).toBe(min)
59 })
c01733f1 60
506c2a14 61 it('Shutdown test', async () => {
cf597bc5 62 const exitPromise = TestUtils.waitExits(pool, min)
1f9a5a44 63 await pool.destroy()
cf597bc5
JB
64 const numberOfExitEvents = await exitPromise
65 expect(numberOfExitEvents).toBe(min)
506c2a14 66 })
67
8d3782fa
JB
68 it('Validation of inputs test', () => {
69 expect(() => new DynamicThreadPool(min)).toThrowError(
85a3f8a7
APA
70 new Error('Please specify a file with a worker implementation')
71 )
506c2a14 72 })
73
74 it('Should work even without opts in input', async () => {
325f50bc 75 const pool1 = new DynamicThreadPool(
e1ffb94f
JB
76 min,
77 max,
76b1e974 78 './tests/worker-files/thread/testWorker.js'
325f50bc 79 )
6db75ad9
JB
80 const res = await pool1.execute()
81 expect(res).toBe(false)
0e2503fc
JB
82 // We need to clean up the resources after our test
83 await pool1.destroy()
506c2a14 84 })
c01733f1 85
4c35177b 86 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
c01733f1 87 const longRunningPool = new DynamicThreadPool(
88 min,
89 max,
85a3f8a7 90 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
4c35177b 91 {
92 errorHandler: e => console.error(e),
292ad316
JB
93 onlineHandler: () => console.log('long running worker is online'),
94 exitHandler: () => console.log('long running worker exited')
4c35177b 95 }
96 )
97 expect(longRunningPool.workers.length).toBe(min)
98 for (let i = 0; i < max * 10; i++) {
6db75ad9 99 longRunningPool.execute()
4c35177b 100 }
101 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 102 await TestUtils.waitExits(longRunningPool, max - min)
4c35177b 103 expect(longRunningPool.workers.length).toBe(min)
0e2503fc
JB
104 // We need to clean up the resources after our test
105 await longRunningPool.destroy()
4c35177b 106 })
107
108 it('Verify scale thread up and down is working when long running task is used:soft', async () => {
109 const longRunningPool = new DynamicThreadPool(
110 min,
111 max,
85a3f8a7 112 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
c01733f1 113 {
114 errorHandler: e => console.error(e),
292ad316
JB
115 onlineHandler: () => console.log('long running worker is online'),
116 exitHandler: () => console.log('long running worker exited')
c01733f1 117 }
118 )
119 expect(longRunningPool.workers.length).toBe(min)
120 for (let i = 0; i < max * 10; i++) {
6db75ad9 121 longRunningPool.execute()
c01733f1 122 }
123 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 124 await TestUtils.sleep(1500)
c01733f1 125 // Here we expect the workers to be at the max size since that the task is still running
126 expect(longRunningPool.workers.length).toBe(max)
0e2503fc
JB
127 // We need to clean up the resources after our test
128 await longRunningPool.destroy()
c01733f1 129 })
8d3782fa
JB
130
131 it('Verify that a pool with zero worker can be instantiated', async () => {
132 const pool = new DynamicThreadPool(
133 0,
134 max,
135 './tests/worker-files/thread/testWorker.js'
136 )
137 expect(pool).toBeInstanceOf(DynamicThreadPool)
138 // We need to clean up the resources after our test
139 await pool.destroy()
140 })
506c2a14 141})