Repository navigation
Expand file tree
/
Copy pathmaxstreams_test.go
More file actions
161 lines (151 loc) · 4.25 KB
/
Copy pathmaxstreams_test.go
File metadata and controls
161 lines (151 loc) · 4.25 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
package rhttp
import (
"io"
"net/http"
"strings"
"testing"
"time"
)
// TestProxyRespectsWorkerMaxConcurrentStreams: the proxy must never open more
// concurrent streams on a tunnel than the worker advertised in
// SETTINGS_MAX_CONCURRENT_STREAMS — strict peers (HAProxy) refuse the excess
// stream and count a glitch against the connection. With a single saturated
// tunnel the request is refused with 503 (backpressure), not forced through.
func TestProxyRespectsWorkerMaxConcurrentStreams(t *testing.T) {
release := make(chan struct{})
started := make(chan struct{}, 4)
f := newProxyFixture(t, proxyFixtureOpts{
maxStreams: 1,
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
started <- struct{}{}
select {
case <-release:
case <-r.Context().Done():
return
}
_, _ = io.WriteString(w, "ok")
}),
})
defer f.Close()
defer close(release)
type result struct {
resp *http.Response
body string
err error
}
res1 := make(chan result, 1)
go func() {
resp, err := http.Get(f.public.URL)
var body string
if err == nil {
b, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
body = string(b)
}
res1 <- result{resp, body, err}
}()
select {
case <-started:
case <-time.After(3 * time.Second):
t.Fatal("first request never reached the worker")
}
// Tunnel is now at its advertised limit (1). The next request must be
// refused with 503 rather than opening a second stream.
resp2, err := http.Get(f.public.URL)
if err != nil {
t.Fatalf("second GET: %v", err)
}
b2, _ := io.ReadAll(resp2.Body)
_ = resp2.Body.Close()
if resp2.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("over-limit request: status %d (body %q), want 503", resp2.StatusCode, b2)
}
if !strings.Contains(string(b2), "concurrent-stream limit") {
t.Fatalf("over-limit response body %q lacks the capacity error", b2)
}
// Release the first request; it must complete untouched.
release <- struct{}{}
select {
case r := <-res1:
if r.err != nil || r.resp.StatusCode != 200 || r.body != "ok" {
t.Fatalf("first request: err=%v status=%v body=%q, want 200 %q",
r.err, r.resp, r.body, "ok")
}
case <-time.After(3 * time.Second):
t.Fatal("first request did not complete after release")
}
}
// TestLeastInFlightSkipsSaturatedTunnels: with two tunnels of capacity 1, a
// request arriving while the first tunnel is saturated must land on the
// second instead of being refused.
func TestLeastInFlightSkipsSaturatedTunnels(t *testing.T) {
release := make(chan struct{})
started := make(chan struct{}, 4)
f := newProxyFixture(t, proxyFixtureOpts{
nbConn: 2,
maxStreams: 1,
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
started <- struct{}{}
select {
case <-release:
case <-r.Context().Done():
return
}
_, _ = io.WriteString(w, "ok")
}),
})
defer f.Close()
defer close(release)
type result struct {
status int
body string
err error
}
res1 := make(chan result, 1)
go func() {
resp, err := http.Get(f.public.URL)
if err != nil {
res1 <- result{err: err}
return
}
b, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
res1 <- result{resp.StatusCode, string(b), nil}
}()
select {
case <-started:
case <-time.After(3 * time.Second):
t.Fatal("first request never reached a worker")
}
// First tunnel is saturated; the second request must be routed to the
// other tunnel and complete while the first is still blocked.
res2 := make(chan result, 1)
go func() {
resp, err := http.Get(f.public.URL)
if err != nil {
res2 <- result{err: err}
return
}
b, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
res2 <- result{resp.StatusCode, string(b), nil}
}()
select {
case <-started:
case <-time.After(3 * time.Second):
t.Fatal("second request was not routed to the free tunnel")
}
// Both handlers are now in flight; release them and both must succeed.
release <- struct{}{}
release <- struct{}{}
for i, ch := range []chan result{res1, res2} {
select {
case r := <-ch:
if r.err != nil || r.status != 200 || r.body != "ok" {
t.Fatalf("request %d: err=%v status=%d body=%q, want 200 %q", i+1, r.err, r.status, r.body, "ok")
}
case <-time.After(3 * time.Second):
t.Fatalf("request %d did not complete", i+1)
}
}
}