-
Notifications
You must be signed in to change notification settings - Fork 11
/
flow_test.go
141 lines (115 loc) · 2.77 KB
/
flow_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
package flow
import (
"math"
"sync"
"testing"
"time"
)
func TestBasic(t *testing.T) {
m := new(Meter)
for i := 0; i < 300; i++ {
m.Mark(1000)
mockClock.Add(40 * time.Millisecond)
}
if rate := m.Snapshot().Rate; rate != 25000 {
t.Errorf("expected rate 25000, got %f", rate)
}
for i := 0; i < 400; i++ {
m.Mark(200)
mockClock.Add(40 * time.Millisecond)
}
if rate := m.Snapshot().Rate; !approxEq(rate, 5000, 1) {
t.Errorf("expected rate 5000, got %f", rate)
}
mockClock.Add(time.Second)
if total := m.Snapshot().Total; total != 380000 {
t.Errorf("expected total %d, got %d", 380000, total)
}
}
func TestShared(t *testing.T) {
var wg sync.WaitGroup
wg.Add(20)
m := new(Meter)
for j := 0; j < 20; j++ {
go func() {
defer wg.Done()
for i := 0; i < 300; i++ {
m.Mark(50)
mockClock.Sleep(40 * time.Millisecond)
}
for i := 0; i < 300; i++ {
m.Mark(10)
mockClock.Sleep(40 * time.Millisecond)
}
}()
}
time.Sleep(time.Millisecond)
mockClock.Add(20 * 300 * time.Millisecond)
time.Sleep(time.Millisecond)
mockClock.Add(20 * 300 * time.Millisecond)
time.Sleep(time.Millisecond)
actual := m.Snapshot()
if !approxEq(actual.Rate, 25000, 1) {
t.Errorf("expected rate 25000, got %f", actual.Rate)
}
time.Sleep(time.Millisecond)
mockClock.Add(20 * 300 * time.Millisecond)
time.Sleep(time.Millisecond)
mockClock.Add(20 * 300 * time.Millisecond)
time.Sleep(time.Millisecond)
// Adjusts
actual = m.Snapshot()
if !approxEq(actual.Rate, 5000, 1) {
t.Errorf("expected rate 5000, got %f", actual.Rate)
}
// Let it settle.
time.Sleep(time.Millisecond)
mockClock.Add(time.Second)
time.Sleep(time.Millisecond)
mockClock.Add(time.Second)
time.Sleep(time.Millisecond)
// get the right total
actual = m.Snapshot()
if actual.Total != 360000 {
t.Errorf("expected total %d, got %d", 360000, actual.Total)
}
wg.Wait()
}
func TestUnregister(t *testing.T) {
m := new(Meter)
for i := 0; i < 40; i++ {
m.Mark(1)
mockClock.Add(100 * time.Millisecond)
}
actual := m.Snapshot()
if actual.Rate != 10 {
t.Errorf("expected rate 10, got %f", actual.Rate)
}
mockClock.Add(62 * time.Second)
if m.accumulator.Load() != 0 {
t.Error("expected meter to be paused")
}
actual = m.Snapshot()
if actual.Total != 40 {
t.Errorf("expected total 4000, got %d", actual.Total)
}
for i := 0; i < 40; i++ {
m.Mark(2)
mockClock.Add(100 * time.Millisecond)
}
actual = m.Snapshot()
if actual.Rate != 20 {
t.Errorf("expected rate 20, got %f", actual.Rate)
}
mockClock.Add(2 * time.Second)
actual = m.Snapshot()
if actual.Total != 120 {
t.Errorf("expected total 120, got %d", actual.Total)
}
if m.accumulator.Load() == 0 {
t.Error("expected meter to be active")
}
}
func approxEq(a, b, err float64) bool {
return math.Abs(a-b) < err
}