-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures_test.go
More file actions
354 lines (320 loc) · 11 KB
/
Copy pathfeatures_test.go
File metadata and controls
354 lines (320 loc) · 11 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
package rhttp
import (
"bytes"
"context"
"io"
"log/slog"
"net/http"
"sync"
"testing"
"time"
"golang.org/x/net/http2"
)
// TestResponseWriterIsFlusher: a handler that casts to http.Flusher and calls
// Flush() should produce an immediate empty DATA frame on the wire, even
// before the handler writes the response body.
func TestResponseWriterIsFlusher(t *testing.T) {
flushed := make(chan struct{})
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
f, ok := w.(http.Flusher)
if !ok {
t.Error("ResponseWriter doesn't implement http.Flusher")
return
}
w.WriteHeader(200)
f.Flush()
<-flushed // hold the handler so the second body Write doesn't end the stream first
})
peer, _, cleanup := newTestPeer(t, ServerOptions{Handler: handler})
defer func() {
close(flushed) // release handler before tearing down
cleanup()
}()
peer.sendHeaders(1, true,
":method", "GET", ":scheme", "https", ":authority", "x", ":path", "/")
// We expect: HEADERS (no END_STREAM, since handler isn't done) then an
// empty DATA frame from Flush. Both must arrive before the handler returns.
sawHeaders := false
sawEmptyData := false
deadline := time.After(2 * time.Second)
for !sawHeaders || !sawEmptyData {
select {
case <-deadline:
t.Fatalf("did not see HEADERS+empty-DATA from Flush (headers=%v data=%v)", sawHeaders, sawEmptyData)
default:
}
f := peer.readFrameWithin(time.Second)
if f == nil {
t.Fatalf("no frame; sawHeaders=%v sawEmptyData=%v", sawHeaders, sawEmptyData)
}
switch ff := f.(type) {
case *http2.HeadersFrame:
if ff.StreamEnded() {
t.Fatal("HEADERS arrived with END_STREAM — handler shouldn't be done yet")
}
sawHeaders = true
case *http2.DataFrame:
if len(ff.Data()) != 0 {
t.Fatalf("expected empty DATA from Flush, got %d bytes", len(ff.Data()))
}
sawEmptyData = true
}
}
}
// TestRequestContextCancelledOnRSTStream: when the peer resets a stream, the
// handler's req.Context().Done() must fire so a handler that's parked on a
// downstream call (i.e. not in Body.Read) can bail out.
func TestRequestContextCancelledOnRSTStream(t *testing.T) {
ctxDone := make(chan error, 1)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.(http.Flusher).Flush() // get headers out so peer can RST after that
select {
case <-r.Context().Done():
ctxDone <- r.Context().Err()
case <-time.After(3 * time.Second):
ctxDone <- nil
}
})
peer, _, cleanup := newTestPeer(t, ServerOptions{Handler: handler})
defer cleanup()
peer.sendHeaders(1, false,
":method", "POST", ":scheme", "https", ":authority", "x", ":path", "/")
// Wait for response HEADERS to confirm handler is running, then reset.
for {
f := peer.readFrameWithin(2 * time.Second)
if f == nil {
t.Fatal("no HEADERS arrived before timeout")
}
if _, ok := f.(*http2.HeadersFrame); ok {
break
}
}
if err := peer.framer.WriteRSTStream(1, http2.ErrCodeCancel); err != nil {
t.Fatalf("write RST_STREAM: %v", err)
}
select {
case err := <-ctxDone:
if err == nil {
t.Fatal("handler timed out waiting on ctx; expected cancellation")
}
if err != context.Canceled {
t.Fatalf("ctx.Err() = %v, want context.Canceled", err)
}
case <-time.After(3 * time.Second):
t.Fatal("handler never observed ctx cancellation")
}
}
// TestRequestContextCancelledOnConnClose: when the peer drops the connection,
// every live handler must observe ctx cancellation — not hang on the bodyReader
// cond.
func TestRequestContextCancelledOnConnClose(t *testing.T) {
ctxDone := make(chan error, 1)
handlerEntered := make(chan struct{})
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.(http.Flusher).Flush()
close(handlerEntered)
select {
case <-r.Context().Done():
ctxDone <- r.Context().Err()
case <-time.After(3 * time.Second):
ctxDone <- nil
}
})
peer, _, cleanup := newTestPeer(t, ServerOptions{Handler: handler})
defer cleanup()
peer.sendHeaders(1, false,
":method", "POST", ":scheme", "https", ":authority", "x", ":path", "/")
select {
case <-handlerEntered:
case <-time.After(2 * time.Second):
t.Fatal("handler never started")
}
// Slam the peer conn shut — server's ReadFrame surfaces EOF, serve exits,
// failAllStreams fires, ctx cancels.
_ = peer.conn.Close()
select {
case err := <-ctxDone:
if err == nil {
t.Fatal("handler timed out; expected ctx cancellation from conn close")
}
case <-time.After(3 * time.Second):
t.Fatal("handler never observed ctx cancellation after conn close")
}
}
// TestHandlerPanicRecovered: a handler that panics must not crash the worker,
// and the connection must remain usable for subsequent streams.
func TestHandlerPanicRecovered(t *testing.T) {
// Capture the structured panic log via a buffered slog handler. Keeps
// test output clean and lets us assert the panic actually fired.
var logBuf bytes.Buffer
prevDefault := slog.Default()
slog.SetDefault(slog.New(slog.NewTextHandler(&logBuf, nil)))
defer slog.SetDefault(prevDefault)
var counter int
var mu sync.Mutex
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
mu.Lock()
counter++
n := counter
mu.Unlock()
if n == 1 {
panic("boom")
}
w.WriteHeader(204)
})
peer, _, cleanup := newTestPeer(t, ServerOptions{Handler: handler})
defer cleanup()
// First request: handler panics. Recovery emits 500.
peer.sendHeaders(1, true,
":method", "GET", ":scheme", "https", ":authority", "x", ":path", "/")
status, _, _ := peer.collectResponse(1)
if status != http.StatusInternalServerError {
t.Fatalf("first request: status = %d, want 500", status)
}
// Second request: same connection, handler should run normally.
peer.sendHeaders(3, true,
":method", "GET", ":scheme", "https", ":authority", "x", ":path", "/")
status, _, _ = peer.collectResponse(3)
if status != http.StatusNoContent {
t.Fatalf("second request: status = %d, want 204", status)
}
// slog.TextHandler emits key=value pairs; check for the message + the
// structured fields rather than the old fmt.Sprintf shape.
out := logBuf.String()
if !bytes.Contains(logBuf.Bytes(), []byte("panic serving stream")) {
t.Fatalf("expected panic log message; got %q", out)
}
if !bytes.Contains(logBuf.Bytes(), []byte("stream_id=1")) {
t.Fatalf("expected stream_id=1 in log; got %q", out)
}
if !bytes.Contains(logBuf.Bytes(), []byte("panic=boom")) {
t.Fatalf("expected panic=boom in log; got %q", out)
}
}
// TestGoAwaySentOnTeardown: when serve exits via the Shutdown path, the
// defer must put a GOAWAY frame on the wire BEFORE closing the conn so the
// peer can distinguish "we're closing cleanly, last-stream-id was N" from a
// raw TCP RST.
//
// We simulate Shutdown by wiring a shutdownCh into the connection and then
// poking ReadFrame's deadline. Closing the conn directly would race with
// the writer goroutine — the write would fail because the conn is already
// dead. The real Pool.Shutdown path keeps the conn open until the deferred
// GOAWAY makes it through.
func TestGoAwaySentOnTeardown(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
})
// shutdownCh wired in before serve starts — no race.
shutdownCh := make(chan struct{})
peer, serverConn, cleanup := newTestPeerShutdown(t, ServerOptions{Handler: handler}, shutdownCh)
defer cleanup()
// Send a complete request so lastStreamID advances to 1.
peer.sendHeaders(1, true,
":method", "GET", ":scheme", "https", ":authority", "x", ":path", "/")
if status, _, _ := peer.collectResponse(1); status != 200 {
t.Fatalf("status = %d, want 200", status)
}
// Trigger the same path Pool.Shutdown takes: close shutdownCh + nudge
// ReadFrame's deadline so serve returns through the GOAWAY-emitting defer.
close(shutdownCh)
_ = serverConn.SetReadDeadline(time.Now())
deadline := time.After(2 * time.Second)
for {
select {
case <-deadline:
t.Fatal("never saw GOAWAY")
default:
}
f := peer.readFrameWithin(2 * time.Second)
if f == nil {
t.Fatal("no frame within 2s; expected GOAWAY")
}
ga, ok := f.(*http2.GoAwayFrame)
if !ok {
continue
}
if ga.LastStreamID != 1 {
t.Fatalf("GOAWAY last-stream-id = %d, want 1", ga.LastStreamID)
}
if ga.ErrCode != http2.ErrCodeNo {
t.Fatalf("GOAWAY err = %v, want NO_ERROR", ga.ErrCode)
}
return
}
}
// TestPoolShutdownDrains: Pool.Shutdown signals shutdownCh, fires
// SetReadDeadline on every registered conn, and waits for maintainers to
// exit — all within the provided ctx.
func TestPoolShutdownDrains(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
})
// Pool's shutdownCh must be wired into the conn before serve starts —
// otherwise the close(pool.shutdownCh) in Shutdown wouldn't reach serve
// (and we'd race the field-write besides). Build the pool first.
pool := &Pool{
ready: &sync.WaitGroup{},
shutdownCh: make(chan struct{}),
conns: make(map[*connection]struct{}),
}
pool.ready.Add(1)
pool.ready.Done()
pool.maintainers.Go(func() {
<-pool.shutdownCh
})
peer, serverConn, cleanup := newTestPeerShutdown(t, ServerOptions{Handler: handler}, pool.shutdownCh)
defer cleanup()
pool.registerConn(serverConn)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
start := time.Now()
if err := pool.Shutdown(ctx); err != nil {
t.Fatalf("Shutdown returned %v", err)
}
if elapsed := time.Since(start); elapsed > 1500*time.Millisecond {
t.Fatalf("Shutdown took too long: %v", elapsed)
}
// After Shutdown, the conn's serve loop should exit (it sees shutdownCh
// closed via SetReadDeadline's wakeup); confirm by reading GOAWAY.
for {
f := peer.readFrameWithin(2 * time.Second)
if f == nil {
t.Fatal("no frame after Shutdown; expected GOAWAY")
}
if _, ok := f.(*http2.GoAwayFrame); ok {
return
}
}
}
// TestPoolShutdownContextDeadline: when a maintainer goroutine refuses to
// exit (simulating a stuck handler), Shutdown returns ctx.Err() at the
// deadline rather than hanging forever.
func TestPoolShutdownContextDeadline(t *testing.T) {
pool := &Pool{
ready: &sync.WaitGroup{},
shutdownCh: make(chan struct{}),
conns: make(map[*connection]struct{}),
}
// Maintainer that never exits.
pool.maintainers.Add(1)
defer pool.maintainers.Done() // released after the test asserts ctx.Err
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
start := time.Now()
err := pool.Shutdown(ctx)
if err != context.DeadlineExceeded {
t.Fatalf("Shutdown returned %v, want DeadlineExceeded", err)
}
if elapsed := time.Since(start); elapsed > 500*time.Millisecond {
t.Fatalf("Shutdown took too long: %v", elapsed)
}
}
// Compile-time assertion that responseWriter implements http.Flusher; if a
// future refactor accidentally drops the Flush method, the build breaks here
// rather than at the next handler that depends on it.
var _ http.Flusher = (*responseWriter)(nil)
// io import kept for any future tests that need it.
var _ = io.EOF