@@ -2,6 +2,7 @@ package probes
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"net/http"
6
7
"sync/atomic"
7
8
"testing"
@@ -22,6 +23,8 @@ func TestHealthCheck(t *testing.T) {
22
23
var _ = Describe ("Probes" , func () {
23
24
var (
24
25
ctrl * gomock.Controller
26
+ host = "localhost"
27
+ port = 8080
25
28
pb * Probes
26
29
ctx context.Context
27
30
cancel context.CancelFunc
@@ -35,7 +38,7 @@ var _ = Describe("Probes", func() {
35
38
ctrl = gomock .NewController (GinkgoT ())
36
39
isStarted = & atomic.Value {}
37
40
isStarted .Store (true )
38
- cfg = config.Probes {Port : 6014 }
41
+ cfg = config.Probes {Port : port }
39
42
m = NewMockManager (ctrl )
40
43
m .EXPECT ().GetWorkerNames ().Return ([]string {"worker-a" }).AnyTimes ()
41
44
hc = NewMockHealthChecker (ctrl )
@@ -80,67 +83,72 @@ var _ = Describe("Probes", func() {
80
83
})
81
84
82
85
It ("responds on /startup request" , func () {
86
+ startupURL := fmt .Sprintf ("http://%s:%d/startup" , host , port )
83
87
go pb .Serve (ctx )
84
- time .Sleep (500 * time .Millisecond )
85
88
86
89
// Test success case when app is started
87
- resp , err := http . Get ( "http://localhost:6014/startup" )
90
+ err := waitForProbe ( startupURL , 3 * time . Second )
88
91
Expect (err ).NotTo (HaveOccurred ())
89
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
90
- resp .Body .Close ()
91
92
92
93
// Test failure case when app is not started
93
94
isStarted .Store (false )
94
- resp , err = http .Get ("http://localhost:6014/startup" )
95
- Expect (err ).NotTo (HaveOccurred ())
96
- Expect (resp .StatusCode ).To (Equal (http .StatusServiceUnavailable ))
97
- resp .Body .Close ()
95
+ err = waitForProbe (startupURL , 100 * time .Millisecond )
96
+ Expect (err ).To (HaveOccurred ())
98
97
})
99
98
100
99
It ("responds on /readiness request" , func () {
100
+ readinessURL := fmt .Sprintf ("http://%s:%d/readiness" , host , port )
101
101
go pb .Serve (ctx )
102
- time .Sleep (500 * time .Millisecond )
103
102
104
103
hc .EXPECT ().GetServerState ("worker-a" ).Return (connectivity .Ready )
105
- resp , err := http . Get ( "http://localhost:6014/readiness" )
104
+ err := waitForProbe ( readinessURL , 3 * time . Second )
106
105
Expect (err ).NotTo (HaveOccurred ())
107
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
108
- resp .Body .Close ()
109
106
110
107
downStates := []connectivity.State {
111
108
connectivity .TransientFailure ,
112
109
connectivity .Shutdown ,
113
110
}
114
111
for _ , state := range downStates {
115
112
hc .EXPECT ().GetServerState ("worker-a" ).Return (state )
116
- resp , err := http .Get ("http://localhost:6014/readiness" )
117
- Expect (err ).NotTo (HaveOccurred ())
118
- Expect (resp .StatusCode ).To (Equal (http .StatusServiceUnavailable ))
119
- resp .Body .Close ()
113
+ err = waitForProbe (readinessURL , 100 * time .Millisecond )
114
+ Expect (err ).To (HaveOccurred ())
120
115
}
121
116
})
122
117
123
118
It ("responds on /liveness request" , func () {
119
+ livenessURL := fmt .Sprintf ("http://%s:%d/liveness" , host , port )
124
120
go pb .Serve (ctx )
125
- time .Sleep (500 * time .Millisecond )
126
121
127
- okStates := []connectivity.State {
128
- connectivity .Ready ,
129
- connectivity .TransientFailure ,
130
- }
131
- for _ , state := range okStates {
132
- hc .EXPECT ().GetServerState ("worker-a" ).Return (state )
133
- resp , err := http .Get ("http://localhost:6014/liveness" )
134
- Expect (err ).NotTo (HaveOccurred ())
135
- Expect (resp .StatusCode ).To (Equal (http .StatusOK ))
136
- resp .Body .Close ()
137
- }
122
+ hc .EXPECT ().GetServerState ("worker-a" ).Return (connectivity .Ready )
123
+ err := waitForProbe (livenessURL , 3 * time .Second )
124
+ Expect (err ).NotTo (HaveOccurred ())
138
125
139
- hc .EXPECT ().GetServerState ("worker-a" ).Return (connectivity .Shutdown )
140
- resp , err := http . Get ( "http://localhost:6014/liveness" )
126
+ hc .EXPECT ().GetServerState ("worker-a" ).Return (connectivity .TransientFailure )
127
+ err = waitForProbe ( livenessURL , 100 * time . Millisecond )
141
128
Expect (err ).NotTo (HaveOccurred ())
142
- Expect (resp .StatusCode ).To (Equal (http .StatusServiceUnavailable ))
143
- resp .Body .Close ()
129
+
130
+ hc .EXPECT ().GetServerState ("worker-a" ).Return (connectivity .Shutdown )
131
+ err = waitForProbe (livenessURL , 100 * time .Millisecond )
132
+ Expect (err ).To (HaveOccurred ())
144
133
})
145
134
})
146
135
})
136
+
137
+ func waitForProbe (url string , timeout time.Duration ) error {
138
+ deadline := time .Now ().Add (timeout )
139
+ var lastErr error
140
+ for time .Now ().Before (deadline ) {
141
+ resp , err := http .Get (url )
142
+ if err == nil {
143
+ resp .Body .Close ()
144
+ if resp .StatusCode == http .StatusOK {
145
+ return nil
146
+ }
147
+ lastErr = fmt .Errorf ("status: %d" , resp .StatusCode )
148
+ } else {
149
+ lastErr = err
150
+ }
151
+ time .Sleep (100 * time .Millisecond )
152
+ }
153
+ return fmt .Errorf ("probe %s not ready: %v" , url , lastErr )
154
+ }
0 commit comments