|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | + |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ordering |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/flowcontrol" |
| 27 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/flowcontrol/mocks" |
| 28 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/scheduling" |
| 29 | +) |
| 30 | + |
| 31 | +func TestSLODeadlinePolicy_Name(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + policy := newSLODeadlinePolicy() |
| 34 | + assert.Equal(t, SLODeadlineOrderingPolicyType, policy.Name()) |
| 35 | +} |
| 36 | + |
| 37 | +func TestSLODeadlinePolicy_WithName(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + policy := newSLODeadlinePolicy().withName("test-name") |
| 40 | + assert.Equal(t, "test-name", policy.Name()) |
| 41 | +} |
| 42 | + |
| 43 | +func TestSLODeadlinePolicy_RequiredQueueCapabilities(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + policy := newSLODeadlinePolicy() |
| 46 | + caps := policy.RequiredQueueCapabilities() |
| 47 | + require.Len(t, caps, 1) |
| 48 | + assert.Equal(t, flowcontrol.CapabilityPriorityConfigurable, caps[0]) |
| 49 | +} |
| 50 | + |
| 51 | +// makeSLOItem builds a QueueItemAccessor with the given SLO header and received time. |
| 52 | +func makeSLOItem(id string, received time.Time, sloTTFTMs string) flowcontrol.QueueItemAccessor { |
| 53 | + req := mocks.NewMockFlowControlRequest(10, id, testFlowKey) |
| 54 | + req.ReceivedTimestampV = received |
| 55 | + req.InferenceRequestV = &scheduling.LLMRequest{Headers: map[string]string{sloTtftHeader: sloTTFTMs}} |
| 56 | + return &mocks.MockQueueItemAccessor{ |
| 57 | + EffectiveTTLV: 0, |
| 58 | + OriginalRequestV: req, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestSLODeadline_Less(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + policy := newSLODeadlinePolicy() |
| 65 | + |
| 66 | + now := time.Now() |
| 67 | + |
| 68 | + // A: received now, 100ms SLO → deadline now+100ms |
| 69 | + itemA := makeSLOItem("a", now, "100") |
| 70 | + // B: received now, 50ms SLO → deadline now+50ms (earlier) |
| 71 | + itemB := makeSLOItem("b", now, "50") |
| 72 | + // C: received now+20ms, 50ms SLO → deadline now+20ms+50ms = now+70ms (after B but earlier than A) |
| 73 | + itemC := makeSLOItem("c", now.Add(20*time.Millisecond), "50") |
| 74 | + // D: no header → far-future deadline |
| 75 | + reqD := mocks.NewMockFlowControlRequest(10, "d", testFlowKey) |
| 76 | + reqD.ReceivedTimestampV = now |
| 77 | + reqD.InferenceRequestV = &scheduling.LLMRequest{Headers: map[string]string{}} |
| 78 | + itemD := &mocks.MockQueueItemAccessor{EffectiveTTLV: 0, OriginalRequestV: reqD} |
| 79 | + // E: same deadline as B (received 1s earlier + 1050ms SLO = now+50ms), earlier ReceivedTimestamp → wins tie-breaker |
| 80 | + itemE := makeSLOItem("e", now.Add(-time.Second), "1050") |
| 81 | + |
| 82 | + testCases := []struct { |
| 83 | + name string |
| 84 | + a flowcontrol.QueueItemAccessor |
| 85 | + b flowcontrol.QueueItemAccessor |
| 86 | + expected bool |
| 87 | + }{ |
| 88 | + {"earlier SLO deadline first (B before A)", itemB, itemA, true}, |
| 89 | + {"later SLO deadline after (A after B)", itemA, itemB, false}, |
| 90 | + {"received later but earlier deadline (C before A)", itemC, itemA, true}, |
| 91 | + {"SLO-bound before no-header (A before D)", itemA, itemD, true}, |
| 92 | + {"no-header after SLO-bound (D after A)", itemD, itemA, false}, |
| 93 | + {"same deadline: earlier ReceivedTimestamp first (E before B)", itemE, itemB, true}, |
| 94 | + {"same deadline: later ReceivedTimestamp after (B after E)", itemB, itemE, false}, |
| 95 | + {"a is nil → b wins", nil, itemA, false}, |
| 96 | + {"b is nil → a wins", itemA, nil, true}, |
| 97 | + {"both nil → false", nil, nil, false}, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tc := range testCases { |
| 101 | + t.Run(tc.name, func(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + assert.Equal(t, tc.expected, policy.Less(tc.a, tc.b)) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestCalculateSLODeadline(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + |
| 111 | + now := time.Now() |
| 112 | + |
| 113 | + // Valid header |
| 114 | + reqValid := mocks.NewMockFlowControlRequest(1, "valid", testFlowKey) |
| 115 | + reqValid.ReceivedTimestampV = now |
| 116 | + reqValid.InferenceRequestV = &scheduling.LLMRequest{Headers: map[string]string{sloTtftHeader: "200"}} |
| 117 | + accValid := &mocks.MockQueueItemAccessor{OriginalRequestV: reqValid} |
| 118 | + deadline := calculateSLODeadline(accValid) |
| 119 | + assert.Equal(t, now.Add(200*time.Millisecond), deadline) |
| 120 | + |
| 121 | + // Missing header |
| 122 | + reqNoHeader := mocks.NewMockFlowControlRequest(2, "no", testFlowKey) |
| 123 | + reqNoHeader.InferenceRequestV = &scheduling.LLMRequest{Headers: map[string]string{}} |
| 124 | + accNoHeader := &mocks.MockQueueItemAccessor{OriginalRequestV: reqNoHeader} |
| 125 | + assert.Equal(t, sloMaxDeadlineTime, calculateSLODeadline(accNoHeader)) |
| 126 | + |
| 127 | + reqNoHeader.InferenceRequestV = &scheduling.LLMRequest{Headers: map[string]string{"x-some-header": "200"}} |
| 128 | + accNoHeader = &mocks.MockQueueItemAccessor{OriginalRequestV: reqNoHeader} |
| 129 | + assert.Equal(t, sloMaxDeadlineTime, calculateSLODeadline(accNoHeader)) |
| 130 | + |
| 131 | + // Invalid value |
| 132 | + reqInvalid := mocks.NewMockFlowControlRequest(3, "inv", testFlowKey) |
| 133 | + reqInvalid.InferenceRequestV = &scheduling.LLMRequest{Headers: map[string]string{sloTtftHeader: "x"}} |
| 134 | + accInvalid := &mocks.MockQueueItemAccessor{OriginalRequestV: reqInvalid} |
| 135 | + assert.Equal(t, sloMaxDeadlineTime, calculateSLODeadline(accInvalid)) |
| 136 | + |
| 137 | + // Nil OriginalRequest |
| 138 | + accNilReq := &mocks.MockQueueItemAccessor{OriginalRequestV: nil} |
| 139 | + assert.Equal(t, sloMaxDeadlineTime, calculateSLODeadline(accNilReq)) |
| 140 | + |
| 141 | + // Nil InferenceRequest |
| 142 | + reqNilInfReq := mocks.NewMockFlowControlRequest(4, "no-inf-req", testFlowKey) |
| 143 | + accNoInfReq := &mocks.MockQueueItemAccessor{OriginalRequestV: reqNilInfReq} |
| 144 | + assert.Equal(t, sloMaxDeadlineTime, calculateSLODeadline(accNoInfReq)) |
| 145 | +} |
0 commit comments