@@ -9629,3 +9629,148 @@ func TestAllocatorRebalanceTargetVoterConstraintUnsatisfied(t *testing.T) {
9629
9629
})
9630
9630
}
9631
9631
}
9632
+
9633
+ // TestRoundToNearestPriorityCategory tests the RoundToNearestPriorityCategory
9634
+ // function.
9635
+ func TestRoundToNearestPriorityCategory (t * testing.T ) {
9636
+ defer leaktest .AfterTest (t )()
9637
+
9638
+ testCases := []struct {
9639
+ name string
9640
+ input float64
9641
+ expected float64
9642
+ }{
9643
+ {
9644
+ name : "zero" ,
9645
+ input : 0.0 ,
9646
+ expected : 0.0 ,
9647
+ },
9648
+ {
9649
+ name : "exact multiple of 100" ,
9650
+ input : 100.0 ,
9651
+ expected : 100.0 ,
9652
+ },
9653
+ {
9654
+ name : "round down to nearest 100" ,
9655
+ input : 149.0 ,
9656
+ expected : 100.0 ,
9657
+ },
9658
+ {
9659
+ name : "round up to nearest 100" ,
9660
+ input : 151.0 ,
9661
+ expected : 200.0 ,
9662
+ },
9663
+ {
9664
+ name : "negative exact multiple of 100" ,
9665
+ input : - 200.0 ,
9666
+ expected : - 200.0 ,
9667
+ },
9668
+ {
9669
+ name : "negative round down to nearest 100" ,
9670
+ input : - 249.0 ,
9671
+ expected : - 200.0 ,
9672
+ },
9673
+ {
9674
+ name : "negative round up to nearest 100" ,
9675
+ input : - 251.0 ,
9676
+ expected : - 300.0 ,
9677
+ },
9678
+ }
9679
+
9680
+ for _ , tc := range testCases {
9681
+ t .Run (tc .name , func (t * testing.T ) {
9682
+ require .Equal (t , tc .expected , roundToNearestPriorityCategory (tc .input ))
9683
+ })
9684
+ }
9685
+ }
9686
+
9687
+ // TestCheckPriorityInversion tests the CheckPriorityInversion function.
9688
+ func TestCheckPriorityInversion (t * testing.T ) {
9689
+ defer leaktest .AfterTest (t )()
9690
+
9691
+ for action := AllocatorNoop ; action <= AllocatorFinalizeAtomicReplicationChange ; action ++ {
9692
+ t .Run (action .String (), func (t * testing.T ) {
9693
+ if action == AllocatorConsiderRebalance || action == AllocatorNoop || action == AllocatorRangeUnavailable {
9694
+ inversion , requeue := CheckPriorityInversion (action .Priority (), AllocatorConsiderRebalance )
9695
+ require .False (t , inversion )
9696
+ require .False (t , requeue )
9697
+ } else {
9698
+ inversion , requeue := CheckPriorityInversion (action .Priority (), AllocatorConsiderRebalance )
9699
+ require .True (t , inversion )
9700
+ require .True (t , requeue )
9701
+ }
9702
+ })
9703
+ }
9704
+
9705
+ testCases := []struct {
9706
+ name string
9707
+ priorityAtEnqueue float64
9708
+ actionAtProcessing AllocatorAction
9709
+ expectedInversion bool
9710
+ expectedRequeue bool
9711
+ }{
9712
+ {
9713
+ name : "AllocatorNoop at processing is noop" ,
9714
+ priorityAtEnqueue : AllocatorFinalizeAtomicReplicationChange .Priority (),
9715
+ actionAtProcessing : AllocatorNoop ,
9716
+ expectedInversion : true ,
9717
+ expectedRequeue : false ,
9718
+ },
9719
+ {
9720
+ name : "AllocatorRangeUnavailable at processing is noop" ,
9721
+ priorityAtEnqueue : AllocatorFinalizeAtomicReplicationChange .Priority (),
9722
+ actionAtProcessing : AllocatorRangeUnavailable ,
9723
+ expectedInversion : true ,
9724
+ expectedRequeue : false ,
9725
+ },
9726
+ {
9727
+ name : "priority -1 bypasses" ,
9728
+ priorityAtEnqueue : - 1 ,
9729
+ actionAtProcessing : AllocatorConsiderRebalance ,
9730
+ expectedInversion : false ,
9731
+ expectedRequeue : false ,
9732
+ },
9733
+ {
9734
+ name : "above range priority(1e5)" ,
9735
+ priorityAtEnqueue : 1e5 ,
9736
+ actionAtProcessing : AllocatorConsiderRebalance ,
9737
+ expectedInversion : false ,
9738
+ expectedRequeue : false ,
9739
+ },
9740
+ {
9741
+ name : "below range priority at -10" ,
9742
+ priorityAtEnqueue : - 10 ,
9743
+ actionAtProcessing : - 100 ,
9744
+ expectedInversion : false ,
9745
+ expectedRequeue : false ,
9746
+ },
9747
+ {
9748
+ name : "inversion but small priority changes" ,
9749
+ priorityAtEnqueue : AllocatorFinalizeAtomicReplicationChange .Priority (),
9750
+ actionAtProcessing : AllocatorReplaceDecommissioningNonVoter ,
9751
+ expectedInversion : true ,
9752
+ expectedRequeue : false ,
9753
+ },
9754
+ {
9755
+ name : "inversion but small priority changes" ,
9756
+ priorityAtEnqueue : AllocatorRemoveDeadVoter .Priority (),
9757
+ actionAtProcessing : AllocatorAddNonVoter ,
9758
+ expectedInversion : true ,
9759
+ expectedRequeue : false ,
9760
+ },
9761
+ {
9762
+ name : "inversion but small priority changes" ,
9763
+ priorityAtEnqueue : AllocatorConsiderRebalance .Priority (),
9764
+ actionAtProcessing : AllocatorNoop ,
9765
+ expectedInversion : false ,
9766
+ expectedRequeue : false ,
9767
+ },
9768
+ }
9769
+ for _ , tc := range testCases {
9770
+ t .Run (tc .name , func (t * testing.T ) {
9771
+ inversion , requeue := CheckPriorityInversion (tc .priorityAtEnqueue , tc .actionAtProcessing )
9772
+ require .Equal (t , tc .expectedInversion , inversion )
9773
+ require .Equal (t , tc .expectedRequeue , requeue )
9774
+ })
9775
+ }
9776
+ }
0 commit comments