-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeseries_test.go
126 lines (113 loc) · 2.97 KB
/
timeseries_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
package graphblast
import (
"errors"
"strings"
"testing"
"time"
)
func TestTimeSeriesAdd(t *testing.T) {
ts := NewTimeSeries()
when := time.Unix(1400000000, 0)
ts.Add(when, 1, nil)
if len(ts.Values) != 1 {
t.Error("Add did not record value")
}
if ts.Values[when.Format(time.RFC3339Nano)] != 1 {
t.Error("Add recorded wrong value")
}
if ts.Min != 1 && ts.Max != 1 {
t.Error("Add recorded wrong value stat")
}
}
func TestTimeSeriesAddError(t *testing.T) {
ts := NewTimeSeries()
when := time.Unix(1400000000, 0)
ts.Add(when, 1, errors.New("fail"))
if len(ts.Values) != 0 {
t.Error("Add recorded value for error")
}
if ts.Count != 0 || ts.Filtered != 0 || ts.Errors != 1 {
t.Error("Add recorded wrong count stat for error")
}
}
func TestTimeSeriesAddFiltered(t *testing.T) {
ts := NewTimeSeries()
ts.Allowed = Range{Countable(0), Countable(100)}
when := time.Unix(1400000000, 0)
ts.Add(when, 101, nil)
if len(ts.Values) != 0 {
t.Error("Add recorded value for filtered")
}
if ts.Count != 0 || ts.Filtered != 1 || ts.Errors != 0 {
t.Error("Add recorded wrong count stat for filtered")
}
}
func TestTimeSeriesAddWindowed(t *testing.T) {
ts := NewTimeSeries()
ts.Window = 1
ts.Add(time.Unix(1400000000, 0), 1, nil)
ts.Add(time.Unix(1400000001, 0), 2, nil)
if len(ts.Values) != 1 {
t.Error("Add recorded too many values for windowed")
}
if ts.Values[time.Unix(1400000001, 0).Format(time.RFC3339Nano)] != 2 {
t.Error("Add dropped the wrong value for windowed")
}
if ts.Count != 2 || ts.Filtered != 0 || ts.Errors != 0 {
t.Error("Add recorded wrong count stat for filtered")
}
}
func TestTimeSeriesChanged(t *testing.T) {
ts := NewTimeSeries()
changed, next := ts.Changed(0)
if changed {
t.Error("Changed incorrectly reported change")
}
if next != 0 {
t.Error("Changed incorrectly returned new indicator")
}
ts.Add(time.Unix(1400000000, 0), 1, nil)
changed, next = ts.Changed(0)
if !changed {
t.Error("Changed incorrectly reported no change")
}
if next <= 0 {
t.Error("Changed did not return a larger indicator value")
}
}
func TestTimeSeriesRead(t *testing.T) {
ts := NewTimeSeries()
ts.Window = 1
reader := strings.NewReader("1\n2\n")
ts.Read(reader)
if ts.Count != 2 {
t.Error("Read failed to read the input fully")
}
if ts.Errors != 0 {
t.Error("Read failed to read the input without errors")
}
if len(ts.Values) != 1 {
t.Error("Read failed to discard values for windowing")
}
for _, val := range ts.Values {
if val != 2 {
t.Error("Read failed to discard the correct value when windowing")
}
}
reader = strings.NewReader("5\na\n")
ts.Read(reader)
if ts.Count != 3 {
t.Error("Read failed to read the good part of a bad input")
}
if ts.Errors != 1 {
t.Error("Read failed to signal an error in the input")
}
if len(ts.Values) != 1 {
t.Error("Read failed to discard values for windowing")
}
for _, val := range ts.Values {
if val != 5 {
t.Error("Read failed to discard the correct value when windowing")
}
}
}