-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhealth_test.go
74 lines (54 loc) · 1.52 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
package health_test
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dotse/go-health"
)
func Example() {
ctx := context.Background()
// Register an instance of some type that implements HealthChecker:
m := new(MyTypeWithHealthCheck)
health.Register(ctx, "mytype", m)
// Register a function:
r := health.RegisterFunc(ctx, "func", func(context.Context) (checks []health.Check) {
// Checkers can return any number of checks.
for i := 0; i < 3; i++ {
var check health.Check
// Make the relevant changes to `check` here, most importantly
// `check.Status`.
checks = append(checks, check)
}
return checks
})
defer r.Deregister()
}
func TestCheckerFunc_LogValue(t *testing.T) {
t.Parallel()
f := health.CheckerFunc(func(context.Context) []health.Check { return nil })
assert.Regexp(t, `^func\(0x[\da-f]+\)`, f.LogValue().String())
}
func TestReadResponse(t *testing.T) {
t.Parallel()
r := strings.NewReader(`{ "status": "pass" }`)
resp, err := health.ReadResponse(r)
assert.NoError(t, err)
require.NotNil(t, resp)
assert.EqualValues(t, health.StatusPass, resp.Status)
}
func TestResponse_Write(t *testing.T) {
t.Parallel()
var (
b strings.Builder
resp health.Response
)
_, err := resp.Write(&b)
require.NoError(t, err)
assert.JSONEq(t, `{"status":"pass"}`, b.String())
}
type MyTypeWithHealthCheck struct{}
func (*MyTypeWithHealthCheck) CheckHealth(context.Context) []health.Check {
return []health.Check{{}}
}