-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: cancel the request context on timeout
- Loading branch information
Showing
5 changed files
with
100 additions
and
14 deletions.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
server: Added `ContextTimeoutMiddleware` to enforce request context timeout |
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
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//nolint:noctx,bodyclose | ||
package api | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimeoutMiddleware(t *testing.T) { | ||
withMiddleware := false | ||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Wait some time, so that the request timeout (10ms) should be reached. | ||
<-time.After(2 * time.Second) | ||
|
||
// Check if the request context is canceled. | ||
if withMiddleware { | ||
// If timeout middleware is used, the request context should be canceled. | ||
require.True(t, r.Context().Err() != nil, "request context should be canceled") | ||
} else { | ||
// The default behavior is that the request context is not canceled. | ||
require.False(t, r.Context().Err() != nil, "request context was not canceled") | ||
} | ||
}) | ||
backend := httptest.NewUnstartedServer(baseHandler) | ||
backend.Config.WriteTimeout = 10 * time.Millisecond | ||
backend.Start() | ||
defer backend.Close() | ||
|
||
// Test server without timeout middleware. | ||
req, err := http.NewRequest("GET", backend.URL, nil) | ||
require.NoError(t, err) | ||
_, err = http.DefaultClient.Do(req) | ||
require.ErrorIs(t, err, io.EOF, "client received EOF") | ||
backend.Close() | ||
|
||
// Test server with timeout middleware. | ||
withMiddleware = true | ||
backendWithTimeout := httptest.NewUnstartedServer(ContextTimeoutMiddleware(10 * time.Millisecond)(baseHandler)) | ||
backendWithTimeout.Config.WriteTimeout = 10 * time.Millisecond | ||
backendWithTimeout.Start() | ||
defer backendWithTimeout.Close() | ||
|
||
req, err = http.NewRequest("GET", backendWithTimeout.URL, nil) | ||
require.NoError(t, err) | ||
_, err = http.DefaultClient.Do(req) | ||
require.ErrorIs(t, err, io.EOF, "client received EOF") | ||
} |
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