Repository navigation
Expand file tree
/
Copy pathctx_reclaim_test.go
More file actions
356 lines (312 loc) · 11.7 KB
/
Copy pathctx_reclaim_test.go
File metadata and controls
356 lines (312 loc) · 11.7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 GitHub Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"bytes"
"io"
"mime/multipart"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
)
// acquireReclaimTestCtx wires up a *DefaultCtx for reclaim tests without going
// through a full request lifecycle, so each test can drive ScheduleReclaim,
// signalReleased, and ReleaseCtx in isolation. The tests release the context
// themselves: when and whether it is released is what they exercise.
func acquireReclaimTestCtx(t *testing.T) (*App, *DefaultCtx) {
t.Helper()
app := New()
raw := app.AcquireCtx(&fasthttp.RequestCtx{})
dc, ok := raw.(*DefaultCtx)
require.True(t, ok, "AcquireCtx must return *DefaultCtx in tests")
return app, dc
}
// ctxExpectReclaimed requires that c is returned to the pool, observable as
// IsAbandoned flipping back to false, and that msg holds when it is.
func ctxExpectReclaimed(t *testing.T, c *DefaultCtx, msg string) {
t.Helper()
require.Eventually(t, func() bool { return !c.IsAbandoned() }, time.Second, 5*time.Millisecond, msg)
}
// TestDefaultCtx_ScheduleReclaim_HappyPath covers the dominant timed-out flow:
// after ScheduleReclaim is armed and both signals fire (handlerDone closes and
// the request handler releases the context), the context is returned to the
// pool, observable as IsAbandoned flipping back to false.
func TestDefaultCtx_ScheduleReclaim_HappyPath(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
handlerDone := make(chan struct{})
c.ScheduleReclaim(handlerDone, nil)
require.True(t, c.IsAbandoned(), "ScheduleReclaim must Abandon the ctx internally")
close(handlerDone)
require.True(t, c.IsAbandoned(), "ctx must stay abandoned until ReleaseCtx fires")
app.ReleaseCtx(c)
ctxExpectReclaimed(t, c, "ctx must be reclaimed once both signals fire")
}
// An application that releases its timed-out contexts itself must not pool one a second time.
func TestDefaultCtx_ForceRelease_LeavesReclaimedCtx(t *testing.T) {
t.Parallel()
t.Run("reclaim armed", func(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
handlerDone := make(chan struct{})
c.ScheduleReclaim(handlerDone, nil)
c.ForceRelease()
require.True(t, c.IsAbandoned())
require.NotNil(t, c.fasthttp)
close(handlerDone)
app.ReleaseCtx(c)
ctxExpectReclaimed(t, c, "the reclaim must still pool the ctx")
})
t.Run("not abandoned", func(t *testing.T) {
t.Parallel()
_, c := acquireReclaimTestCtx(t)
c.route = &Route{}
c.ForceRelease()
require.NotNil(t, c.route)
require.NotNil(t, c.fasthttp)
})
}
// A ForceRelease that runs the moment ScheduleReclaim abandons the ctx must see the reclaim it arms.
func TestDefaultCtx_ForceRelease_DuringScheduleReclaim(t *testing.T) {
t.Parallel()
app := New()
for range 2000 {
c, ok := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)
require.True(t, ok)
handlerDone, armed := make(chan struct{}), make(chan struct{})
go func() {
defer close(armed)
c.ScheduleReclaim(handlerDone, nil)
}()
for !c.IsAbandoned() {
runtime.Gosched()
}
c.ForceRelease()
<-armed
require.NotNil(t, c.fasthttp, "ForceRelease pooled a ctx the reclaim pools as well")
close(handlerDone)
app.ReleaseCtx(c)
}
}
// TestDefaultCtx_ScheduleReclaim_ReleaseBeforeHandlerDone covers the reverse
// ordering: the context is released first, then handlerDone closes. The reclaim
// goroutine must still wait on handlerDone before pooling. Both release entry
// points fire the latch: ReleaseCtx, the public CustomCtx path, and the internal
// releaseDefaultCtx, which defaultRequestHandler's defer calls.
func TestDefaultCtx_ScheduleReclaim_ReleaseBeforeHandlerDone(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
release func(app *App, c *DefaultCtx)
name string
}{
{name: "ReleaseCtx", release: func(app *App, c *DefaultCtx) { app.ReleaseCtx(c) }},
{name: "releaseDefaultCtx", release: (*App).releaseDefaultCtx},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
handlerDone := make(chan struct{})
c.ScheduleReclaim(handlerDone, nil)
tc.release(app, c)
require.True(t, c.IsAbandoned(), "an abandoned ctx must not be pooled until handlerDone closes")
close(handlerDone)
ctxExpectReclaimed(t, c, "the release must wire signalReleased into the latch")
})
}
}
// TestDefaultCtx_ScheduleReclaim_Nested arms one ctx twice, as nested timeout
// middlewares do, in every order relative to the release.
func TestDefaultCtx_ScheduleReclaim_Nested(t *testing.T) {
t.Parallel()
for _, order := range [][]string{
{"outer", "inner", "release"},
{"inner", "outer", "release"},
{"outer", "release", "inner"},
} {
t.Run(strings.Join(order, "_"), func(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
var calls atomic.Int32
cancel := func() { calls.Add(1) }
outerDone, innerDone := make(chan struct{}), make(chan struct{})
steps := map[string]func(){
"outer": func() { c.ScheduleReclaim(outerDone, cancel) },
"inner": func() { c.ScheduleReclaim(innerDone, cancel) },
"release": func() { app.ReleaseCtx(c) },
}
for _, step := range order {
steps[step]()
}
close(outerDone)
require.Never(t, func() bool {
return !c.IsAbandoned()
}, 50*time.Millisecond, 5*time.Millisecond, "ctx must not be reclaimed while the inner handler is still running")
close(innerDone)
require.Eventually(t, func() bool {
return !c.IsAbandoned() && calls.Load() == 2
}, time.Second, 5*time.Millisecond, "ctx must be reclaimed once both goroutines have finished")
})
}
}
// TestDefaultCtx_ScheduleReclaim_Concurrent arms the ctx from the request
// goroutine and from the goroutine it abandons at the same time.
func TestDefaultCtx_ScheduleReclaim_Concurrent(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
outerDone, innerDone := make(chan struct{}), make(chan struct{})
go func() {
defer close(outerDone)
c.ScheduleReclaim(innerDone, nil)
}()
c.ScheduleReclaim(outerDone, nil)
app.ReleaseCtx(c)
<-outerDone
require.Never(t, func() bool {
return !c.IsAbandoned()
}, 50*time.Millisecond, 5*time.Millisecond, "ctx must not be reclaimed while the inner handler is still running")
close(innerDone)
ctxExpectReclaimed(t, c, "ctx must be reclaimed once both goroutines have finished")
}
// TestDefaultCtx_ScheduleReclaim_Cancel verifies the cancel hook fires exactly
// once when the handler goroutine finishes, and that a nil cancel does not block
// reclamation.
func TestDefaultCtx_ScheduleReclaim_Cancel(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
nilCancel bool
}{
{name: "cancel invoked"},
{name: "nil cancel", nilCancel: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
var calls atomic.Int32
cancel := func() { calls.Add(1) }
if tc.nilCancel {
cancel = nil
}
handlerDone := make(chan struct{})
c.ScheduleReclaim(handlerDone, cancel)
close(handlerDone)
app.ReleaseCtx(c)
require.Eventually(t, func() bool {
return (tc.nilCancel || calls.Load() == 1) && !c.IsAbandoned()
}, time.Second, 5*time.Millisecond, "cancel must fire once, if there is one, and ctx must be reclaimed")
})
}
}
// TestDefaultCtx_signalReleased_NoReclaim guards that calling signalReleased on
// a context that was never armed for reclamation is a safe no-op. This is the
// SSE-style path (Abandon without ScheduleReclaim) hit through ReleaseCtx.
func TestDefaultCtx_signalReleased_NoReclaim(t *testing.T) {
t.Parallel()
_, c := acquireReclaimTestCtx(t)
require.NotPanics(t, func() { c.signalReleased() })
require.NotPanics(t, func() { c.signalReleased() })
}
// TestDefaultCtx_signalReleased_Idempotent guards the sync.Once semantics: even
// if the request release path fires multiple times, the latch must close once.
func TestDefaultCtx_signalReleased_Idempotent(t *testing.T) {
t.Parallel()
_, c := acquireReclaimTestCtx(t)
handlerDone := make(chan struct{})
c.ScheduleReclaim(handlerDone, nil)
require.NotPanics(t, func() {
c.signalReleased()
c.signalReleased()
c.signalReleased()
})
close(handlerDone)
ctxExpectReclaimed(t, c, "ctx must still be reclaimed exactly once")
}
// TestApp_releaseDefaultCtx_NotAbandonedPools verifies the non-abandoned branch
// in releaseDefaultCtx still pools the ctx through the normal path.
func TestApp_releaseDefaultCtx_NotAbandonedPools(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
require.False(t, c.IsAbandoned())
require.NotPanics(t, func() { app.releaseDefaultCtx(c) })
}
type reclaimCloser struct {
io.Reader
closed *atomic.Int32
onClose func()
}
func (r reclaimCloser) Close() error {
if r.onClose != nil {
r.onClose()
}
r.closed.Add(1)
return nil
}
// fasthttp never resets a RequestCtx it answered with a timeout response, so the reclaim closes what the request
// left open, and only once the handler is done. Any other RequestCtx is fasthttp's to reset, and one a fasthttp
// handler gave up on as well is that handler's, which still runs on it.
func TestDefaultCtx_ScheduleReclaim_ClosesTimedOutRequest(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct{ timedOut, handlerGaveUp bool }{
"timed out": {timedOut: true},
"not timed out": {},
"handler gave up too": {timedOut: true, handlerGaveUp: true},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
app, c := acquireReclaimTestCtx(t)
var body bytes.Buffer
w := multipart.NewWriter(&body)
part, err := w.CreateFormFile("file", "upload.bin")
require.NoError(t, err)
_, err = part.Write(make([]byte, 64<<10))
require.NoError(t, err)
require.NoError(t, w.Close())
c.Request().Header.SetContentType(w.FormDataContentType())
c.Request().SetBodyStream(&body, body.Len()) // a streamed upload is kept in a temp file
form, err := c.MultipartForm()
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, form.RemoveAll()) })
upload := form.File["file"][0]
onDisk := func() bool {
f, err := upload.Open()
return err == nil && f.Close() == nil
}
require.True(t, onDisk())
var closed atomic.Int32
var cleared atomic.Bool
fctx := c.RequestCtx()
c.Locals("closer", reclaimCloser{closed: &closed})
// Closed after "closer", which by then must be gone from the request: its Close may have pooled it.
c.Locals("check", reclaimCloser{closed: &closed, onClose: func() { cleared.Store(fctx.UserValue("closer") == nil) }})
require.NoError(t, c.SendStream(reclaimCloser{Reader: strings.NewReader("body"), closed: &closed}))
if tc.timedOut {
c.RequestCtx().TimeoutErrorWithResponse(&fasthttp.Response{})
}
untouched := func() bool { return closed.Load() == 0 && onDisk() }
handlerDone := make(chan struct{})
c.ScheduleReclaim(handlerDone, nil)
if tc.handlerGaveUp {
c.RequestCtx().TimeoutError("busy")
}
app.ReleaseCtx(c)
require.Never(t, func() bool { return !untouched() }, 50*time.Millisecond, 5*time.Millisecond)
close(handlerDone)
if tc.handlerGaveUp {
require.Never(t, func() bool { return !untouched() || !c.IsAbandoned() }, 50*time.Millisecond, 5*time.Millisecond)
return
}
ctxExpectReclaimed(t, c, "ctx must be reclaimed")
if !tc.timedOut {
require.Never(t, func() bool { return !untouched() }, 50*time.Millisecond, 5*time.Millisecond)
return
}
require.Eventually(t, func() bool { return closed.Load() == 3 && !onDisk() }, time.Second, 5*time.Millisecond)
require.True(t, cleared.Load(), "a closed value was left in the request")
})
}
}