forked from codesenberg/bombardier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rateestimator_test.go
69 lines (65 loc) · 1.75 KB
/
rateestimator_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 main
import (
"testing"
"time"
)
func TestRateEstimatorPanicWithZeroRate(t *testing.T) {
defer func() {
pv, ok := recover().(string)
if !ok {
t.Error("expected string value")
return
}
if pv != panicZeroRate {
t.Error(panicZeroRate, pv)
}
}()
_, _ = estimate(0, 10*time.Second)
t.Error("should fail with rate == 0")
}
func TestRateEstimatorPanicWithNegativeAdjustTo(t *testing.T) {
defer func() {
pv, ok := recover().(string)
if !ok {
t.Error("expected string value")
return
}
if pv != panicNegativeAdjustTo {
t.Error(panicNegativeAdjustTo, pv)
}
}()
_, _ = estimate(10, -10*time.Second)
t.Error("should fail with adjustTo <= 0")
}
func TestRateEstimatorAccuracy(t *testing.T) {
defer func() {
rv := recover()
if rv != nil {
t.Error(rv)
}
}()
expectations := []struct {
rate uint64
adjustTo time.Duration
expectedQuantum uint64
expectedFillInterval time.Duration
}{
{1, 100 * time.Millisecond, 1, 1 * time.Second},
{1, 1000 * time.Millisecond, 1, 1 * time.Second},
{1, 2000 * time.Millisecond, 2, 2 * time.Second},
{1, 3000 * time.Millisecond, 3, 3 * time.Second},
{4, 3000 * time.Millisecond, 12, 3 * time.Second},
{10000, 100 * time.Millisecond, 1000, 100 * time.Millisecond},
{100000, 100 * time.Millisecond, 10000, 100 * time.Millisecond},
{1000000, 100 * time.Millisecond, 100000, 100 * time.Millisecond},
}
for _, exp := range expectations {
actualFillInterval, actualQuantum := estimate(exp.rate, exp.adjustTo)
if actualFillInterval != exp.expectedFillInterval ||
actualQuantum != exp.expectedQuantum {
t.Log("Expected: ", exp.expectedQuantum, exp.expectedFillInterval)
t.Log("Actual: ", actualQuantum, actualFillInterval)
t.Fail()
}
}
}