refactor: apply stricter strategy design pattern requirements on worker
[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 LessUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
17 const {
18 LessBusyWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
20 const {
21 FairShareWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
23 const {
24 WeightedRoundRobinWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-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(0)
61 }
62 )
63 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
64 const chosenWorkerKey = workerChoiceStrategyContext.execute()
65 expect(
66 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
67 ).toBe(true)
68 expect(chosenWorkerKey).toBe(0)
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(0)
79 }
80 )
81 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
82 const chosenWorkerKey = workerChoiceStrategyContext.execute()
83 expect(
84 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
85 ).toBe(true)
86 expect(chosenWorkerKey).toBe(0)
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 fixedPool,
95 WorkerChoiceStrategies.ROUND_ROBIN
96 )
97 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
98 RoundRobinWorkerChoiceStrategy
99 )
100 })
101
102 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
103 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
104 dynamicPool
105 )
106 workerChoiceStrategyContext.setWorkerChoiceStrategy(
107 dynamicPool,
108 WorkerChoiceStrategies.ROUND_ROBIN
109 )
110 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
111 RoundRobinWorkerChoiceStrategy
112 )
113 })
114
115 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
116 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
117 fixedPool
118 )
119 workerChoiceStrategyContext.setWorkerChoiceStrategy(
120 fixedPool,
121 WorkerChoiceStrategies.LESS_USED
122 )
123 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
124 LessUsedWorkerChoiceStrategy
125 )
126 })
127
128 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
129 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
130 dynamicPool
131 )
132 workerChoiceStrategyContext.setWorkerChoiceStrategy(
133 dynamicPool,
134 WorkerChoiceStrategies.LESS_USED
135 )
136 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
137 LessUsedWorkerChoiceStrategy
138 )
139 })
140
141 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
142 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
143 fixedPool
144 )
145 workerChoiceStrategyContext.setWorkerChoiceStrategy(
146 fixedPool,
147 WorkerChoiceStrategies.LESS_BUSY
148 )
149 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
150 LessBusyWorkerChoiceStrategy
151 )
152 })
153
154 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
155 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
156 dynamicPool
157 )
158 workerChoiceStrategyContext.setWorkerChoiceStrategy(
159 dynamicPool,
160 WorkerChoiceStrategies.LESS_BUSY
161 )
162 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
163 LessBusyWorkerChoiceStrategy
164 )
165 })
166
167 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
168 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
169 fixedPool
170 )
171 workerChoiceStrategyContext.setWorkerChoiceStrategy(
172 fixedPool,
173 WorkerChoiceStrategies.FAIR_SHARE
174 )
175 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
176 FairShareWorkerChoiceStrategy
177 )
178 })
179
180 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
181 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
182 dynamicPool
183 )
184 workerChoiceStrategyContext.setWorkerChoiceStrategy(
185 dynamicPool,
186 WorkerChoiceStrategies.FAIR_SHARE
187 )
188 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
189 FairShareWorkerChoiceStrategy
190 )
191 })
192
193 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
194 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
195 fixedPool
196 )
197 workerChoiceStrategyContext.setWorkerChoiceStrategy(
198 fixedPool,
199 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
200 )
201 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
202 WeightedRoundRobinWorkerChoiceStrategy
203 )
204 })
205
206 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
207 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
208 dynamicPool
209 )
210 workerChoiceStrategyContext.setWorkerChoiceStrategy(
211 dynamicPool,
212 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
213 )
214 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
215 WeightedRoundRobinWorkerChoiceStrategy
216 )
217 })
218 })