-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexclusive_lease_watcher_test.go
More file actions
278 lines (234 loc) · 7.22 KB
/
Copy pathexclusive_lease_watcher_test.go
File metadata and controls
278 lines (234 loc) · 7.22 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package pgoutbox
import (
"context"
"sync/atomic"
"testing"
"time"
)
func TestLeaseWatcher_FiresOnceOnExternalCancel(t *testing.T) {
t.Parallel()
var calls atomic.Int32
w := newLeaseWatcher(func(_ context.Context, topic string) {
if topic != "orders" {
t.Errorf("onCancel called with topic %q, want %q", topic, "orders")
}
calls.Add(1)
})
ctx, cancel := context.WithCancel(context.Background())
w.Start(ctx, "orders")
if got := calls.Load(); got != 0 {
t.Fatalf("onCancel fired before the ctx ended: %d calls", got)
}
cancel()
deadline := time.After(2 * time.Second)
for calls.Load() == 0 {
select {
case <-deadline:
t.Fatal("timed out waiting for onCancel to fire after ctx cancellation")
case <-time.After(5 * time.Millisecond):
}
}
time.Sleep(50 * time.Millisecond)
if got := calls.Load(); got != 1 {
t.Fatalf("onCancel fired %d times, want exactly 1", got)
}
}
func TestLeaseWatcher_OnCancelContextIsDetached(t *testing.T) {
t.Parallel()
fired := make(chan error, 1)
w := newLeaseWatcher(func(ctx context.Context, _ string) {
fired <- ctx.Err()
})
ctx, cancel := context.WithCancel(context.Background())
w.Start(ctx, "orders")
cancel()
select {
case err := <-fired:
if err != nil {
t.Fatalf("onCancel received an already-ended context: %v", err)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for onCancel to fire")
}
}
// TestLeaseWatcher_StopSuppressesOnCancel: a deliberate Stop must not fire
// onCancel — callers stop a watcher exactly when they are about to write
// lease state themselves, and Stop must not block on a database write it
// doesn't need.
func TestLeaseWatcher_StopSuppressesOnCancel(t *testing.T) {
t.Parallel()
var calls atomic.Int32
w := newLeaseWatcher(func(context.Context, string) {
calls.Add(1)
})
w.Start(context.Background(), "orders")
if !w.Stop("orders") {
t.Fatal("Stop did not report stopping the running watcher")
}
time.Sleep(50 * time.Millisecond)
if got := calls.Load(); got != 0 {
t.Fatalf("onCancel fired %d times after a deliberate Stop, want 0", got)
}
}
func TestLeaseWatcher_StopIsNoOpWhenNotRunning(t *testing.T) {
t.Parallel()
w := newLeaseWatcher(func(context.Context, string) {})
// Must return promptly without panicking, even though nothing was started.
done := make(chan struct{})
go func() {
if w.Stop("orders") {
t.Error("Stop reported stopping a watcher that was never started")
}
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Stop hung on a topic that was never started")
}
}
// TestLeaseWatcher_StopBlocksUntilInFlightOnCancelFinishes covers the
// guarantee callers depend on for lease-state ordering: when the watched ctx
// ends just before Stop is called, Stop must not return until the already
// in-flight onCancel has fully finished, so anything the caller writes
// afterwards has the last word.
func TestLeaseWatcher_StopBlocksUntilInFlightOnCancelFinishes(t *testing.T) {
t.Parallel()
entered := make(chan struct{})
release := make(chan struct{})
var stopReturned atomic.Bool
w := newLeaseWatcher(func(_ context.Context, _ string) {
close(entered)
<-release
if stopReturned.Load() {
t.Error("onCancel was still executing after Stop returned")
}
})
ctx, cancel := context.WithCancel(context.Background())
w.Start(ctx, "orders")
// External cancellation puts onCancel in flight before Stop is called.
cancel()
select {
case <-entered:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for onCancel to start")
}
stopDone := make(chan struct{})
go func() {
w.Stop("orders")
stopReturned.Store(true)
close(stopDone)
}()
// Stop must block while onCancel is still running.
select {
case <-stopDone:
t.Fatal("Stop returned before the in-flight onCancel finished")
case <-time.After(100 * time.Millisecond):
}
close(release)
select {
case <-stopDone:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for Stop to return after unblocking onCancel")
}
}
// TestLeaseWatcher_StartRestartsWithoutFiring: restarting a topic stops the
// previous watcher without firing its onCancel; only the final watcher's
// external cancellation fires, exactly once.
func TestLeaseWatcher_StartRestartsWithoutFiring(t *testing.T) {
t.Parallel()
var calls atomic.Int32
w := newLeaseWatcher(func(context.Context, string) {
calls.Add(1)
})
ctx, cancel := context.WithCancel(context.Background())
w.Start(context.Background(), "orders")
for range 5 {
w.Start(context.Background(), "orders")
}
w.Start(ctx, "orders")
if got := calls.Load(); got != 0 {
t.Fatalf("onCancel fired %d times across restarts, want 0", got)
}
cancel()
deadline := time.After(2 * time.Second)
for calls.Load() == 0 {
select {
case <-deadline:
t.Fatal("timed out waiting for the final watcher to fire")
case <-time.After(5 * time.Millisecond):
}
}
time.Sleep(50 * time.Millisecond)
if got := calls.Load(); got != 1 {
t.Fatalf("onCancel fired %d times, want exactly 1 (the final watcher)", got)
}
}
// TestLeaseWatcher_ExternalContextCancelCleansUpRunningMap guards against a
// leak: if a topic's ctx is cancelled externally (not via Stop), the running
// goroutine must remove its own entry from w.running once it exits, so
// running reflects "currently running" rather than "ever started" and Stop
// (or a later Start) doesn't have to be called just to reclaim the memory.
func TestLeaseWatcher_ExternalContextCancelCleansUpRunningMap(t *testing.T) {
t.Parallel()
w := newLeaseWatcher(func(context.Context, string) {})
ctx, cancel := context.WithCancel(context.Background())
w.Start(ctx, "orders")
cancel()
deadline := time.After(2 * time.Second)
for {
if _, ok := w.running.Load("orders"); !ok {
break
}
select {
case <-deadline:
t.Fatal("timed out waiting for the running-map entry to be cleaned up after external cancellation")
case <-time.After(5 * time.Millisecond):
}
}
// Stop must not hang even though the goroutine already exited on its own.
done := make(chan struct{})
go func() {
w.Stop("orders")
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Stop hung after external context cancellation had already stopped the watcher")
}
}
func TestLeaseWatcher_TopicsAreIndependent(t *testing.T) {
t.Parallel()
var ordersCalls, invoicesCalls atomic.Int32
w := newLeaseWatcher(func(_ context.Context, topic string) {
switch topic {
case "orders":
ordersCalls.Add(1)
case "invoices":
invoicesCalls.Add(1)
}
})
ordersCtx, cancelOrders := context.WithCancel(context.Background())
w.Start(ordersCtx, "orders")
w.Start(context.Background(), "invoices")
// Cancelling orders fires only the orders watcher.
cancelOrders()
deadline := time.After(2 * time.Second)
for ordersCalls.Load() == 0 {
select {
case <-deadline:
t.Fatal("timed out waiting for the orders watcher to fire")
case <-time.After(5 * time.Millisecond):
}
}
if got := invoicesCalls.Load(); got != 0 {
t.Fatalf("cancelling orders fired the invoices watcher: %d calls", got)
}
// Stopping invoices fires nothing.
w.Stop("invoices")
time.Sleep(50 * time.Millisecond)
if got := invoicesCalls.Load(); got != 0 {
t.Fatalf("stopping invoices fired its onCancel: %d calls", got)
}
}