New bench execution and sleep between a test and another
[poolifier.git] / tests / pools / cluster / dynamic.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { DynamicClusterPool } = require('../../../lib/index')
85a3f8a7 3const TestUtils = require('../../test-utils')
506c2a14 4const min = 1
c719859c 5const max = 3
325f50bc
S
6const pool = new DynamicClusterPool(
7 min,
8 max,
76b1e974 9 './tests/worker-files/cluster/testWorker.js',
325f50bc 10 {
e5177d86 11 errorHandler: e => console.error(e)
325f50bc
S
12 }
13)
506c2a14 14
a35560ba 15describe('Dynamic cluster pool test suite', () => {
325f50bc 16 it('Verify that the function is executed in a worker cluster', async () => {
506c2a14 17 const result = await pool.execute({ test: 'test' })
18 expect(result).toBeDefined()
19 expect(result).toBeFalsy()
20 })
21
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 () => {
23 const promises = []
b755d5be 24 let fullPool = 0
25 pool.emitter.on('FullPool', () => fullPool++)
cf9aa6c3 26 for (let i = 0; i < max * 2; i++) {
506c2a14 27 promises.push(pool.execute({ test: 'test' }))
28 }
325f50bc
S
29 expect(pool.workers.length).toBeLessThanOrEqual(max)
30 expect(pool.workers.length).toBeGreaterThan(min)
b755d5be 31 expect(fullPool > 1).toBeTruthy()
85a3f8a7
APA
32 const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
33 expect(numberOfExitEvents).toBe(max - min)
506c2a14 34 })
35
325f50bc 36 it('Verify scale worker up and down is working', async () => {
bcf04003 37 expect(pool.workers.length).toBe(min)
38 for (let i = 0; i < max * 10; i++) {
39 pool.execute({ test: 'test' })
40 }
325f50bc 41 expect(pool.workers.length).toBeGreaterThan(min)
85a3f8a7 42 await TestUtils.waitExits(pool, max - min)
bcf04003 43 expect(pool.workers.length).toBe(min)
44 for (let i = 0; i < max * 10; i++) {
45 pool.execute({ test: 'test' })
46 }
325f50bc 47 expect(pool.workers.length).toBeGreaterThan(min)
85a3f8a7 48 await TestUtils.waitExits(pool, max - min)
bcf04003 49 expect(pool.workers.length).toBe(min)
50 })
506c2a14 51
85a3f8a7
APA
52 it('Shutdown test', async () => {
53 const exitPromise = TestUtils.waitExits(pool, min)
54 await pool.destroy()
55 const res = await exitPromise
56 expect(res).toBe(min)
506c2a14 57 })
58
59 it('Should work even without opts in input', async () => {
325f50bc
S
60 const pool1 = new DynamicClusterPool(
61 1,
62 1,
76b1e974 63 './tests/worker-files/cluster/testWorker.js'
325f50bc 64 )
8bc77620
APA
65 const result = await pool1.execute({ test: 'test' })
66 expect(result).toBeFalsy()
67 // We need to clean up the resources after our test
68 await pool1.destroy()
506c2a14 69 })
e826bd34 70
4c35177b 71 it('Verify scale processes up and down is working when long running task is used:hard', async () => {
c01733f1 72 const longRunningPool = new DynamicClusterPool(
73 min,
74 max,
85a3f8a7 75 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js'
4c35177b 76 )
77 expect(longRunningPool.workers.length).toBe(min)
78 for (let i = 0; i < max * 10; i++) {
79 longRunningPool.execute({ test: 'test' })
80 }
81 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 82 await TestUtils.waitExits(longRunningPool, max - min)
4c35177b 83 // Here we expect the workers to be at the max size since that the task is still running
84 expect(longRunningPool.workers.length).toBe(min)
8bc77620
APA
85 // We need to clean up the resources after our test
86 await longRunningPool.destroy()
4c35177b 87 })
88
89 it('Verify scale processes up and down is working when long running task is used:soft', async () => {
90 const longRunningPool = new DynamicClusterPool(
91 min,
92 max,
85a3f8a7 93 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js'
c01733f1 94 )
95 expect(longRunningPool.workers.length).toBe(min)
96 for (let i = 0; i < max * 10; i++) {
97 longRunningPool.execute({ test: 'test' })
98 }
99 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 100 await TestUtils.sleep(1500)
c01733f1 101 // Here we expect the workers to be at the max size since that the task is still running
102 expect(longRunningPool.workers.length).toBe(max)
8bc77620
APA
103 // We need to clean up the resources after our test
104 await longRunningPool.destroy()
c01733f1 105 })
506c2a14 106})