Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func New(opts ...Option) gin.HandlerFunc {
panic(p)

case <-finish:
c.Next()
tw.mu.Lock()
defer tw.mu.Unlock()
dst := tw.ResponseWriter.Header()
Expand Down
24 changes: 22 additions & 2 deletions timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func TestLargeResponse(t *testing.T) {
c.String(http.StatusRequestTimeout, `{"error": "timeout error"}`)
}),
), func(c *gin.Context) {
time.Sleep(999*time.Millisecond + 500*time.Microsecond) // wait almost same as timeout
c.String(http.StatusRequestTimeout, `{"error": "handler error"}`)
time.Sleep(2 * time.Second) // wait almost same as timeout
c.String(http.StatusBadRequest, `{"error": "handler error"}`)
})

wg := sync.WaitGroup{}
Expand All @@ -131,3 +131,23 @@ func TestLargeResponse(t *testing.T) {
}
wg.Wait()
}

// Test to ensure no further middleware is executed after timeout (covers c.Next() removal)
func TestNoNextAfterTimeout(t *testing.T) {
r := gin.New()
called := false
r.Use(New(WithTimeout(50*time.Millisecond), WithHandler(func(c *gin.Context) {
time.Sleep(100 * time.Millisecond)
c.String(http.StatusOK, "should not reach")
})))
r.Use(func(c *gin.Context) {
called = true
})

w := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
r.ServeHTTP(w, req)

assert.Equal(t, http.StatusRequestTimeout, w.Code)
assert.False(t, called, "next middleware should not be called after timeout")
}
Loading