Skip to content

Commit 73c9eaf

Browse files
committed
updated max score picker unit tests to cover multiple pods
Signed-off-by: Nir Rozenbaum <[email protected]>
1 parent 2b6e9fe commit 73c9eaf

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

pkg/epp/scheduling/framework/plugins/picker/max_score_picker.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"sort"
23+
"slices"
2424

2525
"sigs.k8s.io/controller-runtime/pkg/log"
2626
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
@@ -82,8 +82,14 @@ func (p *MaxScorePicker) Pick(ctx context.Context, cycleState *types.CycleState,
8282
log.FromContext(ctx).V(logutil.DEBUG).Info(fmt.Sprintf("Selecting maximum '%d' pods from %d candidates sorted by max score: %+v", p.maxNumOfEndpoints,
8383
len(scoredPods), scoredPods))
8484

85-
sort.Slice(scoredPods, func(i, j int) bool { // highest score first
86-
return scoredPods[i].Score > scoredPods[j].Score
85+
slices.SortStableFunc(scoredPods, func(i, j *types.ScoredPod) int { // highest score first
86+
if i.Score > j.Score {
87+
return -1
88+
}
89+
if i.Score < j.Score {
90+
return 1
91+
}
92+
return 0
8793
})
8894

8995
// if we have enough pods to return keep only the "maxNumOfEndpoints" highest scored pods

pkg/epp/scheduling/framework/plugins/picker/picker_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
"github.com/google/go-cmp/cmp"
24-
"github.com/google/go-cmp/cmp/cmpopts"
2524
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend"
2625
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework"
2726
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
@@ -89,21 +88,21 @@ func TestPickMaxScorePicker(t *testing.T) {
8988
output: []types.Pod{
9089
&types.ScoredPod{Pod: pod3, Score: 30},
9190
&types.ScoredPod{Pod: pod2, Score: 25},
92-
&types.ScoredPod{Pod: pod2, Score: 20},
91+
&types.ScoredPod{Pod: pod1, Score: 20},
9392
},
9493
},
9594
{
9695
name: "Multiple results sorted by highest score, num of pods exactly needed",
9796
picker: NewMaxScorePicker(3), // picker is required to return 3 pods at most, we have only 3.
9897
input: []*types.ScoredPod{
99-
{Pod: pod1, Score: 20},
98+
{Pod: pod1, Score: 30},
10099
{Pod: pod2, Score: 25},
101100
{Pod: pod3, Score: 30},
102101
},
103102
output: []types.Pod{
103+
&types.ScoredPod{Pod: pod1, Score: 30},
104104
&types.ScoredPod{Pod: pod3, Score: 30},
105105
&types.ScoredPod{Pod: pod2, Score: 25},
106-
&types.ScoredPod{Pod: pod2, Score: 20},
107106
},
108107
},
109108
}
@@ -113,10 +112,7 @@ func TestPickMaxScorePicker(t *testing.T) {
113112
result := test.picker.Pick(context.Background(), types.NewCycleState(), test.input)
114113
got := result.TargetPods
115114

116-
diff := cmp.Diff(test.output, got, cmpopts.SortSlices(func(a, b types.Pod) bool {
117-
return a.GetPod().NamespacedName.String() < b.GetPod().NamespacedName.String()
118-
}))
119-
if diff != "" {
115+
if diff := cmp.Diff(test.output, got); diff != "" {
120116
t.Errorf("Unexpected output (-want +got): %v", diff)
121117
}
122118
})

0 commit comments

Comments
 (0)