forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_reuse_e2e_test.go
More file actions
114 lines (105 loc) · 3.04 KB
/
Copy pathframe_reuse_e2e_test.go
File metadata and controls
114 lines (105 loc) · 3.04 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
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http2_test
import (
"io"
"net/http"
"strings"
"sync"
"testing"
)
// TestFrameReuseEndToEndStress drives concurrent multiplexed requests
// through the real net/http HTTP/2 server and Transport (both of
// which call SetReuseFrames on the per-connection Framer) and touches
// every field reachable from a request/response in ways that would
// race against the read loop's next ReadFrame if anything still
// aliased the cached frame after the readMore gate (server) or
// read-loop iteration (Transport).
//
// Under -race this is a regression test against future refactors of
// processHeaders / handleResponse / processTrailers / processData
// that fail to copy aliased data. The synthetic Framer-pair tests in
// frame_reuse_race_test.go cannot reach the production process* code
// paths; this one does.
//
// The handler and client iterate Name/Value byte-by-byte so that any
// aliasing leak shows up as a concrete read concurrent with a write
// in bytes.(*Reader).Read inside ReadFrame.
func TestFrameReuseEndToEndStress(t *testing.T) {
if testing.Short() {
t.Skip("skipping stress test in short mode")
}
const responseBody = "the quick brown fox jumps over the lazy dog"
touchHeader := func(h http.Header) byte {
var sink byte
for k, vs := range h {
for i := 0; i < len(k); i++ {
sink ^= k[i]
}
for _, v := range vs {
for i := 0; i < len(v); i++ {
sink ^= v[i]
}
}
}
return sink
}
ts := newTestServer(t,
func(w http.ResponseWriter, r *http.Request) {
_ = touchHeader(r.Header)
if _, err := io.Copy(io.Discard, r.Body); err != nil {
t.Errorf("server body copy: %v", err)
return
}
_ = touchHeader(r.Trailer)
w.Header().Set("Trailer", "X-Server-Trailer")
w.Header().Set("X-Response-Header", "response-value")
w.WriteHeader(http.StatusOK)
io.WriteString(w, responseBody)
w.Header().Set("X-Server-Trailer", "server-trailer-value")
},
optQuiet,
)
tr := newTransport(t)
const (
workers = 8
iterations = 25
)
var wg sync.WaitGroup
for w := 0; w < workers; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
body := strings.NewReader("client request body content")
req, err := http.NewRequest("POST", ts.URL, body)
if err != nil {
t.Errorf("NewRequest: %v", err)
return
}
req.Header.Set("X-Test-Header", "client-header-value")
req.Trailer = http.Header{
"X-Client-Trailer": []string{"client-trailer-value"},
}
res, err := tr.RoundTrip(req)
if err != nil {
t.Errorf("RoundTrip: %v", err)
return
}
_ = touchHeader(res.Header)
if _, err := io.Copy(io.Discard, res.Body); err != nil {
res.Body.Close()
t.Errorf("client body copy: %v", err)
return
}
if err := res.Body.Close(); err != nil {
t.Errorf("body close: %v", err)
return
}
_ = touchHeader(res.Trailer)
}
}()
}
wg.Wait()
}