-
Notifications
You must be signed in to change notification settings - Fork 4
/
limit_manager_test.go
172 lines (120 loc) · 2.88 KB
/
limit_manager_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
package limio
import (
"bytes"
"io"
"strings"
"sync"
"testing"
"time"
"github.com/golang/glog"
"github.com/stretchr/testify/assert"
_ "net/http/pprof"
)
func verifyIsManager(m Manager) {}
func TestManager(t *testing.T) {
asrt := assert.New(t)
lmr := NewSimpleManager()
asrt.Implements((*Manager)(nil), lmr)
ch := make(chan int, 1)
lmr.Limit(ch)
l1 := lmr.NewReader(strings.NewReader(testText))
l2 := lmr.NewReader(strings.NewReader(testText))
p := make([]byte, len(testText)+1)
ch <- 20
glog.Info("Reading 1")
n, err := l1.Read(p)
glog.Infof("Read 1: %d", n)
asrt.NoError(err)
asrt.Equal(10, n)
glog.Info("Reading 2")
n, err = l2.Read(p)
glog.Infof("Read 2: %d", n)
asrt.NoError(err)
asrt.Equal(10, n)
l3 := lmr.NewReader(strings.NewReader(testText))
asrt.NoError(lmr.Manage(l3))
ch <- 30
glog.Info("Reading 3")
n, err = l3.Read(p)
assert.NoError(t, err)
asrt.Equal(n, 10)
glog.Infof("Read 3: %d", n)
_, err = l1.Read(p)
asrt.NoError(err)
_, err = l2.Read(p)
asrt.NoError(err)
lmr.Unmanage(l3)
glog.Info("unmanage done")
lmr.SimpleLimit(KB, 10*time.Millisecond)
//Drain channel
n, err = l1.Read(p)
asrt.NoError(err)
m, err := l2.Read(p)
asrt.NoError(err)
asrt.Equal(1024, n+m, "Wrong number of total bytes read")
if err != nil {
t.Errorf("Error reading: %v", err)
}
lmr.Unlimit()
n, err = l1.Read(p)
if n != len(testText)-20-m {
t.Errorf("Wrong number read after unlimit: %d", n)
}
if err != nil && err != io.EOF {
t.Errorf("Error reading unlimited: %v", err)
}
n, err = l2.Read(p)
if n != len(testText)-20-m {
t.Errorf("Wrong number read after unlimit: %d", n)
}
if err != nil && err != io.EOF {
t.Errorf("Error reading unlimited: %v", err)
}
_, err = l2.Read(p)
if err != io.EOF {
t.Errorf("Should have thrown EOF after reached EOF.")
}
done := lmr.SimpleLimit(KB, time.Second)
lmr.Manage(l3)
w := &sync.WaitGroup{}
w.Add(1)
go func() {
if cls := <-done; !cls {
t.Errorf("Did not close \"done\" and pass true. Done: %t", cls)
}
w.Done()
}()
err = lmr.Close()
if err != nil {
t.Errorf("Error closing lmr: %v", err)
}
n, err = l3.Read(p)
if n != len(testText)-10 {
t.Errorf("Wrong number of bytes read: %d, should be %d", n, len(testText)-10)
}
w.Wait()
}
func ExampleSimpleManager() {
slowCopy := func(ws []io.Writer, rs []io.Reader) error {
// For a simpler example, imagine len(ws) == len(rs) always
lmr := NewSimpleManager()
// Limit all operations to an aggregate 1MB/s
lmr.SimpleLimit(1*MB, time.Second)
wg := &sync.WaitGroup{}
wg.Add(len(ws))
for i := range ws {
go func(i int) {
lr := NewReader(rs[i])
lmr.Manage(lr)
// Obviously handle the errors in a real implementation
io.Copy(ws[i], lr)
wg.Done()
}(i)
}
wg.Wait()
return nil
}
buf := &bytes.Buffer{}
rdr := strings.NewReader(testText)
slowCopy([]io.Writer{buf}, []io.Reader{rdr})
}