-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_test.go
128 lines (107 loc) · 3.39 KB
/
checker_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
package avchecker
import (
"encoding/json"
"fmt"
"github.com/claudetech/loggo"
g "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"net/http"
"time"
)
type successClient struct{}
func (s *successClient) Do(request *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
}, nil
}
type failClient struct{}
func (s *failClient) Do(request *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 500,
}, nil
}
type errorClient struct{}
func (s *errorClient) Do(request *http.Request) (*http.Response, error) {
return &http.Response{}, fmt.Errorf("error")
}
type dummyReporter struct {
reports []map[string]interface{}
}
func (r *dummyReporter) SendStats(stats []byte) error {
var report map[string]interface{}
if err := json.Unmarshal(stats, &report); err != nil {
return err
}
r.reports = append(r.reports, report)
return nil
}
func (d *dummyReporter) String() string {
return "dummy"
}
func checkReports(reports []map[string]interface{}, expected [][]int) {
Expect(len(reports)).To(Equal(len(expected)))
for i, expectedVals := range expected {
report := reports[i]
Expect(int(report["try_count"].(float64))).To(Equal(expectedVals[0]))
Expect(int(report["success_count"].(float64))).To(Equal(expectedVals[1]))
}
}
type strAppender struct {
s string
}
func (s *strAppender) Append(msg *loggo.Message) {
s.s += msg.String()
}
var _ = g.Describe("Checker", func() {
g.Describe("StartChecking", func() {
var reporter *dummyReporter
var appender *strAppender
var makeDummyChecker = func(client miniHttpClient, reporter Reporter, runs int, wait time.Duration) *Checker {
logger := loggo.New("logger")
logger.AddAppender(appender, loggo.EmptyFlag)
return NewChecker("foo", reporter, &Options{
HttpClient: client,
CheckInterval: 1 * time.Nanosecond,
PublishInterval: wait,
Logger: logger,
totalRuns: runs,
ExtraFields: map[string]string{"foo": "bar"},
FatalThreshold: 0.5,
})
}
g.BeforeEach(func() {
reporter = &dummyReporter{}
appender = &strAppender{}
})
g.It("should register successes", func() {
checker := makeDummyChecker(&successClient{}, reporter, 1, 1*time.Nanosecond)
checker.StartChecking()
checkReports(reporter.reports, [][]int{[]int{1, 1}})
Expect(reporter.reports[0]["success_ratio"].(float64)).To(BeNumerically(">", 0.0))
Expect(reporter.reports[0]).To(HaveKey("foo"))
})
g.It("should work on server failures", func() {
checker := makeDummyChecker(&failClient{}, reporter, 1, 1*time.Nanosecond)
checker.StartChecking()
checkReports(reporter.reports, [][]int{[]int{1, 0}})
})
g.It("should work on errors", func() {
checker := makeDummyChecker(&errorClient{}, reporter, 1, 1*time.Nanosecond)
checker.StartChecking()
checkReports(reporter.reports, [][]int{[]int{1, 0}})
})
g.It("should send reports only when time is ellapsed", func() {
checker := makeDummyChecker(&successClient{}, reporter, 3, 10*time.Millisecond)
checker.StartChecking()
expected := [][]int{{3, 3}}
checkReports(reporter.reports, expected)
})
g.It("should log fatal when under threshold", func() {
checker := makeDummyChecker(&failClient{}, reporter, 1, 1*time.Nanosecond)
Expect(appender.s).To(BeEmpty())
checker.StartChecking()
Expect(appender.s).NotTo(BeEmpty())
Expect(appender.s).To(ContainSubstring("FATAL"))
})
})
})