diff --git a/pkg/epp/scheduling/framework/plugins/picker/picker_test.go b/pkg/epp/scheduling/framework/plugins/picker/picker_test.go index 741a49d59..022328efd 100644 --- a/pkg/epp/scheduling/framework/plugins/picker/picker_test.go +++ b/pkg/epp/scheduling/framework/plugins/picker/picker_test.go @@ -18,6 +18,7 @@ package picker import ( "context" + "math" "testing" "github.com/google/go-cmp/cmp" @@ -138,8 +139,8 @@ func TestPickMaxScorePicker(t *testing.T) { func TestPickWeightedRandomPicker(t *testing.T) { const ( - testIterations = 1000 - tolerance = 0.2 // 20% tolerance in [0,1] range + testIterations = 10000 + tolerance = 0.05 // Verify within tolerance ±5% ) pod1 := &types.PodMetrics{Pod: &backend.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}} @@ -197,14 +198,14 @@ func TestPickWeightedRandomPicker(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { picker := NewWeightedRandomPicker(test.maxPods) - selectionCounts := make(map[string]int) - // Calculate expected probabilities based on scores + // Summarize the total score of all pods totalScore := 0.0 for _, pod := range test.input { totalScore += pod.Score } + // Calculate expected probabilities based on scores expectedProbabilities := make(map[string]float64) for _, pod := range test.input { podName := pod.GetPod().NamespacedName.Name @@ -216,20 +217,19 @@ func TestPickWeightedRandomPicker(t *testing.T) { } // Initialize selection counters for each pod + selectionCounts := make(map[string]int) for _, pod := range test.input { podName := pod.GetPod().NamespacedName.Name selectionCounts[podName] = 0 } // Run multiple iterations to gather statistical data - for i := 0; i < testIterations; i++ { + for range testIterations { result := picker.Pick(context.Background(), types.NewCycleState(), test.input) // Count selections for probability analysis - if len(result.TargetPods) > 0 { - selectedPodName := result.TargetPods[0].GetPod().NamespacedName.Name - selectionCounts[selectedPodName]++ - } + selectedPodName := result.TargetPods[0].GetPod().NamespacedName.Name + selectionCounts[selectedPodName]++ } // Verify probability distribution @@ -237,11 +237,7 @@ func TestPickWeightedRandomPicker(t *testing.T) { actualCount := selectionCounts[podName] actualProb := float64(actualCount) / float64(testIterations) - toleranceValue := expectedProb * tolerance - lowerBound := expectedProb - toleranceValue - upperBound := expectedProb + toleranceValue - - if actualProb < lowerBound || actualProb > upperBound { + if math.Abs(actualProb-expectedProb) > tolerance { t.Errorf("Pod %s: expected probability %.3f ±%.1f%%, got %.3f (count: %d/%d)", podName, expectedProb, tolerance*100, actualProb, actualCount, testIterations) } else {