-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
150 lines (131 loc) · 3.69 KB
/
Copy pathmain_test.go
File metadata and controls
150 lines (131 loc) · 3.69 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestHealthCheckerIsHealthy(t *testing.T) {
startedAt := time.Date(2026, time.March, 20, 12, 0, 0, 0, time.UTC)
testCases := []struct {
name string
now time.Time
healthy bool
}{
{
name: "healthy before first failure window",
now: startedAt.Add(29*time.Minute + 59*time.Second),
healthy: true,
},
{
name: "unhealthy at first failure start",
now: startedAt.Add(30 * time.Minute),
healthy: false,
},
{
name: "unhealthy within first failure window",
now: startedAt.Add(37 * time.Minute),
healthy: false,
},
{
name: "healthy after first failure window",
now: startedAt.Add(40 * time.Minute),
healthy: true,
},
{
name: "unhealthy in later failure window",
now: startedAt.Add(92 * time.Minute),
healthy: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
checker := newHealthChecker(startedAt)
checker.now = func() time.Time { return testCase.now }
if got := checker.isHealthy(); got != testCase.healthy {
t.Fatalf("isHealthy() = %v, want %v", got, testCase.healthy)
}
})
}
}
func TestHealthServer(t *testing.T) {
startedAt := time.Date(2026, time.March, 20, 12, 0, 0, 0, time.UTC)
checker := newHealthChecker(startedAt)
checker.now = func() time.Time { return startedAt.Add(35 * time.Minute) }
request := httptest.NewRequest(http.MethodGet, "/healthz", nil)
recorder := httptest.NewRecorder()
HealthServer(checker).ServeHTTP(recorder, request)
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("status code = %d, want %d", recorder.Code, http.StatusServiceUnavailable)
}
}
func TestReadinessCheckerIsReady(t *testing.T) {
startedAt := time.Date(2026, time.March, 20, 12, 0, 0, 0, time.UTC)
testCases := []struct {
name string
now time.Time
ready bool
}{
{
name: "not ready before threshold",
now: startedAt.Add(119 * time.Second),
ready: false,
},
{
name: "ready at threshold",
now: startedAt.Add(2 * time.Minute),
ready: true,
},
{
name: "ready after threshold",
now: startedAt.Add(3 * time.Minute),
ready: true,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
checker := newReadinessChecker(startedAt)
checker.now = func() time.Time { return testCase.now }
if got := checker.isReady(); got != testCase.ready {
t.Fatalf("isReady() = %v, want %v", got, testCase.ready)
}
})
}
}
func TestReadyServer(t *testing.T) {
startedAt := time.Date(2026, time.March, 20, 12, 0, 0, 0, time.UTC)
testCases := []struct {
name string
now time.Time
wantStatus int
wantBody string
}{
{
name: "returns not ready before threshold",
now: startedAt.Add(90 * time.Second),
wantStatus: http.StatusServiceUnavailable,
wantBody: "not ready\n",
},
{
name: "returns ready after threshold",
now: startedAt.Add(2*time.Minute + 1*time.Second),
wantStatus: http.StatusOK,
wantBody: "ready\n",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
checker := newReadinessChecker(startedAt)
checker.now = func() time.Time { return testCase.now }
request := httptest.NewRequest(http.MethodGet, "/ready", nil)
recorder := httptest.NewRecorder()
ReadyServer(checker).ServeHTTP(recorder, request)
if recorder.Code != testCase.wantStatus {
t.Fatalf("status code = %d, want %d", recorder.Code, testCase.wantStatus)
}
if recorder.Body.String() != testCase.wantBody {
t.Fatalf("body = %q, want %q", recorder.Body.String(), testCase.wantBody)
}
})
}
}