| 1 | const { expect } = require('expect') |
| 2 | const { DynamicThreadPool, PoolEvents } = require('../../../lib') |
| 3 | const { WorkerFunctions } = require('../../test-types') |
| 4 | const TestUtils = require('../../test-utils') |
| 5 | |
| 6 | describe('Dynamic thread pool test suite', () => { |
| 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 | |
| 18 | it('Verify that the function is executed in a worker thread', async () => { |
| 19 | let result = await pool.execute({ |
| 20 | function: WorkerFunctions.fibonacci |
| 21 | }) |
| 22 | expect(result).toBe(121393) |
| 23 | result = await pool.execute({ |
| 24 | function: WorkerFunctions.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 | // 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. |
| 39 | expect(poolBusy).toBe(max + 1) |
| 40 | const numberOfExitEvents = await TestUtils.waitExits(pool, max - min) |
| 41 | expect(numberOfExitEvents).toBe(max - min) |
| 42 | }) |
| 43 | |
| 44 | it('Verify scale thread up and down is working', async () => { |
| 45 | expect(pool.workerNodes.length).toBe(min) |
| 46 | for (let i = 0; i < max * 2; i++) { |
| 47 | pool.execute() |
| 48 | } |
| 49 | expect(pool.workerNodes.length).toBe(max) |
| 50 | await TestUtils.waitExits(pool, max - min) |
| 51 | expect(pool.workerNodes.length).toBe(min) |
| 52 | for (let i = 0; i < max * 2; i++) { |
| 53 | pool.execute() |
| 54 | } |
| 55 | expect(pool.workerNodes.length).toBe(max) |
| 56 | await TestUtils.waitExits(pool, max - min) |
| 57 | expect(pool.workerNodes.length).toBe(min) |
| 58 | }) |
| 59 | |
| 60 | it('Shutdown test', async () => { |
| 61 | const exitPromise = TestUtils.waitExits(pool, min) |
| 62 | await pool.destroy() |
| 63 | const numberOfExitEvents = await exitPromise |
| 64 | expect(numberOfExitEvents).toBe(min) |
| 65 | }) |
| 66 | |
| 67 | it('Validation of inputs test', () => { |
| 68 | expect(() => new DynamicThreadPool(min)).toThrowError( |
| 69 | 'Please specify a file with a worker implementation' |
| 70 | ) |
| 71 | }) |
| 72 | |
| 73 | it('Should work even without opts in input', async () => { |
| 74 | const pool1 = new DynamicThreadPool( |
| 75 | min, |
| 76 | max, |
| 77 | './tests/worker-files/thread/testWorker.js' |
| 78 | ) |
| 79 | const res = await pool1.execute() |
| 80 | expect(res).toBe(false) |
| 81 | // We need to clean up the resources after our test |
| 82 | await pool1.destroy() |
| 83 | }) |
| 84 | |
| 85 | it('Verify scale thread up and down is working when long running task is used:hard', async () => { |
| 86 | const longRunningPool = new DynamicThreadPool( |
| 87 | min, |
| 88 | max, |
| 89 | './tests/worker-files/thread/longRunningWorkerHardBehavior.js', |
| 90 | { |
| 91 | errorHandler: e => console.error(e), |
| 92 | onlineHandler: () => console.log('long running worker is online'), |
| 93 | exitHandler: () => console.log('long running worker exited') |
| 94 | } |
| 95 | ) |
| 96 | expect(longRunningPool.workerNodes.length).toBe(min) |
| 97 | for (let i = 0; i < max * 2; i++) { |
| 98 | longRunningPool.execute() |
| 99 | } |
| 100 | expect(longRunningPool.workerNodes.length).toBe(max) |
| 101 | await TestUtils.waitExits(longRunningPool, max - min) |
| 102 | expect(longRunningPool.workerNodes.length).toBe(min) |
| 103 | expect( |
| 104 | longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 105 | longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy |
| 106 | ).nextWorkerNodeId |
| 107 | ).toBeLessThan(longRunningPool.workerNodes.length) |
| 108 | // We need to clean up the resources after our test |
| 109 | await longRunningPool.destroy() |
| 110 | }) |
| 111 | |
| 112 | it('Verify scale thread up and down is working when long running task is used:soft', async () => { |
| 113 | const longRunningPool = new DynamicThreadPool( |
| 114 | min, |
| 115 | max, |
| 116 | './tests/worker-files/thread/longRunningWorkerSoftBehavior.js', |
| 117 | { |
| 118 | errorHandler: e => console.error(e), |
| 119 | onlineHandler: () => console.log('long running worker is online'), |
| 120 | exitHandler: () => console.log('long running worker exited') |
| 121 | } |
| 122 | ) |
| 123 | expect(longRunningPool.workerNodes.length).toBe(min) |
| 124 | for (let i = 0; i < max * 2; i++) { |
| 125 | longRunningPool.execute() |
| 126 | } |
| 127 | expect(longRunningPool.workerNodes.length).toBe(max) |
| 128 | await TestUtils.sleep(1500) |
| 129 | // Here we expect the workerNodes to be at the max size since the task is still running |
| 130 | expect(longRunningPool.workerNodes.length).toBe(max) |
| 131 | // We need to clean up the resources after our test |
| 132 | await longRunningPool.destroy() |
| 133 | }) |
| 134 | |
| 135 | it('Verify that a pool with zero worker can be instantiated', async () => { |
| 136 | const pool = new DynamicThreadPool( |
| 137 | 0, |
| 138 | max, |
| 139 | './tests/worker-files/thread/testWorker.js' |
| 140 | ) |
| 141 | expect(pool).toBeInstanceOf(DynamicThreadPool) |
| 142 | // We need to clean up the resources after our test |
| 143 | await pool.destroy() |
| 144 | }) |
| 145 | }) |