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