Skip to content

Commit aaae353

Browse files
committedApr 27, 2023
bombardier: deflake some tests and update dependencies
Replacing fasthttp.Server with httptest.Server in TLS-related test. Fix a bug where fasthttp client adapter wouldn't record response codes correctly. Loosen acceptable range of values in TestBucketLimiterLowRates and TestBucketLimiterHighRates tests + remove concurrency that might be contributing to flakiness.
1 parent 038c7b0 commit aaae353

File tree

5 files changed

+124
-126
lines changed

5 files changed

+124
-126
lines changed
 

‎bombardier_test.go

+29-59
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/x509"
88
"errors"
99
"io/ioutil"
10-
"net"
1110
"net/http"
1211
"net/http/httptest"
1312
"os"
@@ -16,8 +15,6 @@ import (
1615
"sync/atomic"
1716
"testing"
1817
"time"
19-
20-
"github.com/valyala/fasthttp"
2118
)
2219

2320
func TestBombardierShouldFireSpecifiedNumberOfRequests(t *testing.T) {
@@ -223,7 +220,7 @@ func testBombardierTimeoutRecoding(clientType clientTyp, t *testing.T) {
223220
shortTimeout := 10 * time.Millisecond
224221
s := httptest.NewServer(
225222
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
226-
time.Sleep(shortTimeout * 2)
223+
time.Sleep(shortTimeout * 10)
227224
}),
228225
)
229226
defer s.Close()
@@ -365,68 +362,47 @@ func testBombardierClientCerts(clientType clientTyp, t *testing.T) {
365362
return
366363
}
367364

368-
x509Cert, err := x509.ParseCertificate(clientCert.Certificate[0])
365+
clientX509Cert, err := x509.ParseCertificate(clientCert.Certificate[0])
369366
if err != nil {
370367
t.Error(err)
371368
return
372369
}
373370

374-
server := fasthttp.Server{
375-
DisableKeepalive: true,
376-
Handler: func(ctx *fasthttp.RequestCtx) {
377-
certs := ctx.TLSConnectionState().PeerCertificates
378-
if numCerts := len(certs); numCerts != 1 {
379-
t.Errorf("expected 1 cert, but got %v", numCerts)
380-
ctx.Error("invalid number of certs", http.StatusBadRequest)
381-
return
382-
}
383-
384-
cert := certs[0]
385-
if !cert.Equal(x509Cert) {
386-
t.Error("certificates don't match")
387-
ctx.Error("wrong cert", http.StatusBadRequest)
388-
return
389-
}
390-
391-
ctx.Success("text/plain; charset=utf-8", []byte("OK"))
392-
},
393-
}
394-
395-
tlsConfig := &tls.Config{
396-
PreferServerCipherSuites: true,
397-
}
398-
399-
cert, err := tls.LoadX509KeyPair("testserver.cert", "testserver.key")
400-
if err != nil {
401-
t.Errorf("cannot load test TLS cert/key pair: %s", err)
402-
return
403-
}
404-
405-
tlsConfig.Certificates = []tls.Certificate{cert}
406-
tlsConfig.BuildNameToCertificate()
407-
tlsConfig.ClientAuth = tls.RequireAnyClientCert
408-
409-
ln, err := net.Listen("tcp", "localhost:8080")
371+
servertCert, err := tls.LoadX509KeyPair("testserver.cert", "testserver.key")
410372
if err != nil {
411373
t.Error(err)
412374
return
413375
}
414376

415-
go func() {
416-
serr := server.Serve(
417-
tls.NewListener(ln, tlsConfig),
418-
)
377+
tlsConfig := &tls.Config{
378+
ClientAuth: tls.RequireAnyClientCert,
379+
Certificates: []tls.Certificate{servertCert},
380+
}
419381

420-
if serr != nil {
421-
t.Error(err)
382+
server := httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
383+
certs := r.TLS.PeerCertificates
384+
if numCerts := len(certs); numCerts != 1 {
385+
t.Errorf("expected 1 cert, but got %v", numCerts)
386+
rw.WriteHeader(http.StatusBadRequest)
387+
return
422388
}
423-
}()
389+
cert := certs[0]
390+
if !cert.Equal(clientX509Cert) {
391+
t.Error("certificates don't match")
392+
rw.WriteHeader(http.StatusBadRequest)
393+
return
394+
}
395+
rw.WriteHeader(http.StatusOK)
396+
}))
424397

