perf: remove unneeded class indirection for dynamic pool in 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 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 RoundRobinWorkerChoiceStrategy
110 )
111 })
112
113 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
114 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
115 fixedPool
116 )
117 workerChoiceStrategyContext.setWorkerChoiceStrategy(
118 WorkerChoiceStrategies.LESS_USED
119 )
120 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
121 LessUsedWorkerChoiceStrategy
122 )
123 })
124
125 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
126 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
127 dynamicPool
128 )
129 workerChoiceStrategyContext.setWorkerChoiceStrategy(
130 WorkerChoiceStrategies.LESS_USED
131 )
132 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
133 LessUsedWorkerChoiceStrategy
134 )
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 )
144 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
145 LessBusyWorkerChoiceStrategy
146 )
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 )
156 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
157 LessBusyWorkerChoiceStrategy
158 )
159 })
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 )
168 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
169 FairShareWorkerChoiceStrategy
170 )
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 )
180 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
181 FairShareWorkerChoiceStrategy
182 )
183 })
184
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 )
192 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
193 WeightedRoundRobinWorkerChoiceStrategy
194 )
195 })
196
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 )
204 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
205 WeightedRoundRobinWorkerChoiceStrategy
206 )
207 })
208 })