forked from ConduitIO/bwlimit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_test.go
187 lines (168 loc) · 5.37 KB
/
io_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
// Copyright © 2023 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bwlimit
import (
"crypto/rand"
"fmt"
"io"
"testing"
"time"
)
// epsilon is the allowed difference between the expected and actual delay
var epsilon time.Duration
func init() {
// we measure how long it takes to read 10 bytes and calculate the
// acceptable epsilon in read times when limiting the bandwidth
r := randomReader(10)
start := time.Now()
_, err := r.Read(make([]byte, 2))
epsilon = time.Millisecond + (time.Since(start) * 20)
if err != nil {
panic(fmt.Errorf("error reading: %v", err))
}
}
func randomReader(size int64) io.Reader {
pr, pw := io.Pipe()
go func() {
random := io.LimitReader(rand.Reader, size)
_, err := io.Copy(pw, random)
_ = pw.CloseWithError(err)
}()
return pr
}
func TestReader_Read(t *testing.T) {
t.Logf("epsilon: %v", epsilon)
testCases := []struct {
byteCount int64
bytesPerSecond Byte
wantDelay time.Duration
}{{
byteCount: 1,
bytesPerSecond: 0,
wantDelay: 0, // no delay
}, {
byteCount: 1,
bytesPerSecond: 1,
wantDelay: 0, // no delay
}, {
byteCount: 4,
bytesPerSecond: 2,
// expected delay is 1 second:
// - first read happens instantly, returns 2 bytes
// - second read happens instantly (reservation contains no delay), returns 2 bytes
// - third read is delayed by 1s (reservation contains delay), returns EOF
wantDelay: 1 * time.Second,
}, {
byteCount: 102,
bytesPerSecond: 100,
// expected delay is 20 milliseconds:
// - first read happens instantly, returns 100 bytes
// - second read happens instantly (reservation contains no delay), returns 2 bytes
// - third read is delayed by 20ms (2/100 of a second, as reservation contains 2 tokens), returns EOF
wantDelay: 20 * time.Millisecond,
}, {
byteCount: 103,
bytesPerSecond: 100,
// expected delay is 30 milliseconds:
// - first read happens instantly, returns 100 bytes
// - second read happens instantly (reservation contains no delay), returns 3 bytes
// - third read is delayed by 30ms (3/100 of a second, as reservation contains 3 tokens), returns EOF
wantDelay: 30 * time.Millisecond,
}}
for _, tc := range testCases {
tc := tc // assign to local var as test cases run in parallel
t.Run(fmt.Sprintf("%d byte/%d bps", tc.byteCount, tc.bytesPerSecond), func(t *testing.T) {
// these tests are blocking, run them in parallel
t.Parallel()
r := NewReader(randomReader(tc.byteCount), tc.bytesPerSecond)
start := time.Now()
got, err := io.ReadAll(r)
gotDelay := time.Since(start)
if err != nil {
t.Fatalf("error reading: %v", err)
}
if len(got) != int(tc.byteCount) {
t.Fatalf("expected %d bytes, got %d", tc.byteCount, len(got))
}
gotDiff := tc.wantDelay - gotDelay
if gotDiff < 0 {
gotDiff = -gotDiff
}
if gotDiff > epsilon {
t.Fatalf("expected a maximum delay of %v, got %v, allowed epsilon of %v exceeded", tc.wantDelay, gotDelay, epsilon)
}
})
}
}
func TestWriter_Write(t *testing.T) {
t.Logf("epsilon: %v", epsilon)
testCases := []struct {
byteCount int64
bytesPerSecond Byte
wantDelay time.Duration
}{{
byteCount: 1,
bytesPerSecond: 0,
wantDelay: 0, // no delay
}, {
byteCount: 1,
bytesPerSecond: 1,
wantDelay: 0, // no delay
}, {
byteCount: 4,
bytesPerSecond: 2,
// expected delay is 1 second:
// - first write happens instantly, writes 2 bytes
// - second write is delayed by 1s, writes 2 bytes
wantDelay: 1 * time.Second,
}, {
byteCount: 102,
bytesPerSecond: 100,
// expected delay is 20 milliseconds:
// - first write happens instantly, writes 100 bytes
// - second write is delayed by 20ms (2/100 of a second to reserve 2 tokens), writes 2 bytes
wantDelay: 20 * time.Millisecond,
}, {
byteCount: 103,
bytesPerSecond: 100,
// expected delay is 30 milliseconds:
// - first write happens instantly, writes 100 bytes
// - second write is delayed by 30ms (3/100 of a second to reserve 3 tokens), writes 3 bytes
wantDelay: 30 * time.Millisecond,
}}
for _, tc := range testCases {
tc := tc // assign to local var as test cases run in parallel
t.Run(fmt.Sprintf("%d byte/%d bps", tc.byteCount, tc.bytesPerSecond), func(t *testing.T) {
// these tests are blocking, run them in parallel
t.Parallel()
w := NewWriter(io.Discard, tc.bytesPerSecond)
start := time.Now()
n, err := io.Copy(w, randomReader(tc.byteCount))
gotDelay := time.Since(start)
if err != nil {
t.Fatalf("error reading: %v", err)
}
if n != tc.byteCount {
t.Fatalf("expected %d bytes, got %d", tc.byteCount, n)
}
gotDiff := tc.wantDelay - gotDelay
if gotDiff < 0 {
gotDiff = -gotDiff
}
if gotDiff > epsilon {
t.Fatalf("expected a maximum delay of %v, got %v, allowed epsilon of %v exceeded", tc.wantDelay, gotDelay, epsilon)
}
})
}
}