Skip to content

Commit 392be7c

Browse files
authored
Improve distribution tests in conformance for MeshHTTPRouteWeight (#3855)
* added entropy to mesh weight test Signed-off-by: carsontham <[email protected]> * added nolint:gosec to skip linter issues for conformance test * move creation of header map to entropy func to avoid concurrent map writes * use crypto/rand instead of math/rand * handle error checking and add default in switch * return error in default for switch case --------- Signed-off-by: carsontham <[email protected]>
1 parent 7f29273 commit 392be7c

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

conformance/tests/mesh/httproute-weight.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ package meshtests
1818

1919
import (
2020
"cmp"
21+
"crypto/rand"
2122
"errors"
2223
"fmt"
2324
"math"
25+
"math/big"
2426
"slices"
2527
"strings"
2628
"sync"
2729
"testing"
30+
"time"
2831

2932
"golang.org/x/sync/errgroup"
3033

@@ -89,7 +92,11 @@ func testDistribution(t *testing.T, client echo.MeshPod, expected http.ExpectedR
8992
g.SetLimit(concurrentRequests)
9093
for i := 0.0; i < totalRequests; i++ {
9194
g.Go(func() error {
92-
_, cRes, err := client.CaptureRequestResponseAndCompare(t, expected)
95+
uniqueExpected := expected
96+
if err := addEntropy(&uniqueExpected); err != nil {
97+
return fmt.Errorf("error adding entropy: %w", err)
98+
}
99+
_, cRes, err := client.CaptureRequestResponseAndCompare(t, uniqueExpected)
93100
if err != nil {
94101
return fmt.Errorf("failed: %w", err)
95102
}
@@ -141,3 +148,54 @@ func testDistribution(t *testing.T, client echo.MeshPod, expected http.ExpectedR
141148
})
142149
return errors.Join(errs...)
143150
}
151+
152+
// addEntropy adds jitter to the request by adding either a delay up to 1 second, or a random header value, or both.
153+
func addEntropy(exp *http.ExpectedResponse) error {
154+
randomNumber := func(limit int64) (*int64, error) {
155+
number, err := rand.Int(rand.Reader, big.NewInt(limit))
156+
if err != nil {
157+
return nil, err
158+
}
159+
n := number.Int64()
160+
return &n, nil
161+
}
162+
163+
// adds a delay
164+
delay := func(limit int64) error {
165+
randomSleepDuration, err := randomNumber(limit)
166+
if err != nil {
167+
return err
168+
}
169+
time.Sleep(time.Duration(*randomSleepDuration) * time.Millisecond)
170+
return nil
171+
}
172+
// adds random header value
173+
randomHeader := func(limit int64) error {
174+
randomHeaderValue, err := randomNumber(limit)
175+
if err != nil {
176+
return err
177+
}
178+
exp.Request.Headers = make(map[string]string)
179+
exp.Request.Headers["X-Jitter"] = fmt.Sprintf("%d", *randomHeaderValue)
180+
return nil
181+
}
182+
183+
random, err := randomNumber(3)
184+
if err != nil {
185+
return err
186+
}
187+
188+
switch *random {
189+
case 0:
190+
return delay(1000)
191+
case 1:
192+
return randomHeader(10000)
193+
case 2:
194+
if err := delay(1000); err != nil {
195+
return err
196+
}
197+
return randomHeader(10000)
198+
default:
199+
return fmt.Errorf("invalid random value: %d", *random)
200+
}
201+
}

0 commit comments

Comments
 (0)