| 1 | import { expect } from 'expect' |
| 2 | import { DynamicClusterPool, PoolEvents } from '../../../lib/index.js' |
| 3 | import { TaskFunctions } from '../../test-types.js' |
| 4 | import { sleep, waitWorkerEvents } from '../../test-utils.js' |
| 5 | |
| 6 | describe('Dynamic cluster pool test suite', () => { |
| 7 | const min = 1 |
| 8 | const max = 3 |
| 9 | const pool = new DynamicClusterPool( |
| 10 | min, |
| 11 | max, |
| 12 | './tests/worker-files/cluster/testWorker.js', |
| 13 | { |
| 14 | errorHandler: e => console.error(e) |
| 15 | } |
| 16 | ) |
| 17 | |
| 18 | it('Verify that the function is executed in a worker cluster', async () => { |
| 19 | let result = await pool.execute({ |
| 20 | function: TaskFunctions.fibonacci |
| 21 | }) |
| 22 | expect(result).toBe(75025) |
| 23 | result = await pool.execute({ |
| 24 | function: TaskFunctions.factorial |
| 25 | }) |
| 26 | expect(result).toBe(9.33262154439441e157) |
| 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 | let poolBusy = 0 |
| 31 | pool.emitter.on(PoolEvents.busy, () => ++poolBusy) |
| 32 | for (let i = 0; i < max * 2; i++) { |
| 33 | pool.execute() |
| 34 | } |
| 35 | expect(pool.workerNodes.length).toBeLessThanOrEqual(max) |
| 36 | expect(pool.workerNodes.length).toBeGreaterThan(min) |
| 37 | expect(poolBusy).toBe(1) |
| 38 | const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min) |
| 39 | expect(numberOfExitEvents).toBe(max - min) |
| 40 | }) |
| 41 | |
| 42 | it('Verify scale worker up and down is working', async () => { |
| 43 | expect(pool.workerNodes.length).toBe(min) |
| 44 | for (let i = 0; i < max * 2; i++) { |
| 45 | pool.execute() |
| 46 | } |
| 47 | expect(pool.workerNodes.length).toBeGreaterThan(min) |
| 48 | await waitWorkerEvents(pool, 'exit', max - min) |
| 49 | expect(pool.workerNodes.length).toBe(min) |
| 50 | for (let i = 0; i < max * 2; i++) { |
| 51 | pool.execute() |
| 52 | } |
| 53 | expect(pool.workerNodes.length).toBeGreaterThan(min) |
| 54 | await waitWorkerEvents(pool, 'exit', max - min) |
| 55 | expect(pool.workerNodes.length).toBe(min) |
| 56 | }) |
| 57 | |
| 58 | it('Shutdown test', async () => { |
| 59 | const exitPromise = waitWorkerEvents(pool, 'exit', min) |
| 60 | expect(pool.emitter.eventNames()).toStrictEqual([PoolEvents.busy]) |
| 61 | let poolDestroy = 0 |
| 62 | pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy) |
| 63 | expect(pool.emitter.eventNames()).toStrictEqual([ |
| 64 | PoolEvents.busy, |
| 65 | PoolEvents.destroy |
| 66 | ]) |
| 67 | await pool.destroy() |
| 68 | const numberOfExitEvents = await exitPromise |
| 69 | expect(pool.started).toBe(false) |
| 70 | expect(pool.workerNodes.length).toBe(0) |
| 71 | expect(numberOfExitEvents).toBe(min) |
| 72 | expect(poolDestroy).toBe(1) |
| 73 | }) |
| 74 | |
| 75 | it('Validation of inputs test', () => { |
| 76 | expect(() => new DynamicClusterPool(min)).toThrow( |
| 77 | "Cannot find the worker file 'undefined'" |
| 78 | ) |
| 79 | }) |
| 80 | |
| 81 | it('Should work even without opts in input', async () => { |
| 82 | const pool = new DynamicClusterPool( |
| 83 | min, |
| 84 | max, |
| 85 | './tests/worker-files/cluster/testWorker.js' |
| 86 | ) |
| 87 | const result = await pool.execute() |
| 88 | expect(result).toStrictEqual({ ok: 1 }) |
| 89 | // We need to clean up the resources after our test |
| 90 | await pool.destroy() |
| 91 | }) |
| 92 | |
| 93 | it('Verify scale processes up and down is working when long executing task is used:hard', async () => { |
| 94 | const longRunningPool = new DynamicClusterPool( |
| 95 | min, |
| 96 | max, |
| 97 | './tests/worker-files/cluster/longRunningWorkerHardBehavior.js', |
| 98 | { |
| 99 | errorHandler: e => console.error(e), |
| 100 | onlineHandler: () => console.info('long executing worker is online'), |
| 101 | exitHandler: () => console.info('long executing worker exited') |
| 102 | } |
| 103 | ) |
| 104 | expect(longRunningPool.workerNodes.length).toBe(min) |
| 105 | for (let i = 0; i < max * 2; i++) { |
| 106 | longRunningPool.execute() |
| 107 | } |
| 108 | expect(longRunningPool.workerNodes.length).toBe(max) |
| 109 | await waitWorkerEvents(longRunningPool, 'exit', max - min) |
| 110 | expect(longRunningPool.workerNodes.length).toBe(min) |
| 111 | expect( |
| 112 | longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 113 | longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy |
| 114 | ).nextWorkerNodeKey |
| 115 | ).toBeLessThan(longRunningPool.workerNodes.length) |
| 116 | // We need to clean up the resources after our test |
| 117 | await longRunningPool.destroy() |
| 118 | }) |
| 119 | |
| 120 | it('Verify scale processes up and down is working when long executing task is used:soft', async () => { |
| 121 | const longRunningPool = new DynamicClusterPool( |
| 122 | min, |
| 123 | max, |
| 124 | './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js', |
| 125 | { |
| 126 | errorHandler: e => console.error(e), |
| 127 | onlineHandler: () => console.info('long executing worker is online'), |
| 128 | exitHandler: () => console.info('long executing worker exited') |
| 129 | } |
| 130 | ) |
| 131 | expect(longRunningPool.workerNodes.length).toBe(min) |
| 132 | for (let i = 0; i < max * 2; i++) { |
| 133 | longRunningPool.execute() |
| 134 | } |
| 135 | expect(longRunningPool.workerNodes.length).toBe(max) |
| 136 | await sleep(1000) |
| 137 | // Here we expect the workerNodes to be at the max size since the task is still executing |
| 138 | expect(longRunningPool.workerNodes.length).toBe(max) |
| 139 | // We need to clean up the resources after our test |
| 140 | await longRunningPool.destroy() |
| 141 | }) |
| 142 | |
| 143 | it('Verify that a pool with zero worker can be instantiated', async () => { |
| 144 | const pool = new DynamicClusterPool( |
| 145 | 0, |
| 146 | max, |
| 147 | './tests/worker-files/cluster/testWorker.js' |
| 148 | ) |
| 149 | expect(pool).toBeInstanceOf(DynamicClusterPool) |
| 150 | // We need to clean up the resources after our test |
| 151 | await pool.destroy() |
| 152 | }) |
| 153 | }) |