-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpool_test.go
215 lines (170 loc) · 4.19 KB
/
pool_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
package iocontrol
import (
"bytes"
"io"
"io/ioutil"
"sync"
"testing"
"time"
)
// writer
func TestWriterPool(t *testing.T) {
writePerSec := 10 * KiB
maxBurst := 5 * time.Millisecond
var wg sync.WaitGroup
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
pool := NewWriterPool(writePerSec, maxBurst)
mwGlobal := NewMeasuredWriter(ioutil.Discard)
mwA := NewMeasuredWriter(mwGlobal)
mwB := NewMeasuredWriter(mwGlobal)
wg.Add(1)
go func() {
defer wg.Done()
useWriter(pool.Get(mwA))
}()
time.Sleep(time.Millisecond * 10)
if want, got := 1, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
wg.Add(1)
go func() {
defer wg.Done()
useWriter(pool.Get(mwB))
}()
time.Sleep(time.Millisecond * 10)
if want, got := 2, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
assertWriteRate(t, writePerSec, mwGlobal, 5, 20*time.Millisecond)
assertWriteRate(t, writePerSec/2, mwA, 5, 20*time.Millisecond)
assertWriteRate(t, writePerSec/2, mwB, 5, 20*time.Millisecond)
oldRate := pool.SetRate(1 * GiB)
if oldRate != writePerSec {
t.Errorf("want old rate %v, got %v", writePerSec, oldRate)
}
select {
case <-done:
case <-time.After(time.Second * 2):
panic("too long!")
}
if want, got := 0, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
// check that we can restart the process
pool.SetRate(writePerSec)
var reusedG sync.WaitGroup
reusedDone := make(chan struct{})
go func() {
reusedG.Wait()
close(reusedDone)
}()
loneWriter := NewMeasuredWriter(ioutil.Discard)
reusedG.Add(1)
go func() {
defer reusedG.Done()
useWriter(pool.Get(loneWriter))
}()
time.Sleep(time.Millisecond * 10)
if want, got := 1, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
assertWriteRate(t, writePerSec, loneWriter, 5, 20*time.Millisecond)
// make it finish
pool.SetRate(1 * GiB)
select {
case <-reusedDone:
case <-time.After(time.Second * 2):
panic("too long!")
}
}
func useWriter(w io.Writer, release func()) {
totalSize := 10 * MiB
defer release()
src := bytes.NewReader(make([]byte, totalSize))
io.Copy(w, src)
}
// reader
func TestReaderPool(t *testing.T) {
totalSize := 10 * MiB
readPerSec := 10 * KiB
maxBurst := 5 * time.Millisecond
src := bytes.NewReader(make([]byte, totalSize))
var wg sync.WaitGroup
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
pool := NewReaderPool(readPerSec, maxBurst)
mrGlobal := NewMeasuredReader(src)
mrA := NewMeasuredReader(mrGlobal)
mrB := NewMeasuredReader(mrGlobal)
wg.Add(1)
go func() {
defer wg.Done()
useReader(pool.Get(mrA))
}()
time.Sleep(time.Millisecond * 10)
if want, got := 1, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
wg.Add(1)
go func() {
defer wg.Done()
useReader(pool.Get(mrB))
}()
time.Sleep(time.Millisecond * 10)
if want, got := 2, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
assertReadRate(t, readPerSec, mrGlobal, 5, 20*time.Millisecond)
assertReadRate(t, readPerSec/2, mrA, 5, 20*time.Millisecond)
assertReadRate(t, readPerSec/2, mrB, 5, 20*time.Millisecond)
oldRate := pool.SetRate(1 * GiB)
if oldRate != readPerSec {
t.Errorf("want old rate %v, got %v", readPerSec, oldRate)
}
select {
case <-done:
case <-time.After(time.Second * 2):
panic("too long!")
}
if want, got := 0, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
// check that we can restart the process
pool.SetRate(readPerSec)
var reusedG sync.WaitGroup
reusedDone := make(chan struct{})
go func() {
reusedG.Wait()
close(reusedDone)
}()
src.Seek(0, 0)
loneReader := NewMeasuredReader(src)
reusedG.Add(1)
go func() {
defer reusedG.Done()
useReader(pool.Get(loneReader))
}()
time.Sleep(time.Millisecond * 10)
if want, got := 1, pool.Len(); want != got {
t.Errorf("want Len %d, got %d", want, got)
}
assertReadRate(t, readPerSec, loneReader, 5, 20*time.Millisecond)
// make it finish
pool.SetRate(1 * GiB)
select {
case <-reusedDone:
case <-time.After(time.Second * 2):
panic("too long!")
}
}
func useReader(r io.Reader, release func()) {
defer release()
io.Copy(ioutil.Discard, r)
}