-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate_limiter_test.go
69 lines (57 loc) · 1.73 KB
/
rate_limiter_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
package grab
import (
"context"
"log"
"os"
"testing"
"time"
"github.com/cavaliercoder/grab/grabtest"
)
// testRateLimiter is a naive rate limiter that limits throughput to r tokens
// per second. The total number of tokens issued is tracked as n.
type testRateLimiter struct {
r, n int
}
func NewLimiter(r int) RateLimiter {
return &testRateLimiter{r: r}
}
func (c *testRateLimiter) WaitN(ctx context.Context, n int) (err error) {
c.n += n
time.Sleep(
time.Duration(1.00 / float64(c.r) * float64(n) * float64(time.Second)))
return
}
func TestRateLimiter(t *testing.T) {
// download a 128 byte file, 8 bytes at a time, with a naive 512bps limiter
// should take > 250ms
filesize := 128
filename := ".testRateLimiter"
defer os.Remove(filename)
grabtest.WithTestServer(t, func(url string) {
// limit to 512bps
lim := &testRateLimiter{r: 512}
req := mustNewRequest(filename, url)
// ensure multiple trips to the rate limiter by downloading 8 bytes at a time
req.BufferSize = 8
req.RateLimiter = lim
resp := mustDo(req)
testComplete(t, resp)
if lim.n != filesize {
t.Errorf("expected %d bytes to pass through limiter, got %d", filesize, lim.n)
}
if resp.Duration().Seconds() < 0.25 {
// BUG: this test can pass if the transfer was slow for unrelated reasons
t.Errorf("expected transfer to take >250ms, took %v", resp.Duration())
}
}, grabtest.ContentLength(filesize))
}
func ExampleRateLimiter() {
req, _ := NewRequest("", "http://www.golang-book.com/public/pdf/gobook.pdf")
// Attach a 1Mbps rate limiter, like the token bucket implementation from
// golang.org/x/time/rate.
req.RateLimiter = NewLimiter(1048576)
resp := DefaultClient.Do(req)
if err := resp.Err(); err != nil {
log.Fatal(err)
}
}