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
|
package operations
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"aigw/internal/billing"
"aigw/internal/controlplane"
)
type healthyStore struct{}
func (healthyStore) Ping(context.Context) error { return nil }
func (healthyStore) MailQueueStatus(context.Context) (controlplane.MailQueueStatus, error) {
return controlplane.MailQueueStatus{}, nil
}
type slowBilling struct{}
func (slowBilling) Ping(context.Context) error { return nil }
func (slowBilling) SettlementQueueStatus(context.Context) (billing.SettlementQueueStatus, error) {
return billing.SettlementQueueStatus{}, nil
}
func (slowBilling) OperationalStatus(ctx context.Context) (billing.OperationalStatus, error) {
<-ctx.Done()
return billing.OperationalStatus{}, ctx.Err()
}
func TestReadinessTimeoutDoesNotMislabelCompletedChecks(t *testing.T) {
handler := Handler{Store: healthyStore{}, Billing: slowBilling{}, ReadinessTimeout: 20 * time.Millisecond}
request := httptest.NewRequest(http.MethodGet, "/readyz", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503", recorder.Code)
}
var payload struct {
Checks map[string]struct {
Status string `json:"status"`
Error string `json:"error"`
} `json:"checks"`
}
if err := json.NewDecoder(recorder.Body).Decode(&payload); err != nil {
t.Fatal(err)
}
for _, name := range []string{"postgres", "mail_queue", "billing_postgres", "settlement_queue"} {
if payload.Checks[name].Status != "ok" {
t.Fatalf("%s status = %q, want ok; payload=%s", name, payload.Checks[name].Status, recorder.Body.String())
}
}
if payload.Checks["billing_operations"].Status != "failed" || payload.Checks["billing_operations"].Error != "check timed out" {
t.Fatalf("billing operations = %+v", payload.Checks["billing_operations"])
}
}
func TestMailQueueIsCheckedWithoutBilling(t *testing.T) {
handler := Handler{Store: healthyStore{}, ReadinessTimeout: time.Second}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/readyz", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, body=%s", recorder.Code, recorder.Body.String())
}
var payload struct {
Checks map[string]any `json:"checks"`
}
if err := json.NewDecoder(recorder.Body).Decode(&payload); err != nil {
t.Fatal(err)
}
if _, ok := payload.Checks["mail_queue"]; !ok {
t.Fatal("mail_queue check missing when billing is disabled")
}
}
func TestReadinessChecksDoNotInheritCanceledClientContext(t *testing.T) {
handler := Handler{Store: healthyStore{}, ReadinessTimeout: time.Second}
request := httptest.NewRequest(http.MethodGet, "/readyz", nil)
ctx, cancel := context.WithCancel(request.Context())
cancel()
request = request.WithContext(ctx)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, body=%s", recorder.Code, recorder.Body.String())
}
}
|