Skip to content

Commit 5140a28

Browse files
committed
allocator: add TestAllocatorPriorityInvariance
This commit adds the TestAllocatorPriorityInvariance test, which acts as a regression safeguard when new actions are added to AllocatorAction, ensuring the contract is upheld. See action.Priority() and ComputeAction() for more details on the contract.
1 parent ad4d147 commit 5140a28

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pkg/kv/kvserver/allocator/allocatorimpl/allocator_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9730,6 +9730,13 @@ func TestCheckPriorityInversion(t *testing.T) {
97309730
expectedInversion: false,
97319731
expectedRequeue: false,
97329732
},
9733+
{
9734+
name: "priority increase",
9735+
priorityAtEnqueue: 0,
9736+
actionAtProcessing: AllocatorFinalizeAtomicReplicationChange,
9737+
expectedInversion: false,
9738+
expectedRequeue: false,
9739+
},
97339740
{
97349741
name: "above range priority(1e5)",
97359742
priorityAtEnqueue: 1e5,
@@ -9774,3 +9781,29 @@ func TestCheckPriorityInversion(t *testing.T) {
97749781
})
97759782
}
97769783
}
9784+
9785+
// TestAllocatorPriorityInvariance verifies that allocator priorities remain
9786+
// spaced in multiples of 100. This prevents regressions against the contract
9787+
// relied on by CheckPriorityInversion. For details, see the comment above
9788+
// action.Priority().
9789+
func TestAllocatorPriorityInvariance(t *testing.T) {
9790+
defer leaktest.AfterTest(t)()
9791+
9792+
exceptions := map[AllocatorAction]struct{}{
9793+
AllocatorFinalizeAtomicReplicationChange: {},
9794+
AllocatorRemoveLearner: {},
9795+
AllocatorReplaceDeadVoter: {},
9796+
}
9797+
lowestPriority := AllocatorNoop.Priority()
9798+
for action := AllocatorNoop; action < AllocatorMaxPriority; action++ {
9799+
require.GreaterOrEqualf(t, action.Priority(), lowestPriority,
9800+
"priority %f is less than AllocatorNoop: likely violating contract",
9801+
action.Priority())
9802+
if _, ok := exceptions[action]; !ok {
9803+
require.Equalf(t, int(action.Priority())%100, 0,
9804+
"priority %f is not a multiple of 100: likely violating contract",
9805+
action.Priority())
9806+
9807+
}
9808+
}
9809+
}

0 commit comments

Comments
 (0)