forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlreader_test.go
129 lines (125 loc) · 2.28 KB
/
rlreader_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
package torrent
import (
"io"
"log"
"math/rand"
"sync"
"testing"
"time"
"github.com/bradfitz/iter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)
func writeN(ws []io.Writer, n int) error {
b := make([]byte, n)
for _, w := range ws[1:] {
n1 := rand.Intn(n)
wn, err := w.Write(b[:n1])
if wn != n1 {
if err == nil {
panic(n1)
}
return err
}
n -= n1
}
wn, err := ws[0].Write(b[:n])
if wn != n {
if err == nil {
panic(n)
}
}
return err
}
func TestRateLimitReaders(t *testing.T) {
const (
numReaders = 2
bytesPerSecond = 100
burst = 5
readSize = 6
writeRounds = 10
bytesPerRound = 12
)
control := rate.NewLimiter(bytesPerSecond, burst)
shared := rate.NewLimiter(bytesPerSecond, burst)
var (
ws []io.Writer
cs []io.Closer
)
wg := sync.WaitGroup{}
type read struct {
N int
// When the read was allowed.
At time.Time
}
reads := make(chan read)
done := make(chan struct{})
for range iter.N(numReaders) {
r, w := io.Pipe()
ws = append(ws, w)
cs = append(cs, w)
wg.Add(1)
go func() {
defer wg.Done()
r := rateLimitedReader{
l: shared,
r: r,
}
b := make([]byte, readSize)
for {
n, err := r.Read(b)
select {
case reads <- read{n, r.lastRead}:
case <-done:
return
}
if err == io.EOF {
return
}
if err != nil {
panic(err)
}
}
}()
}
closeAll := func() {
for _, c := range cs {
c.Close()
}
}
defer func() {
close(done)
closeAll()
wg.Wait()
}()
written := 0
go func() {
for range iter.N(writeRounds) {
err := writeN(ws, bytesPerRound)
if err != nil {
log.Printf("error writing: %s", err)
break
}
written += bytesPerRound
}
closeAll()
wg.Wait()
close(reads)
}()
totalBytesRead := 0
started := time.Now()
for r := range reads {
totalBytesRead += r.N
require.False(t, r.At.IsZero())
// Copy what the reader should have done with its reservation.
res := control.ReserveN(r.At, r.N)
// If we don't have to wait with the control, the reader has gone too
// fast.
if res.Delay() > 0 {
log.Printf("%d bytes not allowed at %s", r.N, time.Since(started))
t.FailNow()
}
}
assert.EqualValues(t, writeRounds*bytesPerRound, totalBytesRead)
}