| 1 | import { expect } from 'expect' |
| 2 | |
| 3 | import { FixedClusterPool, FixedThreadPool } from '../../../lib/index.cjs' |
| 4 | import { |
| 5 | buildWorkerChoiceStrategyOptions, |
| 6 | getWorkerChoiceStrategiesRetries |
| 7 | } from '../../../lib/pools/selection-strategies/selection-strategies-utils.cjs' |
| 8 | |
| 9 | describe('Selection strategies utils test suite', () => { |
| 10 | it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => { |
| 11 | const numberOfWorkers = 4 |
| 12 | const pool = new FixedClusterPool( |
| 13 | numberOfWorkers, |
| 14 | './tests/worker-files/cluster/testWorker.cjs' |
| 15 | ) |
| 16 | expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({ |
| 17 | runTime: { median: false }, |
| 18 | waitTime: { median: false }, |
| 19 | elu: { median: false }, |
| 20 | weights: expect.objectContaining({ |
| 21 | 0: expect.any(Number), |
| 22 | [pool.info.maxSize - 1]: expect.any(Number) |
| 23 | }) |
| 24 | }) |
| 25 | const workerChoiceStrategyOptions = { |
| 26 | runTime: { median: true }, |
| 27 | waitTime: { median: true }, |
| 28 | elu: { median: true }, |
| 29 | weights: { |
| 30 | 0: 100, |
| 31 | 1: 100 |
| 32 | } |
| 33 | } |
| 34 | expect( |
| 35 | buildWorkerChoiceStrategyOptions(pool, workerChoiceStrategyOptions) |
| 36 | ).toStrictEqual(workerChoiceStrategyOptions) |
| 37 | await pool.destroy() |
| 38 | }) |
| 39 | |
| 40 | it('Verify getWorkerChoiceStrategyRetries() behavior', async () => { |
| 41 | const numberOfThreads = 4 |
| 42 | const pool = new FixedThreadPool( |
| 43 | numberOfThreads, |
| 44 | './tests/worker-files/thread/testWorker.mjs' |
| 45 | ) |
| 46 | expect(getWorkerChoiceStrategiesRetries(pool)).toBe(pool.info.maxSize * 2) |
| 47 | const workerChoiceStrategyOptions = { |
| 48 | runTime: { median: true }, |
| 49 | waitTime: { median: true }, |
| 50 | elu: { median: true }, |
| 51 | weights: { |
| 52 | 0: 100, |
| 53 | 1: 100 |
| 54 | } |
| 55 | } |
| 56 | expect( |
| 57 | getWorkerChoiceStrategiesRetries(pool, workerChoiceStrategyOptions) |
| 58 | ).toBe( |
| 59 | pool.info.maxSize + |
| 60 | Object.keys(workerChoiceStrategyOptions.weights).length |
| 61 | ) |
| 62 | await pool.destroy() |
| 63 | }) |
| 64 | }) |