| 1 | import { randomInt } from 'node:crypto' |
| 2 | import { expect } from 'expect' |
| 3 | import { FixedThreadPool } from '../../../lib/index.cjs' |
| 4 | import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.cjs' |
| 5 | import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.cjs' |
| 6 | |
| 7 | describe('Weighted round robin strategy worker choice strategy test suite', () => { |
| 8 | // const min = 1 |
| 9 | const max = 3 |
| 10 | let pool |
| 11 | |
| 12 | before(() => { |
| 13 | pool = new FixedThreadPool( |
| 14 | max, |
| 15 | './tests/worker-files/thread/testWorker.mjs' |
| 16 | ) |
| 17 | }) |
| 18 | |
| 19 | after(async () => { |
| 20 | await pool.destroy() |
| 21 | }) |
| 22 | |
| 23 | it('Verify that WRR reset() resets internals', () => { |
| 24 | const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool) |
| 25 | strategy.nextWorkerNodeKey = randomInt(1, 281474976710656) |
| 26 | strategy.previousWorkerNodeKey = randomInt(1, 281474976710656) |
| 27 | strategy.workerNodeVirtualTaskRunTime = randomInt(1, 281474976710656) |
| 28 | expect(strategy.nextWorkerNodeKey).toBeGreaterThan(0) |
| 29 | expect(strategy.previousWorkerNodeKey).toBeGreaterThan(0) |
| 30 | expect(strategy.workerNodeVirtualTaskRunTime).toBeGreaterThan(0) |
| 31 | expect(strategy.reset()).toBe(true) |
| 32 | expect(strategy.nextWorkerNodeKey).toBe(0) |
| 33 | expect(strategy.previousWorkerNodeKey).toBe(0) |
| 34 | expect(strategy.workerNodeVirtualTaskRunTime).toBe(0) |
| 35 | }) |
| 36 | |
| 37 | it('Verify that IWRR reset() resets internals', () => { |
| 38 | const strategy = new InterleavedWeightedRoundRobinWorkerChoiceStrategy(pool) |
| 39 | strategy.nextWorkerNodeKey = randomInt(1, 281474976710656) |
| 40 | strategy.previousWorkerNodeKey = randomInt(1, 281474976710656) |
| 41 | strategy.roundId = randomInt(1, 281474976710656) |
| 42 | strategy.workerNodeId = randomInt(1, 281474976710656) |
| 43 | strategy.workerNodeVirtualTaskRunTime = randomInt(1, 281474976710656) |
| 44 | expect(strategy.nextWorkerNodeKey).toBeGreaterThan(0) |
| 45 | expect(strategy.previousWorkerNodeKey).toBeGreaterThan(0) |
| 46 | expect(strategy.roundId).toBeGreaterThan(0) |
| 47 | expect(strategy.workerNodeId).toBeGreaterThan(0) |
| 48 | expect(strategy.workerNodeVirtualTaskRunTime).toBeGreaterThan(0) |
| 49 | expect(strategy.reset()).toBe(true) |
| 50 | expect(strategy.nextWorkerNodeKey).toBe(0) |
| 51 | expect(strategy.previousWorkerNodeKey).toBe(0) |
| 52 | expect(strategy.roundId).toBe(0) |
| 53 | expect(strategy.workerNodeId).toBe(0) |
| 54 | expect(strategy.workerNodeVirtualTaskRunTime).toBe(0) |
| 55 | }) |
| 56 | }) |