Add tests for WRR worker choice strategy
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.js
1 const { expect } = require('expect')
2 const sinon = require('sinon')
3 const {
4 FixedThreadPool,
5 DynamicThreadPool,
6 WorkerChoiceStrategies
7 } = require('../../../lib/index')
8 const {
9 WorkerChoiceStrategyContext
10 } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
11 const {
12 RoundRobinWorkerChoiceStrategy
13 } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
14 const {
15 LessRecentlyUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy')
17 const {
18 FairShareWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
20 const {
21 WeightedRoundRobinWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
23 const {
24 DynamicPoolWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
26
27 describe('Worker choice strategy context test suite', () => {
28 const min = 1
29 const max = 3
30 let fixedPool, dynamicPool
31
32 before(() => {
33 fixedPool = new FixedThreadPool(
34 max,
35 './tests/worker-files/thread/testWorker.js'
36 )
37 dynamicPool = new DynamicThreadPool(
38 min,
39 max,
40 './tests/worker-files/thread/testWorker.js'
41 )
42 })
43
44 afterEach(() => {
45 sinon.restore()
46 })
47
48 after(async () => {
49 await fixedPool.destroy()
50 await dynamicPool.destroy()
51 })
52
53 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
54 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
55 fixedPool
56 )
57 const WorkerChoiceStrategyStub = sinon.createStubInstance(
58 RoundRobinWorkerChoiceStrategy,
59 {
60 choose: sinon.stub().returns('worker')
61 }
62 )
63 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
64 const chosenWorker = workerChoiceStrategyContext.execute()
65 expect(
66 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
67 ).toBe(true)
68 expect(chosenWorker).toBe('worker')
69 })
70
71 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
72 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
73 dynamicPool
74 )
75 const WorkerChoiceStrategyStub = sinon.createStubInstance(
76 RoundRobinWorkerChoiceStrategy,
77 {
78 choose: sinon.stub().returns('worker')
79 }
80 )
81 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
82 const chosenWorker = workerChoiceStrategyContext.execute()
83 expect(
84 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
85 ).toBe(true)
86 expect(chosenWorker).toBe('worker')
87 })
88
89 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
90 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
91 fixedPool
92 )
93 workerChoiceStrategyContext.setWorkerChoiceStrategy(
94 WorkerChoiceStrategies.ROUND_ROBIN
95 )
96 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
97 RoundRobinWorkerChoiceStrategy
98 )
99 })
100
101 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
102 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
103 dynamicPool
104 )
105 workerChoiceStrategyContext.setWorkerChoiceStrategy(
106 WorkerChoiceStrategies.ROUND_ROBIN
107 )
108 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
109 DynamicPoolWorkerChoiceStrategy
110 )
111 expect(
112 workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
113 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
114 })
115
116 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
117 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
118 fixedPool
119 )
120 workerChoiceStrategyContext.setWorkerChoiceStrategy(
121 WorkerChoiceStrategies.LESS_RECENTLY_USED
122 )
123 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
124 LessRecentlyUsedWorkerChoiceStrategy
125 )
126 })
127
128 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
129 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
130 dynamicPool
131 )
132 workerChoiceStrategyContext.setWorkerChoiceStrategy(
133 WorkerChoiceStrategies.LESS_RECENTLY_USED
134 )
135 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
136 DynamicPoolWorkerChoiceStrategy
137 )
138 expect(
139 workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
140 ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy)
141 })
142
143 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
144 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
145 fixedPool
146 )
147 workerChoiceStrategyContext.setWorkerChoiceStrategy(
148 WorkerChoiceStrategies.FAIR_SHARE
149 )
150 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
151 FairShareWorkerChoiceStrategy
152 )
153 })
154
155 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
156 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
157 dynamicPool
158 )
159 workerChoiceStrategyContext.setWorkerChoiceStrategy(
160 WorkerChoiceStrategies.FAIR_SHARE
161 )
162 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
163 DynamicPoolWorkerChoiceStrategy
164 )
165 expect(
166 workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
167 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
168 })
169
170 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
171 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
172 fixedPool
173 )
174 workerChoiceStrategyContext.setWorkerChoiceStrategy(
175 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
176 )
177 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
178 WeightedRoundRobinWorkerChoiceStrategy
179 )
180 })
181
182 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
183 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
184 dynamicPool
185 )
186 workerChoiceStrategyContext.setWorkerChoiceStrategy(
187 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
188 )
189 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
190 DynamicPoolWorkerChoiceStrategy
191 )
192 expect(
193 workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
194 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
195 })
196 })