Skip to content

Commit a0cea4f

Browse files
committed
Fix the provider test
1 parent 37f17e8 commit a0cea4f

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

pkg/ext-proc/backend/provider_test.go

+57-29
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package backend
22

33
import (
4+
"context"
5+
"errors"
46
"sync"
57
"testing"
8+
"time"
69

710
"github.com/google/go-cmp/cmp"
811
"github.com/google/go-cmp/cmp/cmpopts"
12+
"github.com/stretchr/testify/assert"
913
"k8s.io/apimachinery/pkg/types"
10-
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/ext-proc/util/logging"
1114
)
1215

1316
var (
@@ -42,20 +45,15 @@ var (
4245
)
4346

4447
func TestProvider(t *testing.T) {
45-
logger := logutil.NewTestLogger()
46-
4748
tests := []struct {
4849
name string
4950
pmc PodMetricsClient
5051
datastore Datastore
5152
want []*PodMetrics
5253
}{
5354
{
54-
name: "Fetch metrics error",
55+
name: "Probing metrics success",
5556
pmc: &FakePodMetricsClient{
56-
// Err: map[string]error{
57-
// pod2.Name: errors.New("injected error"),
58-
// },
5957
Res: map[types.NamespacedName]*PodMetrics{
6058
pod1.NamespacedName: pod1,
6159
pod2.NamespacedName: pod2,
@@ -67,42 +65,72 @@ func TestProvider(t *testing.T) {
6765
want: []*PodMetrics{
6866
pod1,
6967
pod2,
70-
// // Failed to fetch pod2 metrics so it remains the default values.
71-
// {
72-
// Name: "pod2",
73-
// Metrics: Metrics{
74-
// WaitingQueueSize: 0,
75-
// KVCacheUsagePercent: 0,
76-
// MaxActiveModels: 0,
77-
// ActiveModels: map[string]int{},
78-
// },
79-
// },
68+
},
69+
},
70+
{
71+
name: "Only pods in the datastore are probed",
72+
pmc: &FakePodMetricsClient{
73+
Res: map[types.NamespacedName]*PodMetrics{
74+
pod1.NamespacedName: pod1,
75+
pod2.NamespacedName: pod2,
76+
},
77+
},
78+
datastore: &datastore{
79+
pods: populateMap(pod1),
80+
},
81+
want: []*PodMetrics{
82+
pod1,
83+
},
84+
},
85+
{
86+
name: "Probing metrics error",
87+
pmc: &FakePodMetricsClient{
88+
Err: map[types.NamespacedName]error{
89+
pod2.NamespacedName: errors.New("injected error"),
90+
},
91+
Res: map[types.NamespacedName]*PodMetrics{
92+
pod1.NamespacedName: pod1,
93+
},
94+
},
95+
datastore: &datastore{
96+
pods: populateMap(pod1, pod2),
97+
},
98+
want: []*PodMetrics{
99+
pod1,
100+
// Failed to fetch pod2 metrics so it remains the default values.
101+
{
102+
NamespacedName: pod2.NamespacedName,
103+
Metrics: Metrics{
104+
WaitingQueueSize: 0,
105+
KVCacheUsagePercent: 0,
106+
MaxActiveModels: 0,
107+
},
108+
},
80109
},
81110
},
82111
}
83112

84113
for _, test := range tests {
85114
t.Run(test.name, func(t *testing.T) {
86115
p := NewProvider(test.pmc, test.datastore)
87-
// if err := p.refreshMetricsOnce(logger); err != nil {
88-
// t.Fatalf("Unexpected error: %v", err)
89-
// }
90-
_ = p.refreshMetricsOnce(logger)
91-
metrics := test.datastore.PodGetAll()
92-
lessFunc := func(a, b *PodMetrics) bool {
93-
return a.String() < b.String()
94-
}
95-
if diff := cmp.Diff(test.want, metrics, cmpopts.SortSlices(lessFunc)); diff != "" {
96-
t.Errorf("Unexpected output (-want +got): %v", diff)
97-
}
116+
ctx, cancel := context.WithCancel(context.Background())
117+
defer cancel()
118+
p.Init(ctx, time.Millisecond, time.Millisecond)
119+
assert.EventuallyWithT(t, func(t *assert.CollectT) {
120+
metrics := test.datastore.PodGetAll()
121+
diff := cmp.Diff(test.want, metrics, cmpopts.SortSlices(func(a, b *PodMetrics) bool {
122+
return a.String() < b.String()
123+
}))
124+
assert.Equal(t, "", diff, "Unexpected diff (+got/-want)")
125+
}, 5*time.Second, time.Millisecond)
98126
})
99127
}
100128
}
101129

102130
func populateMap(pods ...*PodMetrics) *sync.Map {
103131
newMap := &sync.Map{}
104132
for _, pod := range pods {
105-
newMap.Store(pod.NamespacedName, pod)
133+
newMap.Store(pod.NamespacedName, &PodMetrics{NamespacedName: pod.NamespacedName})
106134
}
107135
return newMap
108136
}

0 commit comments

Comments
 (0)