-
Notifications
You must be signed in to change notification settings - Fork 5
/
armbalancer_test.go
284 lines (266 loc) · 6.56 KB
/
armbalancer_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package armbalancer
import (
"fmt"
"math/rand"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"sync"
"testing"
)
func TestSoak(t *testing.T) {
limit := 20
reqCountByAddr := map[string]int{}
var lock sync.Mutex
var totalRequests int
svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lock.Lock()
defer lock.Unlock()
if r.Proto != "HTTP/2.0" {
t.Errorf("received request with proto: %s", r.Proto)
}
if r.Header.Get("Test") != "true" {
return // don't handle any requests from outside the test
}
if _, ok := reqCountByAddr[r.RemoteAddr]; !ok && rand.Intn(100) == 1 {
// randomly start new connections with zero quota to test min reqs per connection configuration
reqCountByAddr[r.RemoteAddr] = limit
} else {
reqCountByAddr[r.RemoteAddr]++
}
totalRequests++
w.Header().Set("X-Ms-Ratelimit-Remaining-Test", strconv.Itoa(limit-reqCountByAddr[r.RemoteAddr]))
w.Header().Set("X-Ms-Ratelimit-Remaining-Dummy", "10")
w.Header().Set("X-Ms-Ratelimit-Remaining-Invalid", "not-a-number")
}))
var closed int
svr.Config.ConnState = func(c net.Conn, cs http.ConnState) {
if cs == http.StateClosed {
closed++
}
}
svr.EnableHTTP2 = true
svr.StartTLS()
defer svr.Close()
u, _ := url.Parse(svr.URL)
client := &http.Client{Transport: New(Options{
Transport: svr.Client().Transport.(*http.Transport),
Host: u.Host,
PoolSize: 8,
RecycleThreshold: 5,
MinReqsBeforeRecycle: 6,
})}
var wg sync.WaitGroup
for i := 0; i < 12; i++ {
wg.Add(1)
go func() {
defer wg.Add(-1)
for j := 0; j < 500; j++ {
req, _ := http.NewRequest("GET", svr.URL, nil)
req.Header.Set("Test", "true")
resp, err := client.Do(req)
if err != nil {
t.Error(err)
continue
}
resp.Body.Close()
}
}()
}
wg.Wait()
_, err := client.Get("http://not-the-host")
if err == nil || err.Error() != fmt.Sprintf(`Get "http://not-the-host": host "not-the-host" is not supported by the configured ARM balancer, supported host name is %q`, u.Hostname()) {
t.Errorf("expected error when requesting host other than the one configured, got: %s", err)
}
if l := len(reqCountByAddr); l < 100 {
t.Errorf("pool couldn't be working correctly as only %d connections to the server were created", l)
}
if closed < len(reqCountByAddr)/4 {
t.Errorf("expected at least 25 percent of connections to be closed but only %d were closed", closed)
}
overLimit := []string{}
underMin := []string{}
for addr, count := range reqCountByAddr {
if count > limit {
overLimit = append(overLimit, addr)
}
if count < 6 {
underMin = append(underMin, addr)
}
}
// Since connection recycling is async, we can't expect 100% conformance to the configured limits
thres := len(reqCountByAddr) / 10
if l := len(overLimit); l > thres {
t.Errorf("%d clients exceeded the rate limit: %+s", l, overLimit)
}
if l := len(underMin); l > thres {
t.Errorf("%d clients undershot the configured min requests per connection: %+s", l, underMin)
}
}
type testCase struct {
name string
reqHost string
transHost string
transPort string
expected bool
}
func TestCompareHost(t *testing.T) {
cases := []testCase{
{
name: "matched since all without port number",
reqHost: "host.com",
transHost: "host.com",
transPort: "443",
expected: true,
},
{
name: "matched since all with port number",
reqHost: "host.com:443",
transHost: "host.com",
transPort: "443",
expected: true,
},
{
name: "matched with appending port name",
reqHost: "host.com:443",
transHost: "host.com",
transPort: "443",
expected: true,
},
{
name: "matched with removing port name",
reqHost: "host.com",
transHost: "host.com",
transPort: "443",
expected: true,
},
{
name: "not matched since different port number",
reqHost: "host.com:443",
transHost: "host.com",
transPort: "11254",
expected: false,
},
{
name: "not matched since differnt host name without port number",
reqHost: "host.com",
transHost: "abc.com",
expected: false,
},
{
name: "not matched since differnt host name with port number",
reqHost: "host.com:443",
transHost: "abc.com",
transPort: "443",
expected: false,
},
{
name: "not matched since differnt host name with port number for reqHost only",
reqHost: "host.com:443",
transHost: "abc.com",
transPort: "443",
expected: false,
},
{
name: "not matched since differnt host name with port number for transHost only",
reqHost: "host.com",
transHost: "abc.com",
transPort: "443",
expected: false,
},
}
for index, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := recyclableTransport{
host: c.transHost,
port: c.transPort,
}
v := r.compareHost(&url.URL{Host: c.reqHost})
if v != c.expected {
t.Errorf("expected %d result \"%t\" is not same as we get: %t", index, c.expected, v)
}
})
}
}
func TestNew(t *testing.T) {
type args struct {
opts Options
}
tests := []struct {
name string
args args
wantHost string
wantPort string
paniced bool
}{
{
name: "invalid host",
args: args{
opts: Options{
Host: "invalid:host:invalidport",
},
},
paniced: true,
},
{
name: "host is not assigned",
args: args{
opts: Options{
Host: ":445",
},
},
wantHost: "management.azure.com",
wantPort: "445",
paniced: false,
},
{
name: "port is not assigned",
args: args{
opts: Options{
Host: "management.azure.com",
},
},
wantHost: "management.azure.com",
wantPort: "443",
paniced: false,
},
{
name: "hosturl is not assigned",
args: args{
opts: Options{
Host: "",
},
},
wantHost: "management.azure.com",
wantPort: "443",
paniced: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.paniced {
defer func() {
if r := recover(); r != nil {
return
}
t.Errorf("New() did not panic")
}()
} else {
tt.args.opts.TransportFactory = func(id int, parent *http.Transport, host string, port string, recycleThreshold, minReqsBeforeRecycle int64) http.RoundTripper {
if host != tt.wantHost {
t.Errorf("New() host = %v, want %v", host, tt.wantHost)
}
if port != tt.wantPort {
t.Errorf("New() port = %v, want %v", port, tt.wantPort)
}
return nil
}
}
if got := New(tt.args.opts); got == nil {
t.Errorf("New() returned nil")
}
})
}
}