| 1 | const { expect } = require('expect') |
| 2 | // const sinon = require('sinon') |
| 3 | const { |
| 4 | getWorkerChoiceStrategy |
| 5 | } = require('../../../lib/pools/selection-strategies/selection-strategies-utils') |
| 6 | const { |
| 7 | FixedThreadPool, |
| 8 | WorkerChoiceStrategies |
| 9 | } = require('../../../lib/index') |
| 10 | const { |
| 11 | RoundRobinWorkerChoiceStrategy |
| 12 | } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') |
| 13 | const { |
| 14 | LessUsedWorkerChoiceStrategy |
| 15 | } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy') |
| 16 | const { |
| 17 | LessBusyWorkerChoiceStrategy |
| 18 | } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy') |
| 19 | const { |
| 20 | FairShareWorkerChoiceStrategy |
| 21 | } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') |
| 22 | const { |
| 23 | WeightedRoundRobinWorkerChoiceStrategy |
| 24 | } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') |
| 25 | |
| 26 | describe('Selection strategies utils test suite', () => { |
| 27 | const max = 3 |
| 28 | let pool |
| 29 | |
| 30 | before(() => { |
| 31 | pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js') |
| 32 | }) |
| 33 | |
| 34 | // afterEach(() => { |
| 35 | // sinon.restore() |
| 36 | // }) |
| 37 | |
| 38 | after(async () => { |
| 39 | await pool.destroy() |
| 40 | }) |
| 41 | |
| 42 | it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => { |
| 43 | const strategy = getWorkerChoiceStrategy(pool) |
| 44 | expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 45 | }) |
| 46 | |
| 47 | it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => { |
| 48 | const strategy = getWorkerChoiceStrategy( |
| 49 | pool, |
| 50 | WorkerChoiceStrategies.ROUND_ROBIN |
| 51 | ) |
| 52 | expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 53 | }) |
| 54 | |
| 55 | it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => { |
| 56 | const strategy = getWorkerChoiceStrategy( |
| 57 | pool, |
| 58 | WorkerChoiceStrategies.LESS_USED |
| 59 | ) |
| 60 | expect(strategy).toBeInstanceOf(LessUsedWorkerChoiceStrategy) |
| 61 | }) |
| 62 | |
| 63 | it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => { |
| 64 | const strategy = getWorkerChoiceStrategy( |
| 65 | pool, |
| 66 | WorkerChoiceStrategies.LESS_BUSY |
| 67 | ) |
| 68 | expect(strategy).toBeInstanceOf(LessBusyWorkerChoiceStrategy) |
| 69 | }) |
| 70 | |
| 71 | it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => { |
| 72 | const strategy = getWorkerChoiceStrategy( |
| 73 | pool, |
| 74 | WorkerChoiceStrategies.FAIR_SHARE |
| 75 | ) |
| 76 | expect(strategy).toBeInstanceOf(FairShareWorkerChoiceStrategy) |
| 77 | }) |
| 78 | |
| 79 | it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => { |
| 80 | const strategy = getWorkerChoiceStrategy( |
| 81 | pool, |
| 82 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 83 | ) |
| 84 | expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) |
| 85 | }) |
| 86 | |
| 87 | it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => { |
| 88 | expect(() => { |
| 89 | getWorkerChoiceStrategy(pool, 'UNKNOWN_STRATEGY') |
| 90 | }).toThrowError( |
| 91 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") |
| 92 | ) |
| 93 | }) |
| 94 | }) |