-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththreshold_test.go
198 lines (185 loc) · 4.85 KB
/
threshold_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
188
189
190
191
192
193
194
195
196
197
198
package check
import (
"math"
"reflect"
"testing"
)
var testThresholds = map[string]*Threshold{
"10": {Lower: 0, Upper: 10},
"10:": {Lower: 10, Upper: PosInf},
"~:10": {Lower: NegInf, Upper: 10},
"10:20": {Lower: 10, Upper: 20},
"@10:20": {Lower: 10, Upper: 20, Inside: true},
"-10:10": {Lower: -10, Upper: 10},
"-10.001:10.001": {Lower: -10.001, Upper: 10.001},
"": nil,
}
func TestBoundaryToString(t *testing.T) {
if BoundaryToString(10) != "10" {
t.Fatalf("expected '10', got %s", BoundaryToString(10))
}
if BoundaryToString(10.1) != "10.1" {
t.Fatalf("expected '10.1', got %s", BoundaryToString(10.1))
}
if BoundaryToString(10.001) != "10.001" {
t.Fatalf("expected '10.001', got %s", BoundaryToString(10.001))
}
}
func TestParseThreshold(t *testing.T) {
for spec, ref := range testThresholds {
th, err := ParseThreshold(spec)
if ref == nil {
if err == nil {
t.Errorf("Expected error, got nil")
}
} else {
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !reflect.DeepEqual(ref, th) {
t.Fatalf("expected %v, got %v for spec %s", ref, th, spec)
}
if th.String() != spec {
t.Fatalf("expected %s, got %s for spec %s", spec, th.String(), spec)
}
}
}
}
func TestThreshold_String(t *testing.T) {
for spec, ref := range testThresholds {
if ref != nil {
if spec != ref.String() {
t.Fatalf("expected %v, got %v", ref.String(), spec)
}
}
}
}
// Threshold Generate an alert if x...
// 10 < 0 or > 10, (outside the range of {0 .. 10})
// 10: < 10, (outside {10 .. ∞})
// ~:10 > 10, (outside the range of {-∞ .. 10})
// 10:20 < 10 or > 20, (outside the range of {10 .. 20})
// @10:20 ≥ 10 and ≤ 20, (inside the range of {10 .. 20})
func TestThreshold_DoesViolate(t *testing.T) {
thr, err := ParseThreshold("10")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !thr.DoesViolate(11) {
t.Fatalf("expected true, got false")
}
if thr.DoesViolate(10) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(0) {
t.Fatalf("expected false, got true")
}
if !thr.DoesViolate(-1) {
t.Fatalf("expected true, got false")
}
thr, err = ParseThreshold("10:")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if thr.DoesViolate(3000) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(10) {
t.Fatalf("expected false, got true")
}
if !thr.DoesViolate(9) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(0) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(-1) {
t.Fatalf("expected true, got false")
}
thr, err = ParseThreshold("~:10")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if thr.DoesViolate(-3000) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(0) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(10) {
t.Fatalf("expected false, got true")
}
if !thr.DoesViolate(11) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(3000) {
t.Fatalf("expected true, got false")
}
thr, err = ParseThreshold("10:20")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if thr.DoesViolate(10) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(15) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(20) {
t.Fatalf("expected false, got true")
}
if !thr.DoesViolate(9) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(-1) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(20.1) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(3000) {
t.Fatalf("expected true, got false")
}
thr, err = ParseThreshold("@10:20")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !thr.DoesViolate(10) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(15) {
t.Fatalf("expected true, got false")
}
if !thr.DoesViolate(20) {
t.Fatalf("expected true, got false")
}
if thr.DoesViolate(9) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(-1) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(20.1) {
t.Fatalf("expected false, got true")
}
if thr.DoesViolate(3000) {
t.Fatalf("expected false, got true")
}
}
func TestFormatFloat(t *testing.T) {
if FormatFloat(1000000000000) != "1000000000000" {
t.Fatalf("expected '1000000000000', got %s", FormatFloat(1000000000000))
}
if FormatFloat(1000000000) != "1000000000" {
t.Fatalf("expected '1000000000', got %s", FormatFloat(1000000000))
}
if FormatFloat(1234567890.9877) != "1234567890.988" {
t.Fatalf("expected '1234567890.988', got %s", FormatFloat(1234567890.9877))
}
if FormatFloat(math.Inf(-1)) != "-Inf" {
t.Fatalf("expected '-Inf', got %s", FormatFloat(math.Inf(-1)))
}
if FormatFloat(math.Inf(1)) != "+Inf" {
t.Fatalf("expected '+Inf', got %s", FormatFloat(math.Inf(1)))
}
}