1
1
package backend
2
2
3
3
import (
4
+ "context"
5
+ "errors"
4
6
"sync"
5
7
"testing"
8
+ "time"
6
9
7
10
"github.com/google/go-cmp/cmp"
8
11
"github.com/google/go-cmp/cmp/cmpopts"
12
+ "github.com/stretchr/testify/assert"
9
13
"k8s.io/apimachinery/pkg/types"
10
- logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/ext-proc/util/logging"
11
14
)
12
15
13
16
var (
@@ -42,20 +45,15 @@ var (
42
45
)
43
46
44
47
func TestProvider (t * testing.T ) {
45
- logger := logutil .NewTestLogger ()
46
-
47
48
tests := []struct {
48
49
name string
49
50
pmc PodMetricsClient
50
51
datastore Datastore
51
52
want []* PodMetrics
52
53
}{
53
54
{
54
- name : "Fetch metrics error " ,
55
+ name : "Probing metrics success " ,
55
56
pmc : & FakePodMetricsClient {
56
- // Err: map[string]error{
57
- // pod2.Name: errors.New("injected error"),
58
- // },
59
57
Res : map [types.NamespacedName ]* PodMetrics {
60
58
pod1 .NamespacedName : pod1 ,
61
59
pod2 .NamespacedName : pod2 ,
@@ -67,42 +65,72 @@ func TestProvider(t *testing.T) {
67
65
want : []* PodMetrics {
68
66
pod1 ,
69
67
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
+ },
80
109
},
81
110
},
82
111
}
83
112
84
113
for _ , test := range tests {
85
114
t .Run (test .name , func (t * testing.T ) {
86
115
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 )
98
126
})
99
127
}
100
128
}
101
129
102
130
func populateMap (pods ... * PodMetrics ) * sync.Map {
103
131
newMap := & sync.Map {}
104
132
for _ , pod := range pods {
105
- newMap .Store (pod .NamespacedName , pod )
133
+ newMap .Store (pod .NamespacedName , & PodMetrics { NamespacedName : pod . NamespacedName } )
106
134
}
107
135
return newMap
108
136
}
0 commit comments