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