-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhealth_test.go
More file actions
100 lines (85 loc) · 2.23 KB
/
health_test.go
File metadata and controls
100 lines (85 loc) · 2.23 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
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/rs/zerolog"
"github.com/evstack/apex/pkg/types"
)
type mockStatusProvider struct {
status types.SyncStatus
}
func (m *mockStatusProvider) Status() types.SyncStatus {
return m.status
}
func TestHealthEndpoint(t *testing.T) {
tests := []struct {
name string
state types.SyncState
wantCode int
wantHealth bool
}{
{
name: "streaming is healthy",
state: types.Streaming,
wantCode: http.StatusOK,
wantHealth: true,
},
{
name: "backfilling is healthy",
state: types.Backfilling,
wantCode: http.StatusOK,
wantHealth: true,
},
{
name: "initializing is unhealthy",
state: types.Initializing,
wantCode: http.StatusServiceUnavailable,
wantHealth: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sp := &mockStatusProvider{status: types.SyncStatus{
State: tt.state,
LatestHeight: 100,
NetworkHeight: 105,
}}
notifier := NewNotifier(64, 1024, zerolog.Nop())
h := NewHealthHandler(sp, newMockStore(), notifier, "test")
mux := http.NewServeMux()
h.Register(mux)
req := httptest.NewRequest(http.MethodGet, "/health", nil) //nolint:noctx
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != tt.wantCode {
t.Errorf("status code = %d, want %d", rec.Code, tt.wantCode)
}
var hs HealthStatus
if err := json.NewDecoder(rec.Body).Decode(&hs); err != nil {
t.Fatalf("decode response: %v", err)
}
if hs.Healthy != tt.wantHealth {
t.Errorf("healthy = %v, want %v", hs.Healthy, tt.wantHealth)
}
})
}
}
func TestReadyEndpoint(t *testing.T) {
sp := &mockStatusProvider{status: types.SyncStatus{
State: types.Streaming,
LatestHeight: 100,
NetworkHeight: 100,
}}
notifier := NewNotifier(64, 1024, zerolog.Nop())
h := NewHealthHandler(sp, newMockStore(), notifier, "test")
mux := http.NewServeMux()
h.Register(mux)
req := httptest.NewRequest(http.MethodGet, "/health/ready", nil) //nolint:noctx
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("status code = %d, want 200", rec.Code)
}
}