@@ -38,6 +38,7 @@ func TestDynamicChannelScaling(t *testing.T) {
3838 CheckInterval : 10 * time .Second , // Not directly used by calling evaluateAndScale
3939 MaxRemoveConns : 3 ,
4040 ContinuousDownscaleRunsThreshold : 3 ,
41+ MaxScaleUpPercentage : 30 ,
4142 }
4243 tests := []struct {
4344 name string
@@ -51,10 +52,14 @@ func TestDynamicChannelScaling(t *testing.T) {
5152 name : "ScaleUp" ,
5253 initialSize : 3 ,
5354 setLoad : func (conns []* connEntry ) {
54- setConnLoads (conns , 12 , 0 ) // Avg load 12 > 10
55+ // Target load per conn is 6.5.
56+ // Total load = 12 * 3 = 36. Desired = ceil(36 / 6.5) = 6.
57+ // try to add 3 conns
58+ setConnLoads (conns , 12 , 0 )
5559 },
5660 // Total load = 3 * 12 = 36. Desired = ceil(36 / 6.5) = 6
57- wantSize : 6 ,
61+ // capped by 30% of 3, ceil (0.9) => 1
62+ wantSize : 4 ,
5863 evaluateCalls : 1 ,
5964 },
6065 {
@@ -122,10 +127,11 @@ func TestDynamicChannelScaling(t *testing.T) {
122127 name : "ScaleUpAddAtLeastOne" ,
123128 initialSize : 2 ,
124129 setLoad : func (conns []* connEntry ) {
130+ // 10*2= 20 total load, conn count = ceil(20/6.5) = 4
125131 setConnLoads (conns , 10 , 0 ) // Avg load 10, right at threshold.
126132 },
127- // Total load = 20. Desired = ceil(20 / 6.5) = 4. Add 2.
128- wantSize : 4 ,
133+ // Total load = 20. Desired = ceil(20 / 6.5) = 4. Add 2 but capped by 30%
134+ wantSize : 3 ,
129135 evaluateCalls : 1 ,
130136 },
131137 {
@@ -137,6 +143,54 @@ func TestDynamicChannelScaling(t *testing.T) {
137143 wantSize : 6 ,
138144 evaluateCalls : 2 ,
139145 },
146+ {
147+ name : "ScaleUpCappedByMaxScaleUpPercentage" ,
148+ initialSize : 10 ,
149+ configOpt : func (cfg * btopt.DynamicChannelPoolConfig ) {
150+ cfg .MaxConns = 50 // Increase max conns so it doesn't artificially cap the test
151+ cfg .MaxScaleUpPercentage = 20 // 20% of 10 = 2 max additions allowed
152+ },
153+ setLoad : func (conns []* connEntry ) {
154+ // Target load per conn is 6.5.
155+ // Total load = 10 * 20 = 200. Desired = ceil(200 / 6.5) = 31.
156+ // Normally it would try to add 21 connections.
157+ setConnLoads (conns , 20 , 0 )
158+ },
159+ // Capped at adding 2 connections
160+ wantSize : 12 ,
161+ evaluateCalls : 1 ,
162+ },
163+ {
164+ name : "ScaleUpNotCappedByMaxScaleUpPercentage" ,
165+ initialSize : 10 ,
166+ configOpt : func (cfg * btopt.DynamicChannelPoolConfig ) {
167+ cfg .MaxConns = 50
168+ cfg .MaxScaleUpPercentage = 100 // 100% of 10 = 10
169+ },
170+ setLoad : func (conns []* connEntry ) {
171+ // Total load = 10 * 10 = 100. Desired = ceil(100 / 6.5) = 16.
172+ setConnLoads (conns , 10 , 0 )
173+ },
174+ // max allowed 10, so 6 is fine
175+ wantSize : 16 ,
176+ evaluateCalls : 1 ,
177+ },
178+ {
179+ name : "ScaleUpCeilFractionalCap" ,
180+ initialSize : 4 ,
181+ configOpt : func (cfg * btopt.DynamicChannelPoolConfig ) {
182+ cfg .MaxConns = 20
183+ cfg .MaxScaleUpPercentage = 30 // 30% of 4 = 1.2 -> ceil(1.2) = 2 allowed
184+ },
185+ setLoad : func (conns []* connEntry ) {
186+ // Total load = 4 * 20 = 80. Desired = ceil(80 / 6.5) = 13.
187+ // Wants to add 9 connections.
188+ setConnLoads (conns , 20 , 0 )
189+ },
190+ // Capped at adding 2 connections due to math.Ceil(1.2)
191+ wantSize : 6 ,
192+ evaluateCalls : 1 ,
193+ },
140194 }
141195
142196 for _ , tc := range tests {
0 commit comments