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