@@ -229,46 +229,54 @@ def test_update_queue_threshold_asymmetric_ewma(self):
229229 self .assertGreater (policy ._q_level_nbytes [mock_op ], 150.0 )
230230 self .assertLess (policy ._q_level_nbytes [mock_op ], 200.0 )
231231
232- def test_update_queue_threshold_downward_smoothing (self ):
233- """Test threshold update with downward smoothing logic ."""
232+ def test_update_queue_threshold_no_decrease (self ):
233+ """Test that thresholds are never decreased, only maintained or increased ."""
234234 mock_op = MagicMock ()
235235 policy = ConcurrencyCapBackpressurePolicy (
236236 DataContext .get_current (),
237237 {mock_op : MagicMock ()},
238238 MagicMock (),
239239 )
240240
241- # Set up initial state with high threshold and very low level/dev to force downward adjustment
241+ # Set up initial state with high threshold
242242 policy ._queue_thresholds [mock_op ] = 200
243243 policy ._q_level_nbytes [mock_op ] = 10.0 # Very low level
244244 policy ._q_level_dev [mock_op ] = 1.0 # Very low deviation
245245 policy ._queue_history [mock_op ] = deque ([10 , 11 , 12 , 13 , 14 , 15 ])
246246
247- # Test downward adjustment (should be smoothed)
248- # threshold = max(10 + 4*1, 150) = 150, which is < 200, so should be smoothed
247+ # Test that threshold is maintained when calculated threshold is lower
249248 threshold = policy ._update_queue_threshold (mock_op , 150 )
250249
251- # Should be smoothed between 200 and 150
252- self .assertLess (threshold , 200 )
253- self .assertGreaterEqual ( threshold , 150 )
250+ # Should maintain the existing threshold (no decrease)
251+ self .assertEqual (threshold , 200 )
252+ self .assertEqual ( policy . _queue_thresholds [ mock_op ], 200 )
254253
255- # Test that the method works correctly - just verify it doesn't crash
256- # and returns a reasonable threshold value
257- mock_op2 = MagicMock ()
258- policy2 = ConcurrencyCapBackpressurePolicy (
254+ # Test with even lower queue size
255+ threshold_small = policy ._update_queue_threshold (mock_op , 50 )
256+ self .assertEqual (threshold_small , 200 ) # Still maintained
257+ self .assertEqual (policy ._queue_thresholds [mock_op ], 200 )
258+
259+ def test_update_queue_threshold_increase (self ):
260+ """Test that thresholds are increased when calculated threshold is higher."""
261+ mock_op = MagicMock ()
262+ policy = ConcurrencyCapBackpressurePolicy (
259263 DataContext .get_current (),
260- {mock_op2 : MagicMock ()},
264+ {mock_op : MagicMock ()},
261265 MagicMock (),
262266 )
263- policy2 ._queue_thresholds [mock_op2 ] = 200
264- policy2 ._q_level_nbytes [mock_op2 ] = 10.0
265- policy2 ._q_level_dev [mock_op2 ] = 1.0
266- policy2 ._queue_history [mock_op2 ] = deque ([10 , 11 , 12 , 13 , 14 , 15 ])
267267
268- threshold_small = policy2 ._update_queue_threshold (mock_op2 , 50 )
268+ # Set up initial state with moderate threshold
269+ policy ._queue_thresholds [mock_op ] = 100
270+ policy ._q_level_nbytes [mock_op ] = 50.0
271+ policy ._q_level_dev [mock_op ] = 20.0
272+ policy ._queue_history [mock_op ] = deque ([50 , 60 , 70 , 80 , 90 , 100 ])
273+
274+ # Test that threshold is increased when calculated threshold is higher
275+ threshold = policy ._update_queue_threshold (mock_op , 200 )
269276
270- # Just verify it returns a reasonable threshold (at least as high as input)
271- self .assertGreaterEqual (threshold_small , 50 )
277+ # Should increase the threshold
278+ self .assertGreaterEqual (threshold , 200 )
279+ self .assertGreaterEqual (policy ._queue_thresholds [mock_op ], 200 )
272280
273281 def test_effective_cap_calculation_with_trend (self ):
274282 """Test effective cap calculation with different trend scenarios."""
@@ -474,7 +482,7 @@ def test_threshold_calculation_formula(self):
474482 self .assertAlmostEqual (threshold , expected , places = 5 )
475483
476484 def test_threshold_update_logic_comprehensive (self ):
477- """Test comprehensive threshold update logic including bootstrap, upward, and downward cases."""
485+ """Test comprehensive threshold update logic including bootstrap, upward, and no-decrease cases."""
478486 mock_op = MagicMock ()
479487 policy = ConcurrencyCapBackpressurePolicy (
480488 DataContext .get_current (),
@@ -489,7 +497,7 @@ def test_threshold_update_logic_comprehensive(self):
489497 # Bootstrap: threshold = max(level + K_DEV * dev, q_now) = max(100 + 4*0, 100) = 100
490498 self .assertEqual (threshold1 , 100 )
491499
492- # Test 2: Upward adjustment (threshold >= prev_threshold)
500+ # Test 2: Upward adjustment (threshold > prev_threshold)
493501 policy ._queue_thresholds [mock_op ] = 100
494502 policy ._q_level_nbytes [mock_op ] = 50.0
495503 policy ._q_level_dev [mock_op ] = 10.0
@@ -499,17 +507,13 @@ def test_threshold_update_logic_comprehensive(self):
499507 # Just verify it's >= 200 (upward adjustment)
500508 self .assertGreaterEqual (threshold2 , 200 )
501509
502- # Test 3: Downward adjustment (threshold < prev_threshold)
510+ # Test 3: No decrease (threshold < prev_threshold, should maintain existing )
503511 policy ._queue_thresholds [mock_op ] = 200
504- policy ._q_level_nbytes [
505- mock_op
506- ] = 10.0 # Very low level to force downward adjustment
512+ policy ._q_level_nbytes [mock_op ] = 10.0 # Very low level
507513 policy ._q_level_dev [mock_op ] = 1.0 # Very low deviation
508514 policy ._queue_history [mock_op ] = deque ([10 , 11 , 12 , 13 , 14 , 15 ])
509515 threshold3 = policy ._update_queue_threshold (mock_op , 150 )
510- # threshold = max(10 + 4*1, 150) = 150, which is < 200, so should be smoothed
511- self .assertLess (threshold3 , 200 )
512- self .assertGreaterEqual (threshold3 , 150 )
516+ self .assertEqual (threshold3 , 200 )
513517
514518 # Test 4: Zero threshold case
515519 fresh_mock_op = MagicMock ()
@@ -521,8 +525,8 @@ def test_threshold_update_logic_comprehensive(self):
521525 fresh_policy ._queue_thresholds [fresh_mock_op ] = 0
522526 fresh_policy ._queue_history [fresh_mock_op ] = deque ([0 ])
523527 # Fresh policy starts with clean EWMA state
524- threshold5 = fresh_policy ._update_queue_threshold (fresh_mock_op , 0 )
525- self .assertEqual (threshold5 , 1 ) # Should round up to 1
528+ threshold4 = fresh_policy ._update_queue_threshold (fresh_mock_op , 0 )
529+ self .assertEqual (threshold4 , 1 ) # Should round up to 1
526530
527531 def test_trend_and_effective_cap_formulas (self ):
528532 """Test trend calculation and effective cap formulas."""
0 commit comments