forked from zknill/service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
probes_test.go
59 lines (41 loc) · 1.19 KB
/
probes_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
package service_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Attest/service"
)
func TestProbes(t *testing.T) {
g := service.NewCtx(context.Background())
releaseSetup := make(chan struct{})
g.Setup(func(ctx context.Context) error {
<-releaseSetup
return nil
})
verifyStatusCode(t, "live", g.Liveness(), http.StatusServiceUnavailable)
verifyStatusCode(t, "ready", g.Readiness(), http.StatusServiceUnavailable)
go func() { _ = g.Start() }()
verifyStatusCode(t, "live", g.Liveness(), http.StatusOK)
verifyStatusCode(t, "ready", g.Readiness(), http.StatusServiceUnavailable)
close(releaseSetup)
verifyStatusCode(t, "live", g.Liveness(), http.StatusOK)
verifyStatusCode(t, "ready", g.Readiness(), http.StatusOK)
}
func verifyStatusCode(t *testing.T, handlerName string, h http.Handler, status int) {
t.Helper()
var got *http.Response
for i := 0; i < 3; i++ {
w := httptest.NewRecorder()
r := &http.Request{}
h.ServeHTTP(w, r)
got = w.Result()
if got.StatusCode == status {
return
}
d := 200 * time.Millisecond
<-time.After(d * time.Duration(i+1))
}
t.Errorf("%q expected %d, got: %d", handlerName, status, got.StatusCode)
}