425-
numReqs := uint64(1)
398+
server.TLS = tlsConfig
399+
server.StartTLS()
400+
401+
singleRequest := uint64(1)
426402
b, e := newBombardier(config{
427403
numConns: defaultNumberOfConns,
428-
numReqs: &numReqs,
429-
url: "https://localhost:8080/",
404+
numReqs: &singleRequest,
405+
url: server.URL,
430406
headers: new(headersList),
431407
timeout: defaultTimeout,
432408
method: "GET",
@@ -446,16 +422,10 @@ func testBombardierClientCerts(clientType clientTyp, t *testing.T) {
446422

447423
b.bombard()
448424
if b.req2xx != 1 {
449-
t.Error("no requests succeeded")
425+
t.Error("no 2xx responses, total =", b.reqs, ", 1xx/2xx/3xx/4xx/5xx =", b.req1xx, b.req2xx, b.req3xx, b.req4xx, b.req5xx)
450426
}
451427

452-
err = ln.Close()
453-
if err != nil {
454-
t.Error(err)
455-
}
456-
// TODO(codesenberg): remove. Another hacky attempt to fix Travis CI's
457-
// slowness
458-
time.Sleep(100 * time.Millisecond)
428+
server.Close()
459429
}
460430

461431
func TestBombardierRateLimiting(t *testing.T) {

‎clients.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ func (c *fasthttpClient) do() (
105105
// fire the request
106106
start := time.Now()
107107
err = c.client.Do(req, resp)
108-
if err != nil {
109-
code = -1
110-
} else {
111-
code = resp.StatusCode()
112-
}
108+
code = resp.StatusCode()
113109
usTaken = uint64(time.Since(start).Nanoseconds() / 1000)
114110

115111
// release resources

‎go.mod

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
module github.com/codesenberg/bombardier
22

3-
go 1.15
3+
go 1.13
44

55
require (
66
github.com/alecthomas/kingpin v2.2.6+incompatible
77
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
8-
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
9-
github.com/andybalholm/brotli v1.0.1 // indirect
8+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
109
github.com/cheggaaa/pb v1.0.29
1110
github.com/codesenberg/concurrent v0.0.0-20180531114123-64560cfcf964
12-
github.com/juju/ratelimit v1.0.1
13-
github.com/klauspost/compress v1.11.8 // indirect
11+
github.com/juju/ratelimit v1.0.2
12+
github.com/klauspost/compress v1.16.5 // indirect
13+
github.com/mattn/go-runewidth v0.0.14 // indirect
14+
github.com/rivo/uniseg v0.4.4 // indirect
1415
github.com/satori/go.uuid v1.2.0
1516
github.com/stretchr/testify v1.7.0 // indirect
16-
github.com/valyala/fasthttp v1.21.0
17-
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0
17+
github.com/valyala/fasthttp v1.46.0
18+
golang.org/x/net v0.9.0
1819
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
1920
)

‎go.sum

+54-24
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvr
22
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
33
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
44
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5-
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
6-
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
7-
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
8-
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
9-
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
10-
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
5+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
6+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
7+
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
8+
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
119
github.com/cheggaaa/pb v1.0.29 h1:FckUN5ngEk2LpvuG0fw1GEFx6LtyY2pWI/Z2QgCnEYo=
1210
github.com/cheggaaa/pb v1.0.29/go.mod h1:W40334L7FMC5JKWldsTWbdGjLo0RxUKK73K+TuPxX30=
1311
github.com/codesenberg/concurrent v0.0.0-20180531114123-64560cfcf964 h1:9MVnbW3h0Dl4E2oADqwyvODphl9jY1r5HMtcB8U5mGs=
@@ -16,12 +14,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
1614
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1715
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
1816
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
19-
github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY=
20-
github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
21-
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
22-
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
23-
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
24-
github.com/klauspost/compress v1.11.8/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
17+
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
18+
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
19+
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
20+
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
21+
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
2522
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
2623
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
2724
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -32,10 +29,14 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
3229
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
3330
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
3431
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
35-
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
3632
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
33+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
34+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
3735
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3836
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
37+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
38+
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
39+
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3940
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
4041
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
4142
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -44,24 +45,53 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
4445
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4546
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
4647
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
47-
github.com/valyala/fasthttp v1.21.0 h1:fJjaQ7cXdaSF9vDBujlHLDGj7AgoMTMIXvICeePzYbU=
48-
github.com/valyala/fasthttp v1.21.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
49-
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
48+
github.com/valyala/fasthttp v1.46.0 h1:6ZRhrFg8zBXTRYY6vdzbFhqsBd7FVv123pV2m9V87U4=
49+
github.com/valyala/fasthttp v1.46.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
50+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
51+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
5052
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
51-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
52-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
53-
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 h1:5kGOVHlq0euqwzgTC9Vu15p6fV1Wi0ArVi8da2urnVg=
54-
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
53+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
54+
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
55+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
56+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
57+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
58+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
59+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
60+
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
61+
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
62+
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
63+
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
64+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
65+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
66+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5567
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5668
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
57-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5869
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
59-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
60-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
70+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
71+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
72+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
73+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
74+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
75+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
76+
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
77+
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
78+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
79+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
80+
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
81+
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
82+
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
6183
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
62-
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
6384
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
85+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
86+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
87+
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
88+
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
89+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
6490
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
91+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
92+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
93+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
94+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6595
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6696
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
6797
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

‎limiter_test.go

+32-31
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,38 @@ func TestBucketLimiterLowRates(t *testing.T) {
6969
{1500, 1 * time.Second},
7070
{5000, 1 * time.Second},
7171
}
72-
var lwg sync.WaitGroup
73-
lwg.Add(len(expectations))
7472
for i := range expectations {
7573
exp := expectations[i]
74+
lim := newBucketLimiter(exp.rate)
75+
done := make(chan struct{})
76+
counter := uint64(0)
77+
waitChan := make(chan struct{})
7678
go func() {
77-
defer lwg.Done()
78-
lim := newBucketLimiter(exp.rate)
79-
done := make(chan struct{})
80-
counter := uint64(0)
81-
waitChan := make(chan struct{})
82-
go func() {
83-
defer func() {
84-
waitChan <- struct{}{}
85-
}()
86-
for lim.pace(done) == cont {
87-
counter++
88-
}
79+
defer func() {
80+
waitChan <- struct{}{}
8981
}()
90-
time.Sleep(exp.duration)
91-
close(done)
92-
select {
93-
case <-waitChan:
94-
break
95-
case <-time.After(exp.duration + 100*time.Millisecond):
96-
t.Error("failed to complete: ", exp)
97-
return
98-
}
99-
expcounter := float64(exp.rate) * exp.duration.Seconds()
100-
if float64(counter) < (expcounter*0.9) ||
101-
float64(counter) > (expcounter*1.1+5) {
102-
t.Error(expcounter, counter)
82+
for lim.pace(done) == cont {
83+
counter++
10384
}
10485
}()
86+
time.Sleep(exp.duration)
87+
close(done)
88+
select {
89+
case <-waitChan:
90+
case <-time.After(exp.duration + 100*time.Millisecond):
91+
t.Error("failed to complete: ", exp)
92+
return
93+
}
94+
expcounter := float64(exp.rate) * exp.duration.Seconds()
95+
var (
96+
lowerBound = 0.5 * expcounter
97+
upperBound = 1.2*expcounter + 5
98+
)
99+
if float64(counter) < lowerBound ||
100+
float64(counter) > upperBound {
101+
t.Errorf("(lower bound, actual, upper bound): (%11.2f, %11d, %11.2f)", lowerBound, counter, upperBound)
102+
}
105103
}
106-
lwg.Wait()
107104
}
108105

109106
func TestBucketLimiterHighRates(t *testing.T) {
@@ -140,9 +137,13 @@ func TestBucketLimiterHighRates(t *testing.T) {
140137
return
141138
}
142139
expcounter := float64(exp.rate) * exp.duration.Seconds()
143-
if float64(counter) < (expcounter*0.9) ||
144-
float64(counter) > (expcounter*1.1) {
145-
t.Error(expcounter, counter)
140+
var (
141+
lowerBound = 0.5 * expcounter
142+
upperBound = 1.2*expcounter + 5
143+
)
144+
if float64(counter) < lowerBound ||
145+
float64(counter) > upperBound {
146+
t.Errorf("(lower bound, actual, upper bound): (%11.2f, %11d, %11.2f)", lowerBound, counter, upperBound)
146147
}
147148
}
148149
}

0 commit comments

Comments
 (0)
Please sign in to comment.