-
Notifications
You must be signed in to change notification settings - Fork 0
/
h2bug_test.go
92 lines (83 loc) · 1.64 KB
/
h2bug_test.go
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
package http
import (
"context"
"crypto/tls"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/http/httptrace"
"sync"
"testing"
"time"
"golang.org/x/net/http2"
)
var bad struct {
sync.Mutex
val bool
}
type connWrapper struct {
net.Conn
}
func (cw *connWrapper) Write(b []byte) (n int, err error) {
bad.Lock()
defer bad.Unlock()
if bad.val {
log.Printf("bad=false")
bad.val = false
return 0, errors.New("broken pipe")
}
return cw.Conn.Write(b)
}
var trace = &httptrace.ClientTrace{
GotConn: func(info httptrace.GotConnInfo) {
log.Printf("conn: %#v", info)
},
TLSHandshakeDone: func(cfg tls.ConnectionState, err error) {
bad.Lock()
defer bad.Unlock()
log.Printf("bad=true")
bad.val = true
},
}
func dial(network, addr string) (net.Conn, error) {
c, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
return &connWrapper{c}, nil
}
func TestBadCaching(t *testing.T) {
ts := &http.Transport{
ForceAttemptHTTP2: true,
Dial: dial,
DialContext: func(_ context.Context, network, addr string) (net.Conn, error) {
return dial(network, addr)
},
}
if err := http2.ConfigureTransport(ts); err != nil {
t.Fatal(err)
}
cli := &http.Client{
Transport: ts,
}
for {
func() {
req, err := http.NewRequest("GET", "https://container.googleapis.com", nil)
if err != nil {
log.Printf("err: %v\n", err)
return
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
resp, err := cli.Do(req)
if err != nil {
log.Printf("err: %v\n", err)
return
}
fmt.Printf("status: %v\n", resp.Status)
defer resp.Body.Close()
}()
time.Sleep(1 * time.Second)
}
}