fix(api): disable non-positive timeouts
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
65ae0fca6d
commit
9aa7c644ef
2 changed files with 14 additions and 8 deletions
|
|
@ -216,6 +216,9 @@ func WithSlog(logger *slog.Logger) Option {
|
|||
// runs without a deadline) — this is safe and will not panic.
|
||||
func WithTimeout(d time.Duration) Option {
|
||||
return func(e *Engine) {
|
||||
if d <= 0 {
|
||||
return
|
||||
}
|
||||
e.middlewares = append(e.middlewares, timeout.New(
|
||||
timeout.WithTimeout(d),
|
||||
timeout.WithResponse(timeoutResponse),
|
||||
|
|
|
|||
|
|
@ -148,15 +148,8 @@ func TestWithTimeout_Good_CombinesWithOtherMiddleware(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestWithTimeout_Ugly_ZeroDurationDoesNotPanic(t *testing.T) {
|
||||
skipIfRaceDetector(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("WithTimeout(0) panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
e, err := api.New(api.WithTimeout(0))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
|
|
@ -168,5 +161,15 @@ func TestWithTimeout_Ugly_ZeroDurationDoesNotPanic(t *testing.T) {
|
|||
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
// We only care that it did not panic. Status may vary with zero timeout.
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200 with zero timeout disabled, got %d", w.Code)
|
||||
}
|
||||
|
||||
var resp api.Response[string]
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal error: %v", err)
|
||||
}
|
||||
if resp.Data != "pong" {
|
||||
t.Fatalf("expected Data=%q, got %q", "pong", resp.Data)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue