-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathheartbeat_test.go
More file actions
256 lines (213 loc) · 7.58 KB
/
heartbeat_test.go
File metadata and controls
256 lines (213 loc) · 7.58 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package litestream_test
import (
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"sync/atomic"
"testing"
"time"
"github.com/benbjohnson/litestream"
"github.com/benbjohnson/litestream/internal/testingutil"
)
func TestHeartbeatClient_Ping(t *testing.T) {
t.Run("Success", func(t *testing.T) {
var pingCount atomic.Int64
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
pingCount.Add(1)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := litestream.NewHeartbeatClient(server.URL, 5*time.Minute)
if err := client.Ping(context.Background()); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got := pingCount.Load(); got != 1 {
t.Errorf("expected 1 ping, got %d", got)
}
})
t.Run("EmptyURL", func(t *testing.T) {
client := litestream.NewHeartbeatClient("", 5*time.Minute)
if err := client.Ping(context.Background()); err != nil {
t.Fatalf("expected no error for empty URL, got %v", err)
}
})
t.Run("NonSuccessStatusCode", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
client := litestream.NewHeartbeatClient(server.URL, 5*time.Minute)
err := client.Ping(context.Background())
if err == nil {
t.Fatal("expected error for 500 status code")
}
})
t.Run("NetworkError", func(t *testing.T) {
client := litestream.NewHeartbeatClient("http://localhost:1", 5*time.Minute)
err := client.Ping(context.Background())
if err == nil {
t.Fatal("expected error for unreachable server")
}
})
t.Run("ContextCanceled", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
ctx, cancel := context.WithCancel(context.Background())
cancel()
client := litestream.NewHeartbeatClient(server.URL, 5*time.Minute)
err := client.Ping(ctx)
if err == nil {
t.Fatal("expected error for canceled context")
}
})
}
func TestHeartbeatClient_ShouldPing(t *testing.T) {
t.Run("FirstPing", func(t *testing.T) {
client := litestream.NewHeartbeatClient("http://example.com", 5*time.Minute)
if !client.ShouldPing() {
t.Error("expected ShouldPing to return true for first ping")
}
})
t.Run("AfterRecordPing", func(t *testing.T) {
client := litestream.NewHeartbeatClient("http://example.com", 5*time.Minute)
client.RecordPing()
if client.ShouldPing() {
t.Error("expected ShouldPing to return false immediately after RecordPing")
}
})
}
func TestHeartbeatClient_MinInterval(t *testing.T) {
client := litestream.NewHeartbeatClient("http://example.com", 30*time.Second)
if client.Interval != litestream.MinHeartbeatInterval {
t.Errorf("expected interval to be clamped to %v, got %v", litestream.MinHeartbeatInterval, client.Interval)
}
}
func TestHeartbeatClient_LastPingAt(t *testing.T) {
client := litestream.NewHeartbeatClient("http://example.com", 5*time.Minute)
if !client.LastPingAt().IsZero() {
t.Error("expected LastPingAt to be zero initially")
}
before := time.Now()
client.RecordPing()
after := time.Now()
lastPing := client.LastPingAt()
if lastPing.Before(before) || lastPing.After(after) {
t.Errorf("LastPingAt %v should be between %v and %v", lastPing, before, after)
}
}
func TestStore_Heartbeat_AllDatabasesHealthy(t *testing.T) {
t.Run("NoDatabases", func(t *testing.T) {
levels := litestream.CompactionLevels{{Level: 0}}
store := litestream.NewStore(nil, levels)
store.CompactionMonitorEnabled = false
store.HeartbeatCheckInterval = 0 // Disable automatic monitoring
var pingCount atomic.Int64
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
pingCount.Add(1)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
store.Heartbeat = litestream.NewHeartbeatClient(server.URL, 1*time.Minute)
// With no databases, heartbeat should not fire
// We need to trigger the check manually since monitor is disabled
// The store won't send pings because allDatabasesHealthy returns false for empty stores
if pingCount.Load() != 0 {
t.Errorf("expected no pings with no databases, got %d", pingCount.Load())
}
})
t.Run("AllDatabasesSynced", func(t *testing.T) {
db0, sqldb0 := testingutil.MustOpenDBs(t)
defer testingutil.MustCloseDBs(t, db0, sqldb0)
db1, sqldb1 := testingutil.MustOpenDBs(t)
defer testingutil.MustCloseDBs(t, db1, sqldb1)
levels := litestream.CompactionLevels{{Level: 0}, {Level: 1, Interval: time.Second}}
store := litestream.NewStore([]*litestream.DB{db0, db1}, levels)
store.CompactionMonitorEnabled = false
store.HeartbeatCheckInterval = 0
var pingCount atomic.Int64
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
pingCount.Add(1)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
store.Heartbeat = litestream.NewHeartbeatClient(server.URL, 1*time.Minute)
if err := store.Open(t.Context()); err != nil {
t.Fatalf("open store: %v", err)
}
defer store.Close(t.Context())
// Create tables and sync both databases
if _, err := sqldb0.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil {
t.Fatal(err)
}
if err := db0.Sync(t.Context()); err != nil {
t.Fatal(err)
}
if err := db0.Replica.Sync(t.Context()); err != nil {
t.Fatal(err)
}
if _, err := sqldb1.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil {
t.Fatal(err)
}
if err := db1.Sync(t.Context()); err != nil {
t.Fatal(err)
}
if err := db1.Replica.Sync(t.Context()); err != nil {
t.Fatal(err)
}
// Both databases have synced, heartbeat should fire
if err := store.Heartbeat.Ping(t.Context()); err != nil {
t.Fatalf("ping failed: %v", err)
}
if got := pingCount.Load(); got != 1 {
t.Errorf("expected 1 ping after all DBs synced, got %d", got)
}
})
t.Run("OneDatabaseNotSynced", func(t *testing.T) {
db0, sqldb0 := testingutil.MustOpenDBs(t)
defer testingutil.MustCloseDBs(t, db0, sqldb0)
// Create second DB but don't sync it
db1 := litestream.NewDB(filepath.Join(t.TempDir(), "db1"))
db1.Replica = litestream.NewReplica(db1)
db1.Replica.Client = testingutil.NewFileReplicaClient(t)
db1.Replica.MonitorEnabled = false
db1.MonitorInterval = 0
levels := litestream.CompactionLevels{{Level: 0}, {Level: 1, Interval: time.Second}}
store := litestream.NewStore([]*litestream.DB{db0, db1}, levels)
store.CompactionMonitorEnabled = false
store.HeartbeatCheckInterval = 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
store.Heartbeat = litestream.NewHeartbeatClient(server.URL, 1*time.Minute)
if err := store.Open(t.Context()); err != nil {
t.Fatalf("open store: %v", err)
}
defer store.Close(t.Context())
// Only sync db0
if _, err := sqldb0.ExecContext(t.Context(), `CREATE TABLE t (id INT)`); err != nil {
t.Fatal(err)
}
if err := db0.Sync(t.Context()); err != nil {
t.Fatal(err)
}
if err := db0.Replica.Sync(t.Context()); err != nil {
t.Fatal(err)
}
// db1 hasn't synced, so LastSuccessfulSyncAt should be zero
if !db1.LastSuccessfulSyncAt().IsZero() {
t.Error("expected db1.LastSuccessfulSyncAt to be zero")
}
// db0 has synced
if db0.LastSuccessfulSyncAt().IsZero() {
t.Error("expected db0.LastSuccessfulSyncAt to be non-zero")
}
})
}