-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathbackendtimeout_test.go
211 lines (174 loc) · 4.9 KB
/
backendtimeout_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
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
package proxy
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestSlowService(t *testing.T) {
wait := make(chan struct{})
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-wait
}))
defer func() {
close(wait)
service.Close()
}()
doc := fmt.Sprintf(`* -> backendTimeout("100ms") -> "%s"`, service.URL)
tp, err := newTestProxy(doc, FlagsNone)
if err != nil {
t.Fatal(err)
}
defer tp.close()
ps := httptest.NewServer(tp.proxy)
defer ps.Close()
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusGatewayTimeout {
t.Errorf("expected 504, got: %v", rsp)
}
}
func TestFastService(t *testing.T) {
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Millisecond)
}))
defer service.Close()
doc := fmt.Sprintf(`* -> backendTimeout("100ms") -> "%s"`, service.URL)
tp, err := newTestProxy(doc, FlagsNone)
if err != nil {
t.Fatal(err)
}
defer tp.close()
ps := httptest.NewServer(tp.proxy)
defer ps.Close()
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
t.Errorf("expected 200, got: %v", rsp)
}
}
func TestBackendTimeoutInTheMiddleOfServiceResponse(t *testing.T) {
testLog := NewTestLog()
defer testLog.Close()
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("Wish You"))
f := w.(http.Flusher)
f.Flush()
time.Sleep(200 * time.Millisecond)
w.Write([]byte(" Were Here"))
}))
defer service.Close()
doc := fmt.Sprintf(`* -> backendTimeout("100ms") -> "%s"`, service.URL)
tp, err := newTestProxy(doc, FlagsNone)
if err != nil {
t.Fatal(err)
}
defer tp.close()
ps := httptest.NewServer(tp.proxy)
defer ps.Close()
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
t.Errorf("expected 200, got: %v", rsp)
}
body, err := io.ReadAll(rsp.Body)
if err != nil {
t.Error(err)
}
content := string(body)
if content != "Wish You" {
t.Errorf("expected partial content, got %s", content)
}
const msg = "error while copying the response stream: context deadline exceeded"
if err = testLog.WaitFor(msg, 100*time.Millisecond); err != nil {
t.Errorf("expected '%s' in logs", msg)
}
}
type unstableRoundTripper struct {
inner http.RoundTripper
timeout time.Duration
attempt int
}
// Simulates dial timeout on every odd request
func (r *unstableRoundTripper) RoundTrip(req *http.Request) (rsp *http.Response, err error) {
if r.attempt%2 == 0 {
time.Sleep(r.timeout)
rsp, err = nil, &proxyError{
code: -1, // omit 0 handling in proxy.Error()
dialingFailed: true, // indicate error happened before http
}
} else {
rsp, err = r.inner.RoundTrip(req)
}
r.attempt = r.attempt + 1
return
}
func newUnstable(timeout time.Duration) func(r http.RoundTripper) http.RoundTripper {
return func(r http.RoundTripper) http.RoundTripper {
return &unstableRoundTripper{inner: r, timeout: timeout}
}
}
// Retryable request, dial timeout on first attempt, load balanced backend
// dial timeout (100ms) + service latency (100ms) > backendTimeout("150ms") => Gateway Timeout
func TestRetryAndSlowService(t *testing.T) {
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
}))
defer service.Close()
doc := fmt.Sprintf(`* -> backendTimeout("150ms") -> <"%s", "%s">`, service.URL, service.URL)
tp, err := newTestProxyWithParams(doc, Params{
CustomHttpRoundTripperWrap: newUnstable(100 * time.Millisecond),
})
if err != nil {
t.Fatal(err)
}
defer tp.close()
ps := httptest.NewServer(tp.proxy)
defer ps.Close()
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusGatewayTimeout {
t.Errorf("expected 504, got: %v", rsp)
}
}
// Retryable request, dial timeout on first attempt, load balanced backend
// dial timeout (100ms) + service latency (100ms) < backendTimeout("250ms") => OK
func TestRetryAndFastService(t *testing.T) {
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
}))
defer service.Close()
doc := fmt.Sprintf(`* -> backendTimeout("250ms") -> <"%s", "%s">`, service.URL, service.URL)
tp, err := newTestProxyWithParams(doc, Params{
CustomHttpRoundTripperWrap: newUnstable(100 * time.Millisecond),
})
if err != nil {
t.Fatal(err)
}
defer tp.close()
ps := httptest.NewServer(tp.proxy)
defer ps.Close()
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatal(err)
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
t.Errorf("expected 200, got: %v", rsp)
}
}