-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepalive_test.go
More file actions
233 lines (219 loc) · 6.18 KB
/
Copy pathkeepalive_test.go
File metadata and controls
233 lines (219 loc) · 6.18 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
package warc
import (
"compress/gzip"
"context"
"io"
"net"
"sync/atomic"
"testing"
"time"
http "github.com/saveweb/fhttp"
"github.com/saveweb/fhttp/httptest"
)
func newConnectionCountingServer(handler http.Handler) (*httptest.Server, *atomic.Int64) {
connections := &atomic.Int64{}
server := httptest.NewUnstartedServer(handler)
server.Config.ConnState = func(_ net.Conn, state http.ConnState) {
if state == http.StateNew {
connections.Add(1)
}
}
server.Start()
return server, connections
}
func TestDecompressedBodyStillCompletesTransportCapture(t *testing.T) {
server, connections := newConnectionCountingServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Encoding", "gzip")
zw := gzip.NewWriter(w)
_, _ = io.WriteString(zw, "decoded payload")
_ = zw.Close()
}))
defer server.Close()
client, err := NewWARCWritingHTTPClient(HTTPClientSettings{
RotatorSettings: defaultRotatorSettings(t),
DecompressBody: true,
})
if err != nil {
t.Fatal(err)
}
for i := 0; i < 2; i++ {
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)
exchange, err := client.Start(req)
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(exchange.Response.Body)
if err != nil || string(body) != "decoded payload" {
t.Fatalf("body=%q err=%v", body, err)
}
if err := exchange.Response.Body.Close(); err != nil {
t.Fatal(err)
}
result, err := exchange.Commit(context.Background())
if err != nil {
t.Fatal(err)
}
if len(result.Attempts) != 1 || result.Attempts[0].Outcome != http.CaptureOutcomeComplete {
t.Fatalf("attempts = %#v", result.Attempts)
}
}
if got := connections.Load(); got != 1 {
t.Fatalf("connections = %d, want 1", got)
}
if err := client.Close(); err != nil {
t.Fatal(err)
}
}
func doCapturedRequest(t *testing.T, client *CustomHTTPClient, url string, closeEarly bool) {
t.Helper()
feedback := make(chan FeedbackEvent, 1)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatal(err)
}
req = req.WithContext(WithFeedbackChannel(req.Context(), feedback))
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if !closeEarly {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
t.Fatal(err)
}
}
if err := resp.Body.Close(); err != nil {
t.Fatal(err)
}
select {
case <-feedback:
case <-time.After(2 * time.Second):
t.Fatal("capture feedback timed out")
}
}
func TestHTTP1KeepAliveDefaultsToEnabled(t *testing.T) {
server, connections := newConnectionCountingServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "complete")
}))
defer server.Close()
client, err := NewWARCWritingHTTPClient(HTTPClientSettings{RotatorSettings: defaultRotatorSettings(t)})
if err != nil {
t.Fatal(err)
}
go func() {
for range client.ErrChan {
}
}()
doCapturedRequest(t, client, server.URL+"/one", false)
doCapturedRequest(t, client, server.URL+"/two", false)
if got := connections.Load(); got != 1 {
t.Fatalf("connections = %d, want 1 with default keepalive", got)
}
if err := client.Close(); err != nil {
t.Fatal(err)
}
}
func TestHTTP1DisableKeepAlives(t *testing.T) {
server, connections := newConnectionCountingServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "complete")
}))
defer server.Close()
client, err := NewWARCWritingHTTPClient(HTTPClientSettings{
RotatorSettings: defaultRotatorSettings(t),
DisableKeepAlives: true,
})
if err != nil {
t.Fatal(err)
}
go func() {
for range client.ErrChan {
}
}()
doCapturedRequest(t, client, server.URL+"/one", false)
doCapturedRequest(t, client, server.URL+"/two", false)
if got := connections.Load(); got != 2 {
t.Fatalf("connections = %d, want 2 with keepalive disabled", got)
}
if err := client.Close(); err != nil {
t.Fatal(err)
}
}
func TestHTTP2KeepAliveAndDisableFlag(t *testing.T) {
for _, tc := range []struct {
name string
disable bool
connections int64
}{
{name: "default reuse", connections: 1},
{name: "disabled", disable: true, connections: 2},
} {
t.Run(tc.name, func(t *testing.T) {
connections := &atomic.Int64{}
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "complete")
}))
server.EnableHTTP2 = true
server.Config.ConnState = func(_ net.Conn, state http.ConnState) {
if state == http.StateNew {
connections.Add(1)
}
}
server.StartTLS()
defer server.Close()
client, err := NewWARCWritingHTTPClient(HTTPClientSettings{
RotatorSettings: defaultRotatorSettings(t),
ForceProtocol: "h2",
InsecureSkipVerifyCerts: true,
DisableKeepAlives: tc.disable,
})
if err != nil {
t.Fatal(err)
}
for i := 0; i < 2; i++ {
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)
exchange, err := client.Start(req)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(io.Discard, exchange.Response.Body); err != nil {
t.Fatal(err)
}
_ = exchange.Response.Body.Close()
if _, err := exchange.Commit(context.Background()); err != nil {
t.Fatal(err)
}
}
if got := connections.Load(); got != tc.connections {
t.Fatalf("connections = %d, want %d", got, tc.connections)
}
if err := client.Close(); err != nil {
t.Fatal(err)
}
})
}
}
func TestHTTP1EarlyCloseDrainsAndReusesConnection(t *testing.T) {
server, connections := newConnectionCountingServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "small response body")
}))
defer server.Close()
client, err := NewWARCWritingHTTPClient(HTTPClientSettings{
RotatorSettings: defaultRotatorSettings(t),
EarlyCloseDrainLimit: 1024,
EarlyCloseDrainTimeout: time.Second,
})
if err != nil {
t.Fatal(err)
}
go func() {
for range client.ErrChan {
}
}()
doCapturedRequest(t, client, server.URL+"/early", true)
doCapturedRequest(t, client, server.URL+"/next", false)
if got := connections.Load(); got != 1 {
t.Fatalf("connections = %d, want 1 after bounded early-close drain", got)
}
if err := client.Close(); err != nil {
t.Fatal(err)
}
}