-
Notifications
You must be signed in to change notification settings - Fork 351
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
AlexanderYastrebov
merged 1 commit into
master
from
predicates/traffic/fix-segment-tests
Jun 23, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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) { | ||
|
@@ -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) | ||
} | ||
|
||
|
@@ -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]) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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