@@ -18,6 +18,7 @@ package picker
18
18
19
19
import (
20
20
"context"
21
+ "math"
21
22
"testing"
22
23
23
24
"github.com/google/go-cmp/cmp"
@@ -138,8 +139,8 @@ func TestPickMaxScorePicker(t *testing.T) {
138
139
139
140
func TestPickWeightedRandomPicker (t * testing.T ) {
140
141
const (
141
- testIterations = 1000
142
- tolerance = 0.2 // 20% tolerance in [0,1] range
142
+ testIterations = 10000
143
+ tolerance = 0.05 // Verify within tolerance ±5%
143
144
)
144
145
145
146
pod1 := & types.PodMetrics {Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod1" }}}
@@ -197,14 +198,14 @@ func TestPickWeightedRandomPicker(t *testing.T) {
197
198
for _ , test := range tests {
198
199
t .Run (test .name , func (t * testing.T ) {
199
200
picker := NewWeightedRandomPicker (test .maxPods )
200
- selectionCounts := make (map [string ]int )
201
201
202
- // Calculate expected probabilities based on scores
202
+ // Summarize the total score of all pods
203
203
totalScore := 0.0
204
204
for _ , pod := range test .input {
205
205
totalScore += pod .Score
206
206
}
207
207
208
+ // Calculate expected probabilities based on scores
208
209
expectedProbabilities := make (map [string ]float64 )
209
210
for _ , pod := range test .input {
210
211
podName := pod .GetPod ().NamespacedName .Name
@@ -216,32 +217,27 @@ func TestPickWeightedRandomPicker(t *testing.T) {
216
217
}
217
218
218
219
// Initialize selection counters for each pod
220
+ selectionCounts := make (map [string ]int )
219
221
for _ , pod := range test .input {
220
222
podName := pod .GetPod ().NamespacedName .Name
221
223
selectionCounts [podName ] = 0
222
224
}
223
225
224
226
// Run multiple iterations to gather statistical data
225
- for i := 0 ; i < testIterations ; i ++ {
227
+ for range testIterations {
226
228
result := picker .Pick (context .Background (), types .NewCycleState (), test .input )
227
229
228
230
// Count selections for probability analysis
229
- if len (result .TargetPods ) > 0 {
230
- selectedPodName := result .TargetPods [0 ].GetPod ().NamespacedName .Name
231
- selectionCounts [selectedPodName ]++
232
- }
231
+ selectedPodName := result .TargetPods [0 ].GetPod ().NamespacedName .Name
232
+ selectionCounts [selectedPodName ]++
233
233
}
234
234
235
235
// Verify probability distribution
236
236
for podName , expectedProb := range expectedProbabilities {
237
237
actualCount := selectionCounts [podName ]
238
238
actualProb := float64 (actualCount ) / float64 (testIterations )
239
239
240
- toleranceValue := expectedProb * tolerance
241
- lowerBound := expectedProb - toleranceValue
242
- upperBound := expectedProb + toleranceValue
243
-
244
- if actualProb < lowerBound || actualProb > upperBound {
240
+ if math .Abs (actualProb - expectedProb ) > tolerance {
245
241
t .Errorf ("Pod %s: expected probability %.3f ±%.1f%%, got %.3f (count: %d/%d)" ,
246
242
podName , expectedProb , tolerance * 100 , actualProb , actualCount , testIterations )
247
243
} else {
0 commit comments