perf: remove unneeded class indirection for dynamic pool in worker
[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
26
27describe('Worker choice strategy context test suite', () => {
c15273f2
JB
28 const min = 1
29 const max = 3
40ad1d27 30 let fixedPool, dynamicPool
c15273f2
JB
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 )
40ad1d27
JB
42 })
43
44 afterEach(() => {
45 sinon.restore()
46 })
47
fd7ebd49
JB
48 after(async () => {
49 await fixedPool.destroy()
50 await dynamicPool.destroy()
c15273f2
JB
51 })
52
40ad1d27
JB
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 {
c923ce56 60 choose: sinon.stub().returns(0)
40ad1d27
JB
61 }
62 )
63 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
c923ce56 64 const chosenWorkerKey = workerChoiceStrategyContext.execute()
40ad1d27 65 expect(
5502c07c 66 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
40ad1d27 67 ).toBe(true)
c923ce56 68 expect(chosenWorkerKey).toBe(0)
40ad1d27
JB
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 {
c923ce56 78 choose: sinon.stub().returns(0)
40ad1d27
JB
79 }
80 )
81 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
c923ce56 82 const chosenWorkerKey = workerChoiceStrategyContext.execute()
40ad1d27 83 expect(
5502c07c 84 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
40ad1d27 85 ).toBe(true)
c923ce56 86 expect(chosenWorkerKey).toBe(0)
40ad1d27
JB
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 )
5502c07c
JB
96 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
97 RoundRobinWorkerChoiceStrategy
98 )
40ad1d27
JB
99 })
100
23ff945a 101 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
40ad1d27
JB
102 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
103 dynamicPool
104 )
105 workerChoiceStrategyContext.setWorkerChoiceStrategy(
106 WorkerChoiceStrategies.ROUND_ROBIN
107 )
5502c07c 108 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
9cd39dd4 109 RoundRobinWorkerChoiceStrategy
5502c07c 110 )
40ad1d27
JB
111 })
112
737c6d97 113 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
40ad1d27
JB
114 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
115 fixedPool
116 )
117 workerChoiceStrategyContext.setWorkerChoiceStrategy(
737c6d97 118 WorkerChoiceStrategies.LESS_USED
40ad1d27 119 )
5502c07c
JB
120 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
121 LessUsedWorkerChoiceStrategy
122 )
40ad1d27
JB
123 })
124
737c6d97 125 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
40ad1d27
JB
126 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
127 dynamicPool
128 )
129 workerChoiceStrategyContext.setWorkerChoiceStrategy(
737c6d97 130 WorkerChoiceStrategies.LESS_USED
40ad1d27 131 )
5502c07c 132 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
9cd39dd4 133 LessUsedWorkerChoiceStrategy
5502c07c 134 )
168c526f
JB
135 })
136
137 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
138 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
139 fixedPool
140 )
141 workerChoiceStrategyContext.setWorkerChoiceStrategy(
142 WorkerChoiceStrategies.LESS_BUSY
143 )
5502c07c
JB
144 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
145 LessBusyWorkerChoiceStrategy
146 )
168c526f
JB
147 })
148
149 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
150 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
151 dynamicPool
152 )
153 workerChoiceStrategyContext.setWorkerChoiceStrategy(
154 WorkerChoiceStrategies.LESS_BUSY
155 )
5502c07c 156 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
9cd39dd4 157 LessBusyWorkerChoiceStrategy
5502c07c 158 )
40ad1d27 159 })
23ff945a
JB
160
161 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
162 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
163 fixedPool
164 )
165 workerChoiceStrategyContext.setWorkerChoiceStrategy(
166 WorkerChoiceStrategies.FAIR_SHARE
167 )
5502c07c
JB
168 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
169 FairShareWorkerChoiceStrategy
170 )
23ff945a
JB
171 })
172
173 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
174 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
175 dynamicPool
176 )
177 workerChoiceStrategyContext.setWorkerChoiceStrategy(
178 WorkerChoiceStrategies.FAIR_SHARE
179 )
5502c07c 180 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
9cd39dd4 181 FairShareWorkerChoiceStrategy
5502c07c 182 )
23ff945a
JB
183 })
184
c15273f2
JB
185 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
186 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
187 fixedPool
188 )
189 workerChoiceStrategyContext.setWorkerChoiceStrategy(
190 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
191 )
5502c07c
JB
192 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
193 WeightedRoundRobinWorkerChoiceStrategy
194 )
c15273f2 195 })
23ff945a 196
c15273f2
JB
197 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
198 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
199 dynamicPool
200 )
201 workerChoiceStrategyContext.setWorkerChoiceStrategy(
202 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
203 )
5502c07c 204 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
9cd39dd4 205 WeightedRoundRobinWorkerChoiceStrategy
5502c07c 206 )
c15273f2 207 })
40ad1d27 208})