-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_test.go
105 lines (75 loc) · 1.99 KB
/
health_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
package health
import (
"bytes"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
untabber = strings.NewReplacer("\n\t\t", "\n")
)
func TestHealth_Alert_SavesError(t *testing.T) {
test := assert.New(t)
health := NewHealth()
health.Alert(errors.New("a"), "key1", "key2")
test.EqualValues([]error{Error("a")}, health.GetErrors())
}
func TestHealth_Alert_UsesKeys(t *testing.T) {
test := assert.New(t)
health := NewHealth()
health.Alert(errors.New("a"), "key1", "key2")
test.EqualValues([]error{Error("a")}, health.GetErrors())
test.EqualValues([]string{"key1@key2"}, health.keys)
}
func TestHealth_Resolve_RemovesError(t *testing.T) {
test := assert.New(t)
health := NewHealth()
health.Alert(errors.New("a"), "key1", "key2")
health.Alert(errors.New("b"), "key3", "key4")
health.Resolve("key3", "key4")
test.EqualValues([]error{Error("a")}, health.GetErrors())
}
func TestHealth_MarshalJSON_ReturnsZeroForNoErrors(t *testing.T) {
test := assert.New(t)
health := NewHealth()
buffer := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", "\t")
err := encoder.Encode(health)
test.NoError(err)
test.Equal(
untabber.Replace(`{
"status": 0
}
`),
string(buffer.Bytes()),
)
}
func TestHealth_HasErrors_ReturnsTrueIfErrorExists(t *testing.T) {
test := assert.New(t)
health := NewHealth()
health.Alert(nil, "a")
test.True(health.HasErrors())
}
func TestHealth_HasErrors_ReturnsFalseIfNoErrorExists(t *testing.T) {
test := assert.New(t)
health := NewHealth()
test.False(health.HasErrors())
}
func BenchmarkHealth_Resolve(b *testing.B) {
health := NewHealth()
health.Alert(errors.New("a"), "key1", "key2")
health.Alert(errors.New("b"), "key1", "key2")
health.Alert(errors.New("c"), "key1", "key2")
for i := 0; i < b.N; i++ {
health.Resolve("x")
}
}
func BenchmarkHealth_Alert(b *testing.B) {
health := NewHealth()
for i := 0; i < b.N; i++ {
health.Alert(errors.New("a"), "key1", "key2")
}
}