forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckup_test.go
188 lines (168 loc) · 4.79 KB
/
checkup_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
package checkup
import (
"errors"
"testing"
"time"
)
func TestCheckAndStore(t *testing.T) {
f := new(fake)
c := Checkup{
Storage: f,
Checkers: []Checker{f, f},
ConcurrentChecks: 1,
Timestamp: time.Now(),
}
err := c.CheckAndStore()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := f.checked, 2; got != want {
t.Errorf("Expected %d checks to be executed, but had: %d", want, got)
}
if got, want := len(f.stored), 2; got != want {
t.Errorf("Expected %d checks to be stored, but had: %d", want, got)
}
for i := range f.stored {
if i > 0 && f.stored[i].Timestamp != f.stored[i-1].Timestamp {
t.Error("Expected timestamps to be the same, but they weren't")
}
}
// Check error handling
f.returnErr = true
err = c.CheckAndStore()
if err == nil {
t.Error("Expected an error, didn't get one")
}
if got, want := err.Error(), "i'm an error; i'm an error"; got != want {
t.Errorf(`Expected error string "%s" but got: "%s"`, want, got)
}
c.ConcurrentChecks = -1
_, err = c.Check()
if err == nil {
t.Error("Expected an error with ConcurrentChecks < 0, didn't get one")
}
c.ConcurrentChecks = 0
c.Storage = nil
err = c.CheckAndStore()
if err == nil {
t.Error("Expected an error with no storage, didn't get one")
}
}
func TestCheckAndStoreEvery(t *testing.T) {
f := new(fake)
c := Checkup{Storage: f, Checkers: []Checker{f}}
ticker := c.CheckAndStoreEvery(50 * time.Millisecond)
time.Sleep(170 * time.Millisecond)
ticker.Stop()
if got, want := f.checked, 3; got != want {
t.Errorf("Expected %d checks while sleeping, had: %d", want, got)
}
}
func TestComputeStats(t *testing.T) {
s := Result{Times: []Attempt{
{RTT: 7 * time.Second},
{RTT: 4 * time.Second},
{RTT: 4 * time.Second},
{RTT: 6 * time.Second},
{RTT: 6 * time.Second},
{RTT: 3 * time.Second},
}}.ComputeStats()
if got, want := s.Total, 30*time.Second; got != want {
t.Errorf("Expected Total=%v, got %v", want, got)
}
if got, want := s.Average, 5*time.Second; got != want {
t.Errorf("Expected Average=%v, got %v", want, got)
}
if got, want := s.Median, 5*time.Second; got != want {
t.Errorf("Expected Median=%v, got %v", want, got)
}
if got, want := s.Min, 3*time.Second; got != want {
t.Errorf("Expected Min=%v, got %v", want, got)
}
if got, want := s.Max, 7*time.Second; got != want {
t.Errorf("Expected Max=%v, got %v", want, got)
}
}
func TestResultStatus(t *testing.T) {
r := Result{Healthy: true}
if got, want := r.Status(), Healthy; got != want {
t.Errorf("Expected status '%s' but got: '%s'", want, got)
}
r = Result{Degraded: true}
if got, want := r.Status(), Degraded; got != want {
t.Errorf("Expected status '%s' but got: '%s'", want, got)
}
r = Result{Down: true}
if got, want := r.Status(), Down; got != want {
t.Errorf("Expected status '%s' but got: '%s'", want, got)
}
r = Result{}
if got, want := r.Status(), Unknown; got != want {
t.Errorf("Expected status '%s' but got: '%s'", want, got)
}
// These are invalid states, but we need to test anyway in case a
// checker is buggy. We expect the worst of the enabled fields.
r = Result{Down: true, Degraded: true}
if got, want := r.Status(), Down; got != want {
t.Errorf("(INVALID RESULT CASE) Expected status '%s' but got: '%s'", want, got)
}
r = Result{Degraded: true, Healthy: true}
if got, want := r.Status(), Degraded; got != want {
t.Errorf("(INVALID RESULT CASE) Expected status '%s' but got: '%s'", want, got)
}
r = Result{Down: true, Healthy: true}
if got, want := r.Status(), Down; got != want {
t.Errorf("(INVALID RESULT CASE) Expected status '%s' but got: '%s'", want, got)
}
}
func TestPriorityOver(t *testing.T) {
for i, test := range []struct {
status StatusText
another StatusText
expected bool
}{
{Down, Down, false},
{Down, Degraded, true},
{Down, Healthy, true},
{Down, Unknown, true},
{Degraded, Down, false},
{Degraded, Degraded, false},
{Degraded, Healthy, true},
{Degraded, Unknown, true},
{Healthy, Down, false},
{Healthy, Degraded, false},
{Healthy, Healthy, false},
{Healthy, Unknown, true},
{Unknown, Down, false},
{Unknown, Degraded, false},
{Unknown, Healthy, false},
{Unknown, Unknown, false},
} {
actual := test.status.PriorityOver(test.another)
if actual != test.expected {
t.Errorf("Test %d: Expected %s.PriorityOver(%s)=%v, but got %v",
i, test.status, test.another, test.expected, actual)
}
}
}
var errTest = errors.New("i'm an error")
type fake struct {
returnErr bool
checked int
stored []Result
}
func (f *fake) Check() (Result, error) {
f.checked++
r := Result{Timestamp: time.Now().UTC().UnixNano()}
if f.returnErr {
return r, errTest
}
return r, nil
}
func (f *fake) Store(results []Result) error {
f.stored = results
if f.returnErr {
return errTest
}
return nil
}