Skip to content

Commit 73612e7

Browse files
committed
address comments fix
1 parent 0da2b21 commit 73612e7

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

conformance/tests/grpcroute-weight.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ var GRPCRouteWeight = suite.ConformanceTest{
8585
return resp.Response.GetAssertions().GetContext().GetPod(), nil
8686
})
8787

88-
for i := 0; i < 10; i++ {
88+
for i := 0; i < weight.MaxTestRetries; i++ {
8989
if err := weight.TestWeightedDistribution(sender, expectedWeights); err != nil {
90-
t.Logf("Traffic distribution test failed (%d/10): %s", i+1, err)
90+
t.Logf("Traffic distribution test failed (%d/%d): %s", i+1, weight.MaxTestRetries, err)
9191
} else {
9292
return
9393
}

conformance/tests/httproute-weight.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ var HTTPRouteWeight = suite.ConformanceTest{
8383
return cReq.Pod, nil
8484
})
8585

86-
for i := 0; i < 10; i++ {
86+
for i := 0; i < weight.MaxTestRetries; i++ {
8787
if err := weight.TestWeightedDistribution(sender, expectedWeights); err != nil {
88-
t.Logf("Traffic distribution test failed (%d/10): %s", i+1, err)
88+
t.Logf("Traffic distribution test failed (%d/%d): %s", i+1, weight.MaxTestRetries, err)
8989
} else {
9090
return
9191
}

conformance/tests/mesh/grpcroute-weight.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ var MeshGRPCRouteWeight = suite.ConformanceTest{
7070
return cRes.Hostname, nil
7171
})
7272

73-
for i := 0; i < 10; i++ {
73+
for i := 0; i < weight.MaxTestRetries; i++ {
7474
if err := weight.TestWeightedDistribution(sender, expectedWeights); err != nil {
75-
t.Logf("Traffic distribution test failed (%d/10): %s", i+1, err)
75+
t.Logf("Traffic distribution test failed (%d/%d): %s", i+1, weight.MaxTestRetries, err)
7676
} else {
7777
return
7878
}

conformance/tests/mesh/grpcroute-weight.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ spec:
1717
- name: echo-v2
1818
port: 7070
1919
weight: 30
20+
- name: echo-v3
21+
port: 7070
22+
weight: 0

conformance/tests/mesh/httproute-weight.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ var MeshHTTPRouteWeight = suite.ConformanceTest{
7070
return cRes.Hostname, nil
7171
})
7272

73-
for i := 0; i < 10; i++ {
73+
for i := 0; i < weight.MaxTestRetries; i++ {
7474
if err := weight.TestWeightedDistribution(sender, expectedWeights); err != nil {
75-
t.Logf("Traffic distribution test failed (%d/10): %s", i+1, err)
75+
t.Logf("Traffic distribution test failed (%d/%d): %s", i+1, weight.MaxTestRetries, err)
7676
} else {
7777
return
7878
}

conformance/utils/weight/weight.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ package weight
1818

1919
import (
2020
"cmp"
21+
"crypto/rand"
2122
"errors"
2223
"fmt"
2324
"math"
24-
"math/rand/v2"
25+
"math/big"
2526
"slices"
2627
"strconv"
2728
"strings"
@@ -31,6 +32,12 @@ import (
3132
"golang.org/x/sync/errgroup"
3233
)
3334

35+
const (
36+
// MaxTestRetries is the maximum number of times to retry the weight distribution test
37+
// if it fails due to statistical variance before considering it a real failure
38+
MaxTestRetries = 10
39+
)
40+
3441
// RequestSender defines an interface for sending requests (HTTP, gRPC, or mesh)
3542
type RequestSender interface {
3643
SendRequest() (podName string, err error)
@@ -132,27 +139,46 @@ func TestWeightedDistribution(sender RequestSender, expectedWeights map[string]f
132139

133140
// addRandomDelay adds a random delay up to the specified limit in milliseconds
134141
func addRandomDelay(limit int) {
135-
randomSleepDuration := rand.IntN(limit)
142+
n, err := rand.Int(rand.Reader, big.NewInt(int64(limit)))
143+
if err != nil {
144+
// Fallback to no delay if crypto/rand fails
145+
return
146+
}
147+
randomSleepDuration := n.Int64()
136148
time.Sleep(time.Duration(randomSleepDuration) * time.Millisecond)
137149
}
138150

139151
// AddRandomEntropy randomly chooses to add delay, random value, or both
140152
// The addRandomValue function should be provided by the caller to handle
141153
// protocol-specific ways of adding the random value (HTTP headers, gRPC metadata, etc.)
142154
func AddRandomEntropy(addRandomValue func(string) error) error {
143-
random := rand.IntN(3)
155+
n, err := rand.Int(rand.Reader, big.NewInt(3))
156+
if err != nil {
157+
// Fallback to case 0 if crypto/rand fails
158+
addRandomDelay(1000)
159+
return nil
160+
}
161+
random := n.Int64()
144162

145163
switch random {
146164
case 0:
147165
addRandomDelay(1000)
148166
return nil
149167
case 1:
150-
randomValue := rand.IntN(10000)
151-
return addRandomValue(strconv.Itoa(randomValue))
168+
valueN, err := rand.Int(rand.Reader, big.NewInt(10000))
169+
if err != nil {
170+
return fmt.Errorf("failed to generate random value: %w", err)
171+
}
172+
randomValue := valueN.Int64()
173+
return addRandomValue(strconv.FormatInt(randomValue, 10))
152174
case 2:
153175
addRandomDelay(1000)
154-
randomValue := rand.IntN(10000)
155-
return addRandomValue(strconv.Itoa(randomValue))
176+
valueN, err := rand.Int(rand.Reader, big.NewInt(10000))
177+
if err != nil {
178+
return fmt.Errorf("failed to generate random value: %w", err)
179+
}
180+
randomValue := valueN.Int64()
181+
return addRandomValue(strconv.FormatInt(randomValue, 10))
156182
default:
157183
return fmt.Errorf("invalid random value: %d", random)
158184
}

0 commit comments

Comments
 (0)