feat: add less busy 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 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 const {
27 DynamicPoolWorkerChoiceStrategy
28 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
29
30 describe('Worker choice strategy context test suite', () => {
31 const min = 1
32 const max = 3
33 let fixedPool, dynamicPool
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 )
45 })
46
47 afterEach(() => {
48 sinon.restore()
49 })
50
51 after(async () => {
52 await fixedPool.destroy()
53 await dynamicPool.destroy()
54 })
55
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 {
63 choose: sinon.stub().returns('worker')
64 }
65 )
66 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
67 const chosenWorker = workerChoiceStrategyContext.execute()
68 expect(
69 workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce
70 ).toBe(true)
71 expect(chosenWorker).toBe('worker')
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 {
81 choose: sinon.stub().returns('worker')
82 }
83 )
84 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
85 const chosenWorker = workerChoiceStrategyContext.execute()
86 expect(
87 workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce
88 ).toBe(true)
89 expect(chosenWorker).toBe('worker')
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 )
99 expect(
100 workerChoiceStrategyContext.getWorkerChoiceStrategy()
101 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
102 })
103
104 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
105 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
106 dynamicPool
107 )
108 workerChoiceStrategyContext.setWorkerChoiceStrategy(
109 WorkerChoiceStrategies.ROUND_ROBIN
110 )
111 expect(
112 workerChoiceStrategyContext.getWorkerChoiceStrategy()
113 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
114 expect(
115 workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
116 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
117 })
118
119 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
120 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
121 fixedPool
122 )
123 workerChoiceStrategyContext.setWorkerChoiceStrategy(
124 WorkerChoiceStrategies.LESS_USED
125 )
126 expect(
127 workerChoiceStrategyContext.getWorkerChoiceStrategy()
128 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
129 })
130
131 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
132 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
133 dynamicPool
134 )
135 workerChoiceStrategyContext.setWorkerChoiceStrategy(
136 WorkerChoiceStrategies.LESS_USED
137 )
138 expect(
139 workerChoiceStrategyContext.getWorkerChoiceStrategy()
140 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
141 expect(
142 workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
143 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
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 )
153 expect(
154 workerChoiceStrategyContext.getWorkerChoiceStrategy()
155 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
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 )
165 expect(
166 workerChoiceStrategyContext.getWorkerChoiceStrategy()
167 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
168 expect(
169 workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
170 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
171 })
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 )
180 expect(
181 workerChoiceStrategyContext.getWorkerChoiceStrategy()
182 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
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 )
192 expect(
193 workerChoiceStrategyContext.getWorkerChoiceStrategy()
194 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
195 expect(
196 workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
197 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
198 })
199
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 )
207 expect(
208 workerChoiceStrategyContext.getWorkerChoiceStrategy()
209 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
210 })
211
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 )
219 expect(
220 workerChoiceStrategyContext.getWorkerChoiceStrategy()
221 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
222 expect(
223 workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
224 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
225 })
226 })