-
Notifications
You must be signed in to change notification settings - Fork 6
/
healthcheck_test.go
108 lines (95 loc) · 2.6 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
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"testing"
"time"
gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/utilitywarehouse/wiresteward/mocks"
)
func TestHealthcheck_NewHealthCheck(t *testing.T) {
hc, err := newHealthCheck(
"test-dev",
"10.0.0.0",
Duration{10 * time.Second},
Duration{time.Second},
Duration{time.Second},
3,
make(chan struct{}))
if err != nil {
t.Fatal(err)
}
// Assert init healthcheck conditions
assert.Equal(t, hc.healthy, false)
assert.Equal(t, hc.running, false)
}
func TestHealthcheck_RunConsecutiveFails(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChecker := mocks.NewMockchecker(ctrl)
renewNotify := make(chan struct{})
hc, err := newHealthCheck(
"test-dev",
"10.0.0.0",
Duration{100 * time.Millisecond},
Duration{10 * time.Millisecond},
Duration{time.Second},
3,
renewNotify)
if err != nil {
t.Fatal(err)
}
hc.checker = mockChecker
mockChecker.EXPECT().TargetIP().Return("10.0.0.0").AnyTimes()
// Check that exactly 3 failed checks will mark the health check as
// failed. If there are calls registered but missing, or the timeout
// expires the test will fail.
mockChecker.EXPECT().Check().Return(fmt.Errorf("unhealthy")).Times(3)
go hc.Run()
defer hc.Stop()
timeoutTicker := time.NewTicker(1 * time.Second)
defer timeoutTicker.Stop()
select {
case <-renewNotify:
break
case <-timeoutTicker.C:
t.Fatal(fmt.Errorf("timeout"))
}
}
func TestHealthcheck_RunHealthyCheckResets(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockChecker := mocks.NewMockchecker(ctrl)
renewNotify := make(chan struct{})
hc, err := newHealthCheck(
"test-dev",
"10.0.0.0",
Duration{100 * time.Millisecond},
Duration{10 * time.Millisecond},
Duration{time.Second},
3,
renewNotify)
if err != nil {
t.Fatal(err)
}
hc.checker = mockChecker
mockChecker.EXPECT().TargetIP().Return("10.0.0.0").AnyTimes()
// Check that a healthy check after 2 consecutive failed attempts will
// reset the health check and thus it will require 3 more failures in
// a row to mark the target as failed.
// If there are calls registered but missing, or the timeout
// expires the test will fail.
mockChecker.EXPECT().Check().Return(fmt.Errorf("unhealthy")).Times(2)
mockChecker.EXPECT().Check().Return(nil).Times(1)
mockChecker.EXPECT().Check().Return(fmt.Errorf("unhealthy")).Times(3)
go hc.Run()
defer hc.Stop()
timeoutTicker := time.NewTicker(1 * time.Second)
defer timeoutTicker.Stop()
select {
case <-renewNotify:
break
case <-timeoutTicker.C:
t.Fatal(fmt.Errorf("timeout"))
}
}