-
Notifications
You must be signed in to change notification settings - Fork 9
/
slidingwindow_test.go
133 lines (118 loc) · 3.72 KB
/
slidingwindow_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
package average
import (
"testing"
"time"
)
func TestNew(t *testing.T) {
_, err := New(time.Second, time.Second)
if err == nil || err.Error() != "window size has to be a multiplier of the granularity size" {
t.Errorf("expected multiplier error, not %q", err)
}
_, err = New(time.Second, 2*time.Second)
if err == nil || err.Error() != "window size has to be a multiplier of the granularity size" {
t.Errorf("expected multiplier error, not %q", err)
}
_, err = New(3*time.Second, 2*time.Second)
if err == nil || err.Error() != "window size has to be a multiplier of the granularity size" {
t.Errorf("expected multiplier error, not %q", err)
}
_, err = New(0, time.Second)
if err == nil || err.Error() != "window cannot be 0" {
t.Errorf("expected window size cannot be 0 error, not %q", err)
}
_, err = New(time.Second, 0)
if err == nil || err.Error() != "granularity cannot be 0" {
t.Errorf("expected granularity cannot be 0 error, not %q", err)
}
}
func TestAdd(t *testing.T) {
sw := &SlidingWindow{
window: 2 * time.Second,
granularity: time.Second,
samples: []int64{1, 1},
pos: 1,
size: 2,
}
sw.Add(1)
if v := sw.samples[1]; v != 2 {
t.Errorf("expected the 2nd sample to be 2, but got %d", v)
}
}
func TestAverage(t *testing.T) {
sw := &SlidingWindow{
window: 10 * time.Second,
granularity: time.Second,
samples: []int64{1, 2, 5, 0, 0, 0, 0, 0, 4, 0},
pos: 2,
size: 10,
}
if v := sw.Average(0); v != 0 {
t.Errorf("expected the average with a window of 0 seconds be 0, not %f", v)
}
if v := sw.Average(time.Second); v != 2 {
t.Errorf("expected the average over the last second to be 2, not %f", v)
}
if v := sw.Average(2 * time.Second); v != 1.5 {
t.Errorf("expected the average over the 2 seconds to be 1.5, not %f", v)
}
if v := sw.Average(4 * time.Second); v != 1.75 {
t.Errorf("expected the average over the 4 seconds to be 1.75, not %f", v)
}
if v := sw.Average(10 * time.Second); v != 1.20 {
t.Errorf("expected the average over the 10 seconds to be 1.20, not %f", v)
}
// This one should be equivalent to 10 seconds.
if v := sw.Average(20 * time.Second); v != 1.20 {
t.Errorf("expected the average over the 20 seconds to be 1.20, not %f", v)
}
}
func TestReset(t *testing.T) {
sw := MustNew(2*time.Second, time.Second)
defer sw.Stop()
sw.samples = []int64{1, 2}
sw.pos = 1
sw.size = 10
sw.Reset()
for _, v := range sw.samples {
if v != 0 {
t.Fatalf("expected the samples all to be 0, but at least one value was %d", v)
}
}
}
func TestResetFlow(t *testing.T) {
sw := MustNew(time.Second, 10*time.Millisecond)
defer sw.Stop()
sw.Reset()
time.Sleep(50 * time.Millisecond)
sw.Reset()
time.Sleep(50 * time.Millisecond)
sw.Reset()
}
func TestTotal(t *testing.T) {
sw := &SlidingWindow{
window: 10 * time.Second,
granularity: time.Second,
samples: []int64{1, 2, 5, 0, 0, 0, 0, 0, 4, 0},
pos: 2,
size: 10,
}
if v, _ := sw.Total(0); v != 0 {
t.Errorf("expected the total with a window of 0 seconds to be 0, not %d", v)
}
if v, _ := sw.Total(time.Second); v != 2 {
t.Errorf("expected the total over the last second to be 2, not %d", v)
}
if v, _ := sw.Total(2 * time.Second); v != 3 {
t.Errorf("expected the total over the last 2 seconds to be 3, not %d", v)
}
if v, _ := sw.Total(4 * time.Second); v != 7 {
t.Errorf("expected the total over the last 4 seconds to be 7, not %d", v)
}
if v, _ := sw.Total(10 * time.Second); v != 12 {
t.Errorf("expected the total over the last 10 seconds to be 12, not %d", v)
}
// This one should be equivalent to 10 seconds.
if v, _ := sw.Total(20 * time.Second); v != 12 {
t.Errorf("expected the total over the last 10 seconds to be 12, not %d", v)
}
}