Add fair sharing worker choice strategy
[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 {
15 LessRecentlyUsedWorkerChoiceStrategy
16} = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy')
17const {
23ff945a
JB
18 FairShareWorkerChoiceStrategy
19} = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
20// const {
21// WeightedRoundRobinWorkerChoiceStrategy
22// } = require('../../../lib/pools/selection-strategies/weighted-round-robin-choice-strategy')
40ad1d27
JB
23const {
24 DynamicPoolWorkerChoiceStrategy
25} = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
26
27describe('Worker choice strategy context test suite', () => {
28 let fixedPool, dynamicPool
29 beforeEach(() => {
30 fixedPool = sinon.createStubInstance(FixedThreadPool)
31 dynamicPool = sinon.createStubInstance(DynamicThreadPool)
32 })
33
34 afterEach(() => {
35 sinon.restore()
36 })
37
38 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
39 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
40 fixedPool
41 )
42 const WorkerChoiceStrategyStub = sinon.createStubInstance(
43 RoundRobinWorkerChoiceStrategy,
44 {
45 choose: sinon.stub().returns('worker')
46 }
47 )
48 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
49 const worker = workerChoiceStrategyContext.execute()
50 expect(
51 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
52 ).toBe(true)
53 expect(worker).toBe('worker')
54 })
55
56 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
57 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
58 dynamicPool
59 )
60 const WorkerChoiceStrategyStub = sinon.createStubInstance(
61 RoundRobinWorkerChoiceStrategy,
62 {
63 choose: sinon.stub().returns('worker')
64 }
65 )
66 workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
67 const worker = workerChoiceStrategyContext.execute()
68 expect(
69 workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
70 ).toBe(true)
71 expect(worker).toBe('worker')
72 })
73
74 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
75 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
76 fixedPool
77 )
78 workerChoiceStrategyContext.setWorkerChoiceStrategy(
79 WorkerChoiceStrategies.ROUND_ROBIN
80 )
81 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
82 RoundRobinWorkerChoiceStrategy
83 )
84 })
85
23ff945a 86 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
40ad1d27
JB
87 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
88 dynamicPool
89 )
90 workerChoiceStrategyContext.setWorkerChoiceStrategy(
91 WorkerChoiceStrategies.ROUND_ROBIN
92 )
93 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
94 DynamicPoolWorkerChoiceStrategy
95 )
96 })
97
98 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
99 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
100 fixedPool
101 )
102 workerChoiceStrategyContext.setWorkerChoiceStrategy(
103 WorkerChoiceStrategies.LESS_RECENTLY_USED
104 )
105 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
106 LessRecentlyUsedWorkerChoiceStrategy
107 )
108 })
109
23ff945a 110 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
40ad1d27
JB
111 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
112 dynamicPool
113 )
114 workerChoiceStrategyContext.setWorkerChoiceStrategy(
115 WorkerChoiceStrategies.LESS_RECENTLY_USED
116 )
117 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
118 DynamicPoolWorkerChoiceStrategy
119 )
120 })
23ff945a
JB
121
122 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
123 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
124 fixedPool
125 )
126 workerChoiceStrategyContext.setWorkerChoiceStrategy(
127 WorkerChoiceStrategies.FAIR_SHARE
128 )
129 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
130 FairShareWorkerChoiceStrategy
131 )
132 })
133
134 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
135 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
136 dynamicPool
137 )
138 workerChoiceStrategyContext.setWorkerChoiceStrategy(
139 WorkerChoiceStrategies.FAIR_SHARE
140 )
141 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
142 DynamicPoolWorkerChoiceStrategy
143 )
144 })
145
146 // it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
147 // const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
148 // fixedPool
149 // )
150 // workerChoiceStrategyContext.setWorkerChoiceStrategy(
151 // WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
152 // )
153 // expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
154 // WeightedRoundRobinWorkerChoiceStrategy
155 // )
156 // })
157
158 // it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
159 // const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
160 // dynamicPool
161 // )
162 // workerChoiceStrategyContext.setWorkerChoiceStrategy(
163 // WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
164 // )
165 // expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
166 // DynamicPoolWorkerChoiceStrategy
167 // )
168 // })
40ad1d27 169})