-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhc_test.go
71 lines (63 loc) Β· 1.54 KB
/
hc_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
package hc
import (
"context"
"errors"
"reflect"
"testing"
)
func TestMultiChecker_Health(t *testing.T) {
errTest := errors.New("test error")
type tcase struct {
checkers []HealthChecker
want error
}
tests := map[string]tcase{
"Nil checkers": {
checkers: nil,
want: nil,
},
"Nil error": {
checkers: []HealthChecker{
&testChecker{HealthFunc: func(ctx context.Context) error { return nil }},
&testChecker{HealthFunc: func(ctx context.Context) error { return nil }},
},
want: nil,
},
"Non nil error": {
checkers: []HealthChecker{
&testChecker{HealthFunc: func(ctx context.Context) error { return errTest }},
&testChecker{HealthFunc: func(ctx context.Context) error { return nil }},
},
want: errTest,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
c := NewMultiChecker(tc.checkers...)
if err := c.Health(context.Background()); err != tc.want {
t.Errorf("Health() error := %v, want := %v", err, tc.want)
}
})
}
}
func TestMultiChecker_Add(t *testing.T) {
mc := NewMultiChecker()
tc := &testChecker{HealthFunc: func(ctx context.Context) error { return nil }}
mc.Add(tc)
if len(mc.hcs) != 1 {
t.Errorf("expected len := 1, got := %d", len(mc.hcs))
}
expTC := mc.hcs[0]
if !reflect.DeepEqual(expTC, tc) {
t.Errorf("expected := %v, got := %v", expTC, tc)
}
}
type testChecker struct {
HealthFunc func(ctx context.Context) error
}
func (c *testChecker) Health(ctx context.Context) error {
if c.HealthFunc == nil {
return nil
}
return c.HealthFunc(ctx)
}