mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 20:17:41 +00:00
Ensure timeout test handlers don't complete before timing out.
TestTimeoutRequestHeaders and TestTimeoutWithLogging are designed to catch data races on request headers and include an HTTP handler that triggers timeout then repeatedly mutates request headers. Sometimes, the request header mutation loop could complete before the timeout filter observed the timeout, resulting in a test failure. The mutation loop now runs until the test ends.
This commit is contained in:
parent
aaaf70c8ad
commit
e5a15c87e9
@ -272,6 +272,8 @@ func TestTimeoutRequestHeaders(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
testDone := make(chan struct{})
|
||||
defer close(testDone)
|
||||
ts := httptest.NewServer(
|
||||
withDeadline(
|
||||
WithTimeoutForNonLongRunningRequests(
|
||||
@ -280,9 +282,14 @@ func TestTimeoutRequestHeaders(t *testing.T) {
|
||||
cancel()
|
||||
// mutate request Headers
|
||||
// Authorization filter does it for example
|
||||
for j := 0; j < 10000; j++ {
|
||||
for {
|
||||
select {
|
||||
case <-testDone:
|
||||
return
|
||||
default:
|
||||
req.Header.Set("Test", "post")
|
||||
}
|
||||
}
|
||||
}),
|
||||
func(r *http.Request, requestInfo *request.RequestInfo) bool {
|
||||
return false
|
||||
@ -301,8 +308,8 @@ func TestTimeoutRequestHeaders(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.StatusCode != http.StatusGatewayTimeout {
|
||||
t.Errorf("got res.StatusCde %d; expected %d", res.StatusCode, http.StatusServiceUnavailable)
|
||||
if actual, expected := res.StatusCode, http.StatusGatewayTimeout; actual != expected {
|
||||
t.Errorf("got status code %d; expected %d", actual, expected)
|
||||
}
|
||||
res.Body.Close()
|
||||
}
|
||||
@ -323,6 +330,8 @@ func TestTimeoutWithLogging(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
testDone := make(chan struct{})
|
||||
defer close(testDone)
|
||||
ts := httptest.NewServer(
|
||||
WithHTTPLogging(
|
||||
withDeadline(
|
||||
@ -332,9 +341,14 @@ func TestTimeoutWithLogging(t *testing.T) {
|
||||
cancel()
|
||||
// mutate request Headers
|
||||
// Authorization filter does it for example
|
||||
for j := 0; j < 10000; j++ {
|
||||
for {
|
||||
select {
|
||||
case <-testDone:
|
||||
return
|
||||
default:
|
||||
req.Header.Set("Test", "post")
|
||||
}
|
||||
}
|
||||
}),
|
||||
func(r *http.Request, requestInfo *request.RequestInfo) bool {
|
||||
return false
|
||||
@ -354,8 +368,8 @@ func TestTimeoutWithLogging(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.StatusCode != http.StatusGatewayTimeout {
|
||||
t.Errorf("got res.StatusCode %d; expected %d", res.StatusCode, http.StatusServiceUnavailable)
|
||||
if actual, expected := res.StatusCode, http.StatusGatewayTimeout; actual != expected {
|
||||
t.Errorf("got status code %d; expected %d", actual, expected)
|
||||
}
|
||||
res.Body.Close()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user