Repository navigation
Expand file tree
/
Copy patherrors_http2_live_test.go
More file actions
144 lines (127 loc) · 3.95 KB
/
Copy patherrors_http2_live_test.go
File metadata and controls
144 lines (127 loc) · 3.95 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
package fantasy
import (
"context"
"errors"
"io"
"net"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
// TestLostPingIsClassifiedLive pins http2ConnectionLostMessages to the
// message Go actually produces. The entry is matched as a string, so a
// reword in a future release would silently stop dead connections being
// retryable, and only this test would notice.
func TestLostPingIsClassifiedLive(t *testing.T) {
t.Parallel()
// Streams a chunk so the response is live, then holds the stream open
// so the freeze lands mid-stream, where a real one would.
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("data: hello\n\n"))
w.(http.Flusher).Flush()
<-r.Context().Done()
}))
srv.EnableHTTP2 = true
srv.StartTLS()
defer srv.Close()
conn := &freezableConn{closed: make(chan struct{}), frozen: make(chan struct{})}
defer conn.Close()
tr := &http.Transport{
// A Transport carrying a TLSClientConfig does not negotiate h2 on
// its own, and the ping is h2-only, so without this the test would
// pass over HTTP/1.1 having proved nothing.
ForceAttemptHTTP2: true,
TLSClientConfig: srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone(),
HTTP2: &http.HTTP2Config{
SendPingTimeout: 200 * time.Millisecond,
PingTimeout: 200 * time.Millisecond,
},
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return conn.dial(ctx, network, addr)
},
}
defer tr.CloseIdleConnections()
resp, err := (&http.Client{Transport: tr}).Get(srv.URL)
if err != nil {
t.Fatalf("initial request: %v", err)
}
defer resp.Body.Close()
if resp.ProtoMajor != 2 {
t.Fatalf("got %s, want HTTP/2: the ping config only applies to h2", resp.Proto)
}
buf := make([]byte, 64)
if _, err := resp.Body.Read(buf); err != nil {
t.Fatalf("reading first chunk: %v", err)
}
conn.freeze()
read := make(chan error, 1)
go func() {
for {
if _, err := resp.Body.Read(buf); err != nil {
read <- err
return
}
}
}()
select {
case err := <-read:
if err == nil || errors.Is(err, io.EOF) {
t.Fatalf("expected a transport failure, got %v", err)
}
if !IsTransportError(err) {
t.Fatalf("IsTransportError(%q) = false, so a dead connection is no "+
"longer retryable. Go reworded it; update http2ConnectionLostMessages.", err)
}
case <-time.After(10 * time.Second):
t.Fatal("the read never returned: no health check noticed the dead peer")
}
}
// freezableConn is a client connection that goes deaf and mute on freeze
// while staying open, which is what a socket killed by a sleeping laptop
// looks like: the peer is gone, but no FIN or RST ever says so, and only
// the transport's health-check ping can tell that from a slow reply.
type freezableConn struct {
net.Conn
frozen chan struct{}
closed chan struct{}
once sync.Once
}
func (c *freezableConn) dial(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, network, addr)
c.Conn = conn
return c, err
}
func (c *freezableConn) freeze() { close(c.frozen) }
func (c *freezableConn) isFrozen() bool {
select {
case <-c.frozen:
return true
default:
return false
}
}
// Read parks until teardown once frozen, so no frame arrives to reset the
// transport's read-idle timer and the health check fires.
func (c *freezableConn) Read(p []byte) (int, error) {
if c.isFrozen() {
<-c.closed
return 0, io.EOF
}
return c.Conn.Read(p)
}
// Write reports success without sending once frozen, so the health-check
// ping never reaches the server and goes unanswered.
func (c *freezableConn) Write(p []byte) (int, error) {
if c.isFrozen() {
return len(p), nil
}
return c.Conn.Write(p)
}
// Close releases any parked Read and lets the server see EOF, which is
// what ends its handler and unblocks httptest.Server.Close.
func (c *freezableConn) Close() error {
c.once.Do(func() { close(c.closed) })
return c.Conn.Close()
}