Benchmarks and tests code cleanups. (#257)
[poolifier.git] / tests / pools / cluster / dynamic.test.js
1 const expect = require('expect')
2 const { DynamicClusterPool } = require('../../../lib/index')
3 const TestUtils = require('../../test-utils')
4 const min = 1
5 const max = 3
6 const pool = new DynamicClusterPool(
7 min,
8 max,
9 './tests/worker-files/cluster/testWorker.js',
10 {
11 errorHandler: e => console.error(e)
12 }
13 )
14
15 describe('Dynamic cluster pool test suite', () => {
16 it('Verify that the function is executed in a worker cluster', async () => {
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 = []
24 let poolBusy = 0
25 pool.emitter.on('busy', () => poolBusy++)
26 for (let i = 0; i < max * 2; i++) {
27 promises.push(pool.execute({ test: 'test' }))
28 }
29 expect(pool.workers.length).toBeLessThanOrEqual(max)
30 expect(pool.workers.length).toBeGreaterThan(min)
31 expect(poolBusy).toEqual(max + 1)
32 const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
33 expect(numberOfExitEvents).toBe(max - min)
34 })
35
36 it('Verify scale worker up and down is working', async () => {
37 expect(pool.workers.length).toBe(min)
38 for (let i = 0; i < max * 10; i++) {
39 pool.execute({ test: 'test' })
40 }
41 expect(pool.workers.length).toBeGreaterThan(min)
42 await TestUtils.waitExits(pool, max - min)
43 expect(pool.workers.length).toBe(min)
44 for (let i = 0; i < max * 10; i++) {
45 pool.execute({ test: 'test' })
46 }
47 expect(pool.workers.length).toBeGreaterThan(min)
48 await TestUtils.waitExits(pool, max - min)
49 expect(pool.workers.length).toBe(min)
50 })
51
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)
57 })
58
59 it('Validation of inputs test', () => {
60 expect(() => new DynamicClusterPool(min)).toThrowError(
61 new Error('Please specify a file with a worker implementation')
62 )
63 })
64
65 it('Should work even without opts in input', async () => {
66 const pool1 = new DynamicClusterPool(
67 1,
68 1,
69 './tests/worker-files/cluster/testWorker.js'
70 )
71 const result = await pool1.execute({ test: 'test' })
72 expect(result).toBeFalsy()
73 // We need to clean up the resources after our test
74 await pool1.destroy()
75 })
76
77 it('Verify scale processes up and down is working when long running task is used:hard', async () => {
78 const longRunningPool = new DynamicClusterPool(
79 min,
80 max,
81 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js',
82 {
83 errorHandler: e => console.error(e),
84 onlineHandler: () => console.log('long running worker is online'),
85 exitHandler: () => console.log('long running worker exited')
86 }
87 )
88 expect(longRunningPool.workers.length).toBe(min)
89 for (let i = 0; i < max * 10; i++) {
90 longRunningPool.execute({ test: 'test' })
91 }
92 expect(longRunningPool.workers.length).toBe(max)
93 await TestUtils.waitExits(longRunningPool, max - min)
94 // Here we expect the workers to be at the max size since that the task is still running
95 expect(longRunningPool.workers.length).toBe(min)
96 // We need to clean up the resources after our test
97 await longRunningPool.destroy()
98 })
99
100 it('Verify scale processes up and down is working when long running task is used:soft', async () => {
101 const longRunningPool = new DynamicClusterPool(
102 min,
103 max,
104 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js',
105 {
106 errorHandler: e => console.error(e),
107 onlineHandler: () => console.log('long running worker is online'),
108 exitHandler: () => console.log('long running worker exited')
109 }
110 )
111 expect(longRunningPool.workers.length).toBe(min)
112 for (let i = 0; i < max * 10; i++) {
113 longRunningPool.execute({ test: 'test' })
114 }
115 expect(longRunningPool.workers.length).toBe(max)
116 await TestUtils.sleep(1500)
117 // Here we expect the workers to be at the max size since that the task is still running
118 expect(longRunningPool.workers.length).toBe(max)
119 // We need to clean up the resources after our test
120 await longRunningPool.destroy()
121 })
122
123 it('Verify that a pool with zero worker can be instantiated', async () => {
124 const pool = new DynamicClusterPool(
125 0,
126 max,
127 './tests/worker-files/cluster/testWorker.js'
128 )
129 expect(pool).toBeInstanceOf(DynamicClusterPool)
130 // We need to clean up the resources after our test
131 await pool.destroy()
132 })
133 })