Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predicates/traffic: fix flaky TrafficSegment tests #2423

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions predicates/traffic/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,30 @@ func requestWithR(r float64) *http.Request {
return req
}

func doN(t *testing.T, n int, client *proxytest.TestClient, request func() *http.Request) map[int]int {
codes := make(map[int]int)
// doN performs a number of requests and returns the number of responses for each status code and
// a total number of requests performed.
// Results use float64 type to simplify fractional comparisons.
func doN(t *testing.T, client *proxytest.TestClient, request func() *http.Request) (map[int]float64, float64) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the N?
Maybe better to call it do ?

Copy link
Member Author

@AlexanderYastrebov AlexanderYastrebov Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N is hardcoded, see below.
The name highlights that it does multiple requests

const n = 10_000
codes := make(map[int]float64)
for i := 0; i < n; i++ {
rsp, err := client.Do(request())
require.NoError(t, err)
rsp.Body.Close()

codes[rsp.StatusCode]++
}
return codes
return codes, n
}

func getN(t *testing.T, n int, client *proxytest.TestClient, url string) map[int]int {
return doN(t, n, client, func() *http.Request {
// assertEqualWithTolerance verifies that actual value is within predefined delta of the expected value.
func assertEqualWithTolerance(t *testing.T, expected, actual float64) {
t.Helper()
assert.InDelta(t, expected, actual, 0.06*expected)
}

func getN(t *testing.T, client *proxytest.TestClient, url string) (map[int]float64, float64) {
return doN(t, client, func() *http.Request {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
return req
Expand Down Expand Up @@ -129,18 +139,13 @@ func TestTrafficSegmentSplit(t *testing.T) {
}.Create()
defer p.Close()

const (
N = 1_000
delta = 0.05 * N
)

codes := getN(t, N, p.Client(), p.URL+"/test")
codes, n := getN(t, p.Client(), p.URL+"/test")

t.Logf("Response codes: %v", codes)

assert.InDelta(t, N*0.5, codes[200], delta)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why moving to a function?

assert.InDelta(t, N*0.3, codes[201], delta)
assert.InDelta(t, N*0.2, codes[202], delta)
assertEqualWithTolerance(t, n*0.5, codes[200])
assertEqualWithTolerance(t, n*0.3, codes[201])
assertEqualWithTolerance(t, n*0.2, codes[202])
}

func TestTrafficSegmentRouteWeight(t *testing.T) {
Expand All @@ -159,17 +164,15 @@ func TestTrafficSegmentRouteWeight(t *testing.T) {
}.Create()
defer p.Close()

const N = 1_000
codes, n := getN(t, p.Client(), p.URL+"/test")
assert.Equal(t, n, codes[200])

codes := getN(t, N, p.Client(), p.URL+"/test")
assert.Equal(t, N, codes[200])

codes = doN(t, N, p.Client(), func() *http.Request {
codes, n = doN(t, p.Client(), func() *http.Request {
req, _ := http.NewRequest("GET", p.URL+"/test", nil)
req.Header.Set("X-Foo", "bar")
return req
})
assert.Equal(t, N, codes[201])
assert.Equal(t, n, codes[201])
}

func TestTrafficSegmentTeeLoopback(t *testing.T) {
Expand All @@ -196,22 +199,17 @@ func TestTrafficSegmentTeeLoopback(t *testing.T) {
}.Create()
defer p.Close()

const (
N = 1_000
delta = 0.05 * N
)

codes := getN(t, N, p.Client(), p.URL+"/test")
codes, n := getN(t, p.Client(), p.URL+"/test")

// wait for loopback requests to complete
time.Sleep(100 * time.Millisecond)

loopRequests := int(atomic.LoadInt32(loopRequestsPtr))
loopRequests := float64(atomic.LoadInt32(loopRequestsPtr))

t.Logf("Response codes: %v, loopRequests: %d", codes, loopRequests)
t.Logf("Response codes: %v, loopRequests: %f", codes, loopRequests)

assert.InDelta(t, N*0.5, codes[200], delta)
assert.InDelta(t, N*0.5, codes[201], delta)
assertEqualWithTolerance(t, n*0.5, codes[200])
assertEqualWithTolerance(t, n*0.5, codes[201])
assert.Equal(t, codes[201], loopRequests)
}

Expand All @@ -233,15 +231,10 @@ func TestTrafficSegmentLoopbackBackend(t *testing.T) {
}.Create()
defer p.Close()

const (
N = 1_000
delta = 0.05 * N
)

codes := getN(t, N, p.Client(), p.URL+"/test")
codes, n := getN(t, p.Client(), p.URL+"/test")

t.Logf("Response codes: %v", codes)

assert.InDelta(t, N*0.5, codes[200], delta)
assert.InDelta(t, N*0.5, codes[201], delta)
assertEqualWithTolerance(t, n*0.5, codes[200])
assertEqualWithTolerance(t, n*0.5, codes[201])
}