Skip to content

Commit a3ef12e

Browse files
committed
tests: fix flaky tests
1 parent e3d6e7e commit a3ef12e

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

internal/probes/probes_test.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package probes
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"sync/atomic"
78
"testing"
@@ -22,6 +23,8 @@ func TestHealthCheck(t *testing.T) {
2223
var _ = Describe("Probes", func() {
2324
var (
2425
ctrl *gomock.Controller
26+
host = "localhost"
27+
port = 8080
2528
pb *Probes
2629
ctx context.Context
2730
cancel context.CancelFunc
@@ -35,7 +38,7 @@ var _ = Describe("Probes", func() {
3538
ctrl = gomock.NewController(GinkgoT())
3639
isStarted = &atomic.Value{}
3740
isStarted.Store(true)
38-
cfg = config.Probes{Port: 6014}
41+
cfg = config.Probes{Port: port}
3942
m = NewMockManager(ctrl)
4043
m.EXPECT().GetWorkerNames().Return([]string{"worker-a"}).AnyTimes()
4144
hc = NewMockHealthChecker(ctrl)
@@ -80,67 +83,72 @@ var _ = Describe("Probes", func() {
8083
})
8184

8285
It("responds on /startup request", func() {
86+
startupURL := fmt.Sprintf("http://%s:%d/startup", host, port)
8387
go pb.Serve(ctx)
84-
time.Sleep(500 * time.Millisecond)
8588

8689
// Test success case when app is started
87-
resp, err := http.Get("http://localhost:6014/startup")
90+
err := waitForProbe(startupURL, 3*time.Second)
8891
Expect(err).NotTo(HaveOccurred())
89-
Expect(resp.StatusCode).To(Equal(http.StatusOK))
90-
resp.Body.Close()
9192

9293
// Test failure case when app is not started
9394
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())
9897
})
9998

10099
It("responds on /readiness request", func() {
100+
readinessURL := fmt.Sprintf("http://%s:%d/readiness", host, port)
101101
go pb.Serve(ctx)
102-
time.Sleep(500 * time.Millisecond)
103102

104103
hc.EXPECT().GetServerState("worker-a").Return(connectivity.Ready)
105-
resp, err := http.Get("http://localhost:6014/readiness")
104+
err := waitForProbe(readinessURL, 3*time.Second)
106105
Expect(err).NotTo(HaveOccurred())
107-
Expect(resp.StatusCode).To(Equal(http.StatusOK))
108-
resp.Body.Close()
109106

110107
downStates := []connectivity.State{
111108
connectivity.TransientFailure,
112109
connectivity.Shutdown,
113110
}
114111
for _, state := range downStates {
115112
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())
120115
}
121116
})
122117

123118
It("responds on /liveness request", func() {
119+
livenessURL := fmt.Sprintf("http://%s:%d/liveness", host, port)
124120
go pb.Serve(ctx)
125-
time.Sleep(500 * time.Millisecond)
126121

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())
138125

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)
141128
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())
144133
})
145134
})
146135
})
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

Comments
 (0)