forked from Ilhasoft/courier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
healthcheck_test.go
100 lines (72 loc) · 2.64 KB
/
healthcheck_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
package courier
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHealthCheck(t *testing.T) {
t.Run("All Ok", func(t *testing.T) {
healthcheck := NewHealthCheck()
testCheck1 := func() error {
return nil
}
healthcheck.AddCheck("check test1", testCheck1)
healthcheck.AddCheck("check test2", testCheck1)
assert.Equal(t, 2, len(healthcheck.ComponentChecks))
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
healthcheck.CheckUp(ctx)
assert.Equal(t, "Ok", healthcheck.HealthStatus.Status)
hcJSON, err := json.Marshal(healthcheck.HealthStatus)
assert.NoError(t, err)
assert.Equal(t,
"{\"status\":\"Ok\",\"message\":\"All working fine!\",\"details\":{\"check test1\":{\"message\":\"check test1 ok\",\"status\":\"Ok\"},\"check test2\":{\"message\":\"check test2 ok\",\"status\":\"Ok\"}}}",
string(hcJSON),
)
})
t.Run("One Error", func(t *testing.T) {
healthcheck := NewHealthCheck()
testCheck1 := func() error {
return nil
}
testCheck2 := func() error {
return errors.New("Test Error")
}
healthcheck.AddCheck("check test1", testCheck1)
healthcheck.AddCheck("check test2", testCheck2)
assert.Equal(t, 2, len(healthcheck.ComponentChecks))
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
healthcheck.CheckUp(ctx)
assert.Equal(t, "Error", healthcheck.HealthStatus.Status)
hcJSON, err := json.Marshal(healthcheck.HealthStatus)
assert.NoError(t, err)
assert.Equal(t, `{"status":"Error","message":"Test Error","details":{"check test1":{"message":"check test1 ok","status":"Ok"},"check test2":{"message":"Test Error","status":"Error"}}}`, string(hcJSON))
})
t.Run("One TimedOut", func(t *testing.T) {
healthcheck := NewHealthCheck()
testCheck1 := func() error {
return nil
}
testCheck2 := func() error {
time.Sleep(time.Second * 2)
return nil
}
healthcheck.AddCheck("check test1", testCheck1)
healthcheck.AddCheck("check test2", testCheck2)
assert.Equal(t, 2, len(healthcheck.ComponentChecks))
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
healthcheck.CheckUp(ctx)
assert.Equal(t, "Error", healthcheck.HealthStatus.Status)
hcJSON, err := json.Marshal(healthcheck.HealthStatus)
assert.NoError(t, err)
assert.Equal(t,
"{\"status\":\"Error\",\"message\":\"check test2 check is timed out\",\"details\":{\"check test1\":{\"message\":\"check test1 ok\",\"status\":\"Ok\"},\"check test2\":{\"message\":\"check test2 check is timed out\",\"status\":\"Timed Out\"}}}",
string(hcJSON),
)
})
}