-
Notifications
You must be signed in to change notification settings - Fork 46
/
health_test.go
124 lines (103 loc) · 2.82 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package health
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
type testSink struct {
LastEmitKind string // "Event", "EventErr", ..., "Complete"
LastJob string
LastEvent string
LastErr error
LastErrEmitted bool
LastErrUnmuted bool
LastErrMuted bool
LastErrRaw bool
LastNanos int64
LastValue float64
LastKvs map[string]string
LastStatus CompletionStatus
}
func (s *testSink) EmitEvent(job string, event string, kvs map[string]string) {
s.LastEmitKind = "Event"
s.LastJob = job
s.LastEvent = event
s.LastKvs = kvs
}
func (s *testSink) EmitEventErr(job string, event string, inputErr error, kvs map[string]string) {
s.LastEmitKind = "EventErr"
s.LastJob = job
s.LastEvent = event
s.LastKvs = kvs
s.LastErr = inputErr
switch inputErr := inputErr.(type) {
case *UnmutedError:
s.LastErrUnmuted = true
s.LastErrEmitted = inputErr.Emitted
case *MutedError:
s.LastErrMuted = true
default: // eg, case error:
s.LastErrRaw = true
}
}
func (s *testSink) EmitTiming(job string, event string, nanos int64, kvs map[string]string) {
s.LastEmitKind = "Timing"
s.LastJob = job
s.LastEvent = event
s.LastKvs = kvs
s.LastNanos = nanos
}
func (s *testSink) EmitGauge(job string, event string, value float64, kvs map[string]string) {
s.LastEmitKind = "Gauge"
s.LastJob = job
s.LastEvent = event
s.LastKvs = kvs
s.LastValue = value
}
func (s *testSink) EmitComplete(job string, status CompletionStatus, nanos int64, kvs map[string]string) {
s.LastEmitKind = "Complete"
s.LastJob = job
s.LastKvs = kvs
s.LastNanos = nanos
s.LastStatus = status
}
func successFunc() error {
return nil
}
func errorFunc() error {
return errors.New("sad_day")
}
func panicFunc() error {
panic("wat")
return nil
}
func TestRun(t *testing.T) {
s := NewStream()
ts := &testSink{}
s.AddSink(ts)
err := s.Run("foo", successFunc)
assert.NoError(t, err)
assert.Equal(t, "Complete", ts.LastEmitKind)
assert.Equal(t, "foo", ts.LastJob)
assert.Equal(t, Success, ts.LastStatus)
err = s.Run("foo", errorFunc)
assert.Equal(t, "sad_day", err.Error())
assert.Equal(t, "Complete", ts.LastEmitKind)
assert.Equal(t, "foo", ts.LastJob)
assert.Equal(t, Error, ts.LastStatus)
err = s.Run("foo", panicFunc)
assert.Equal(t, "wat", err.Error())
assert.Equal(t, "Complete", ts.LastEmitKind)
assert.Equal(t, "foo", ts.LastJob)
assert.Equal(t, Panic, ts.LastStatus)
// Panicing will fire an EventErr and then a Complete(Panic)
// This test relies on the fact that LastErr isn't cleared when a Complete comes in
assert.Equal(t, "wat", ts.LastErr.Error())
// Now just make sure that job also has a similar Run function:
j := s.NewJob("bob")
err = j.Run(successFunc)
assert.NoError(t, err)
assert.Equal(t, "Complete", ts.LastEmitKind)
assert.Equal(t, "bob", ts.LastJob)
assert.Equal(t, Success, ts.LastStatus)
}