-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout_test.go
More file actions
175 lines (145 loc) · 4.74 KB
/
timeout_test.go
File metadata and controls
175 lines (145 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// skipIfRaceDetector skips the test when the race detector is enabled.
// gin-contrib/timeout@v1.1.0 has a known data race on Context.index
// between the timeout goroutine and the handler goroutine.
func skipIfRaceDetector(t *testing.T) {
t.Helper()
if raceDetectorEnabled {
t.Skip("skipping: gin-contrib/timeout has known data race (upstream bug)")
}
}
// ── Helpers ─────────────────────────────────────────────────────────────
// slowGroup provides a route that sleeps longer than the test timeout.
type slowGroup struct{}
func (s *slowGroup) Name() string { return "slow" }
func (s *slowGroup) BasePath() string { return "/v1/slow" }
func (s *slowGroup) RegisterRoutes(rg *gin.RouterGroup) {
rg.GET("/wait", func(c *gin.Context) {
time.Sleep(200 * time.Millisecond)
c.JSON(http.StatusOK, api.OK("done"))
})
}
// ── WithTimeout ─────────────────────────────────────────────────────────
func TestWithTimeout_Good_FastRequestSucceeds(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithTimeout(500 * time.Millisecond))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, 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.Success {
t.Fatal("expected Success=true")
}
if resp.Data != "pong" {
t.Fatalf("expected Data=%q, got %q", "pong", resp.Data)
}
}
func TestWithTimeout_Good_SlowRequestTimesOut(t *testing.T) {
skipIfRaceDetector(t)
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithTimeout(50 * time.Millisecond))
e.Register(&slowGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/v1/slow/wait", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusGatewayTimeout {
t.Fatalf("expected 504, got %d", w.Code)
}
}
func TestWithTimeout_Good_TimeoutResponseEnvelope(t *testing.T) {
skipIfRaceDetector(t)
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithTimeout(50 * time.Millisecond))
e.Register(&slowGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/v1/slow/wait", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusGatewayTimeout {
t.Fatalf("expected 504, got %d", w.Code)
}
var resp api.Response[any]
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp.Success {
t.Fatal("expected Success=false")
}
if resp.Error == nil {
t.Fatal("expected Error to be non-nil")
}
if resp.Error.Code != "timeout" {
t.Fatalf("expected error code=%q, got %q", "timeout", resp.Error.Code)
}
if resp.Error.Message != "Request timed out" {
t.Fatalf("expected error message=%q, got %q", "Request timed out", resp.Error.Message)
}
}
func TestWithTimeout_Good_CombinesWithOtherMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(
api.WithRequestID(),
api.WithTimeout(500*time.Millisecond),
)
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// WithRequestID should still set the header.
id := w.Header().Get("X-Request-ID")
if id == "" {
t.Fatal("expected X-Request-ID header to be set")
}
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)
}
}
func TestWithTimeout_Ugly_ZeroDurationDoesNotPanic(t *testing.T) {
gin.SetMode(gin.TestMode)
e, err := api.New(api.WithTimeout(0))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
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)
}
}