Repository navigation
Expand file tree
/
Copy pathnotification_test.go
More file actions
453 lines (424 loc) · 17.3 KB
/
Copy pathnotification_test.go
File metadata and controls
453 lines (424 loc) · 17.3 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package flow
import (
"context"
"errors"
"testing"
"time"
"github.com/goware/flow/internal/canonical"
"github.com/goware/flow/internal/failure"
"github.com/goware/flow/internal/fault"
"github.com/goware/flow/internal/pgschema"
retrypolicy "github.com/goware/flow/internal/retry"
"github.com/goware/flow/internal/store"
"github.com/goware/flow/internal/testpg"
"github.com/jackc/pgx/v5"
)
func TestNotificationHintsCommitButDoNotRollback(t *testing.T) {
t.Parallel()
database := testpg.Open(t)
ctx := context.Background()
if err := Migrate(ctx, database.DB, WithSchema(database.Schema)); err != nil {
t.Fatalf("Migrate() error = %v", err)
}
runtime, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true))
if err != nil {
t.Fatalf("New() error = %v", err)
}
command := DefineCommand[runtimeArgs, runtimeResult]("notify.commit", 1)
listener, err := pgx.ConnectConfig(ctx, database.DB.Conn.Config().ConnConfig)
if err != nil {
t.Fatalf("connect listener: %v", err)
}
defer listener.Close(context.Background())
channel := runtime.store.NotificationChannel()
if _, err := listener.Exec(ctx, "LISTEN "+pgx.Identifier{channel}.Sanitize()); err != nil {
t.Fatalf("LISTEN: %v", err)
}
tx, err := database.DB.Conn.Begin(ctx)
if err != nil {
t.Fatalf("Begin(commit) error = %v", err)
}
exec, err := command.Enqueue(ctx, runtime.InTx(tx), "notify/commit", runtimeArgs{})
if err != nil {
_ = tx.Rollback(ctx)
t.Fatalf("Enqueue(commit) error = %v", err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
if err := tx.Commit(ctx); err != nil {
t.Fatalf("Commit() error = %v", err)
}
notifyCtx, cancelNotify := context.WithTimeout(ctx, 2*time.Second)
notification, err := listener.WaitForNotification(notifyCtx)
cancelNotify()
if err != nil {
t.Fatalf("WaitForNotification(commit) error = %v", err)
}
hint, valid := store.ParseNotificationHint(notification.Payload)
if !valid || hint.RunID.String() != string(exec.RunID) || hint.Kind != store.NotificationRun || notification.Channel != channel {
t.Fatalf("notification = %#v, parsed=%#v/%t", notification, hint, valid)
}
if len(notification.Payload) > 128 {
t.Fatalf("notification payload contains more than a bounded identity hint: %q", notification.Payload)
}
tx, err = database.DB.Conn.Begin(ctx)
if err != nil {
t.Fatalf("Begin() error = %v", err)
}
if _, err := command.Enqueue(ctx, runtime.InTx(tx), "notify/rollback", runtimeArgs{}); err != nil {
_ = tx.Rollback(ctx)
t.Fatalf("Enqueue(rollback) error = %v", err)
}
if err := tx.Rollback(ctx); err != nil {
t.Fatalf("Rollback() error = %v", err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
}
func TestReplaceCurrentRunNotifiesOnlyForCommittedRunnableSuccessor(t *testing.T) {
t.Parallel()
database := testpg.Open(t)
ctx := context.Background()
if err := Migrate(ctx, database.DB, WithSchema(database.Schema)); err != nil {
t.Fatal(err)
}
runtime, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true))
if err != nil {
t.Fatal(err)
}
command := DefineCommand[None, None]("notify.replace", 1)
listener := openNotificationListener(t, database, runtime)
original, err := command.Enqueue(ctx, runtime, "notify/replace", None{}, WithLiveKey(), WithStartDelay(time.Hour))
if err != nil {
t.Fatal(err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
replaced, err := command.ReplaceCurrentRun(ctx, runtime, original.RunID, "notify/replace", None{}, "runnable replacement", WithLiveKey())
if err != nil || !replaced.Replaced {
t.Fatalf("replacement = %#v, %v", replaced, err)
}
waitForNotificationHintKind(t, listener, replaced.RunID, store.NotificationRun, 2*time.Second)
rolledBackOriginal, err := command.Enqueue(ctx, runtime, "notify/replace-rollback", None{}, WithLiveKey(), WithStartDelay(time.Hour))
if err != nil {
t.Fatal(err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
tx, err := database.DB.Conn.Begin(ctx)
if err != nil {
t.Fatal(err)
}
if _, err := command.ReplaceCurrentRun(ctx, runtime.InTx(tx), rolledBackOriginal.RunID,
"notify/replace-rollback", None{}, "rolled back replacement", WithLiveKey()); err != nil {
_ = tx.Rollback(ctx)
t.Fatal(err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
if err := tx.Rollback(ctx); err != nil {
t.Fatal(err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
}
func TestNotificationHintsForRunnableAndApplicationEventTransitions(t *testing.T) {
t.Parallel()
database := testpg.Open(t)
ctx := context.Background()
if err := Migrate(ctx, database.DB, WithSchema(database.Schema)); err != nil {
t.Fatalf("Migrate() error = %v", err)
}
event := DefineEvent[None]("notify.release")
command := DefineCommand[None, None]("notify.gated", 1)
api, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true))
if err != nil {
t.Fatalf("New(api) error = %v", err)
}
worker, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true),
WithPollInterval(5*time.Second), WithWorkerConcurrency(1))
if err != nil {
t.Fatalf("New(worker) error = %v", err)
}
if err := worker.Register(Handle(command, func(context.Context, *Work[None]) (None, error) {
return None{}, nil
})); err != nil {
t.Fatalf("Register() error = %v", err)
}
listener := openNotificationListener(t, database, api)
cancelRun, runResult := startRuntime(t, worker)
defer stopRuntime(t, cancelRun, runResult)
exec, err := command.Enqueue(ctx, api, "notify/gated", None{}, WaitFor(event, "ready"), Within(time.Second))
if err != nil {
t.Fatalf("Enqueue(gated) error = %v", err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
if err := event.Deliver(ctx, api, exec.RunID, "unrelated", None{}); err != nil {
t.Fatalf("Emit(unrelated) error = %v", err)
}
waitForNotificationHintKind(t, listener, exec.RunID, store.NotificationEvent, 2*time.Second)
if err := event.Deliver(ctx, api, exec.RunID, "unrelated", None{}); err != nil {
t.Fatalf("Emit(equivalent) error = %v", err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
tx, err := database.DB.Conn.Begin(ctx)
if err != nil {
t.Fatal(err)
}
if err := event.Deliver(ctx, api.InTx(tx), exec.RunID, "rolled-back", None{}); err != nil {
_ = tx.Rollback(ctx)
t.Fatal(err)
}
assertNoNotification(t, listener, 100*time.Millisecond)
if err := tx.Rollback(ctx); err != nil {
t.Fatal(err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
started := time.Now()
if err := event.Deliver(ctx, api, exec.RunID, "ready", None{}); err != nil {
t.Fatalf("Emit(ready) error = %v", err)
}
waitForNotificationHintKind(t, listener, exec.RunID, store.NotificationRun, 2*time.Second)
waitForRunStatus(t, database.Schema, database.DB.Conn, exec.RunID, "succeeded", 2*time.Second)
if elapsed := time.Since(started); elapsed >= 2*time.Second {
t.Fatalf("event-release notification wake took %s with five-second polling", elapsed)
}
}
func TestClaimDoesNotNotifyAndTerminalSettlementWakesWatchers(t *testing.T) {
t.Parallel()
database := testpg.Open(t)
ctx := context.Background()
if err := Migrate(ctx, database.DB, WithSchema(database.Schema)); err != nil {
t.Fatalf("Migrate() error = %v", err)
}
runtime, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true))
if err != nil {
t.Fatalf("New() error = %v", err)
}
command := DefineCommand[None, None]("notify.journal_only", 1)
listener := openNotificationListener(t, database, runtime)
exec, err := command.Enqueue(ctx, runtime, "notify/journal-only", None{})
if err != nil {
t.Fatalf("Enqueue() error = %v", err)
}
waitForNotificationHintKind(t, listener, exec.RunID, store.NotificationRun, 2*time.Second)
candidates, err := runtime.store.ProbeCommands(ctx, []store.CommandKind{{Name: command.Name(), Version: command.Version()}}, 1)
if err != nil || len(candidates) != 1 {
t.Fatalf("ProbeCommands() = %+v, %v", candidates, err)
}
claimed, err := runtime.store.ClaimCommand(ctx, candidates[0], time.Minute, "notification-test", fault.None{})
if err != nil || claimed.Command == nil {
t.Fatalf("ClaimCommand() = %+v, %v", claimed, err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
result, err := canonical.Marshal(None{}, 0)
if err != nil {
t.Fatalf("encode result: %v", err)
}
if _, err := runtime.store.SettleCommandSuccess(ctx, store.CommandSuccess{Claim: *claimed.Command, Result: result}, fault.None{}); err != nil {
t.Fatalf("SettleCommandSuccess() error = %v", err)
}
waitForNotificationHintKind(t, listener, exec.RunID, store.NotificationEvent, 2*time.Second)
}
func TestImmediateRetryAndLeaseRecoveryNotify(t *testing.T) {
t.Parallel()
database := testpg.Open(t)
ctx := context.Background()
if err := Migrate(ctx, database.DB, WithSchema(database.Schema)); err != nil {
t.Fatalf("Migrate() error = %v", err)
}
runtime, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true))
if err != nil {
t.Fatalf("New() error = %v", err)
}
command := DefineCommand[None, None]("notify.retry", 1, WithRetry(Attempts(3)))
listener := openNotificationListener(t, database, runtime)
claim := startAndClaimForNotification(t, ctx, runtime, listener, command, "notify/immediate-retry")
if _, err := runtime.store.SettleCommandConclusion(ctx, store.CommandConclusion{
Claim: claim, Classification: retrypolicy.ClassInterrupted,
Failure: failure.Value{Code: "interrupted", Message: "test interruption"},
}, fault.None{}); err != nil {
t.Fatalf("SettleCommandConclusion(interrupted) error = %v", err)
}
waitForNotificationHintKind(t, listener, RunID(claim.RunID.String()), store.NotificationRun, 2*time.Second)
candidates, err := runtime.store.ProbeCommands(ctx,
[]store.CommandKind{{Name: command.Name(), Version: command.Version()}}, 10)
if err != nil {
t.Fatalf("ProbeCommands(future retry) error = %v", err)
}
var retryCandidate *store.CommandCandidate
for index := range candidates {
if candidates[index].RunID == claim.RunID {
retryCandidate = &candidates[index]
break
}
}
if retryCandidate == nil {
t.Fatal("immediate retry was not claimable")
}
retryClaim, err := runtime.store.ClaimCommand(ctx, *retryCandidate, time.Minute, "notification-test", fault.None{})
if err != nil || retryClaim.Command == nil {
t.Fatalf("ClaimCommand(future retry) = %+v, %v", retryClaim, err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
if _, err := runtime.store.SettleCommandConclusion(ctx, store.CommandConclusion{
Claim: *retryClaim.Command, Classification: retrypolicy.ClassRetryable,
Failure: failure.Value{Code: "retryable", Message: "test retry"},
}, fault.None{}); err != nil {
t.Fatalf("SettleCommandConclusion(future retry) error = %v", err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
recoveryClaim := startAndClaimForNotification(t, ctx, runtime, listener, command, "notify/lease-recovery")
if _, err := database.DB.Conn.Exec(ctx, `UPDATE `+pgschema.Table(database.Schema, "flow_command_queue")+`
SET lease_expires_at=clock_timestamp()-interval '1 second' WHERE command_id=$1`, recoveryClaim.CommandID); err != nil {
t.Fatalf("expire lease: %v", err)
}
recovery, err := runtime.store.RecoverExpiredCommandLease(ctx, store.ExpiredLeaseCandidate{
CommandID: recoveryClaim.CommandID, RunID: recoveryClaim.RunID,
})
if err != nil || !recovery.Changed {
t.Fatalf("RecoverExpiredCommandLease() = %t, %v", recovery.Changed, err)
}
waitForNotificationHintKind(t, listener, RunID(recoveryClaim.RunID.String()), store.NotificationRun, 2*time.Second)
}
func openNotificationListener(t *testing.T, database testpg.Database, runtime *Runtime) *pgx.Conn {
t.Helper()
ctx := context.Background()
listener, err := pgx.ConnectConfig(ctx, database.DB.Conn.Config().ConnConfig)
if err != nil {
t.Fatalf("connect notification listener: %v", err)
}
t.Cleanup(func() { _ = listener.Close(context.Background()) })
channel := runtime.store.NotificationChannel()
if _, err := listener.Exec(ctx, "LISTEN "+pgx.Identifier{channel}.Sanitize()); err != nil {
t.Fatalf("LISTEN: %v", err)
}
return listener
}
func waitForNotificationHintKind(t *testing.T, listener *pgx.Conn, runID RunID, kind string, timeout time.Duration) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
notification, err := listener.WaitForNotification(ctx)
if err != nil {
t.Fatalf("WaitForNotification() error = %v", err)
}
hint, valid := store.ParseNotificationHint(notification.Payload)
if valid && hint.RunID.String() == string(runID) && hint.Kind == kind {
return
}
}
}
func assertNoNotification(t *testing.T, listener *pgx.Conn, timeout time.Duration) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
_, err := listener.WaitForNotification(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("notification error = %v, want deadline", err)
}
}
func startAndClaimForNotification(
t *testing.T,
ctx context.Context,
runtime *Runtime,
listener *pgx.Conn,
command Command[None, None],
key string,
) store.ClaimedCommand {
t.Helper()
exec, err := command.Enqueue(ctx, runtime, key, None{})
if err != nil {
t.Fatalf("Enqueue(%s) error = %v", key, err)
}
waitForNotificationHintKind(t, listener, exec.RunID, store.NotificationRun, 2*time.Second)
candidates, err := runtime.store.ProbeCommands(ctx,
[]store.CommandKind{{Name: command.Name(), Version: command.Version()}}, 10)
if err != nil {
t.Fatalf("ProbeCommands(%s) error = %v", key, err)
}
for _, candidate := range candidates {
if candidate.RunID.String() != string(exec.RunID) {
continue
}
claimed, err := runtime.store.ClaimCommand(ctx, candidate, time.Minute, "notification-test", fault.None{})
if err != nil || claimed.Command == nil {
t.Fatalf("ClaimCommand(%s) = %+v, %v", key, claimed, err)
}
assertNoNotification(t, listener, 150*time.Millisecond)
return *claimed.Command
}
t.Fatalf("no command candidate found for %s", exec.RunID)
return store.ClaimedCommand{}
}
func TestDistributedNotificationAndReconnectCatchUp(t *testing.T) {
t.Parallel()
database := testpg.Open(t)
ctx := context.Background()
if err := Migrate(ctx, database.DB, WithSchema(database.Schema)); err != nil {
t.Fatalf("Migrate() error = %v", err)
}
command := DefineCommand[runtimeArgs, runtimeResult]("notify.distributed", 1)
api, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true))
if err != nil {
t.Fatalf("New(api) error = %v", err)
}
observer := &recordingObserver{}
worker, err := New(database.DB, WithSchema(database.Schema), WithNotifications(true),
WithObserver(observer), WithPollInterval(5*time.Second), WithWorkerConcurrency(1))
if err != nil {
t.Fatalf("New(worker) error = %v", err)
}
if err := worker.Register(Handle(command, func(_ context.Context, work *Work[runtimeArgs]) (runtimeResult, error) {
return runtimeResult{Value: work.Info().CommandKey}, nil
})); err != nil {
t.Fatalf("Register() error = %v", err)
}
cancelRun, runResult := startRuntime(t, worker)
waitForObservation(t, observer, "notify_listener", "listening", 1, 2*time.Second)
started := time.Now()
first, err := command.Enqueue(ctx, api, "notify/remote", runtimeArgs{})
if err != nil {
t.Fatalf("Enqueue(remote) error = %v", err)
}
waitForRunStatus(t, database.Schema, database.DB.Conn, first.RunID, "succeeded", 2*time.Second)
if elapsed := time.Since(started); elapsed >= 2*time.Second {
t.Fatalf("notification wake took %s with five-second polling", elapsed)
}
if _, err := database.DB.Conn.Exec(ctx, `SELECT pg_notify($1,$2)`, worker.store.NotificationChannel(), `{"v":99}`); err != nil {
t.Fatalf("publish future notification hint: %v", err)
}
waitForObservation(t, observer, "notify_hint", "broad_wake", 1, 2*time.Second)
var terminated bool
applicationName := "flow-listener-" + worker.instanceID.String()
if err := database.DB.Conn.QueryRow(ctx, `SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE application_name=$1 AND pid <> pg_backend_pid() LIMIT 1`, applicationName).Scan(&terminated); err != nil {
t.Fatalf("terminate listener: %v", err)
}
if !terminated {
t.Fatal("listener backend was not terminated")
}
waitForObservation(t, observer, "notify_listener", "reconnecting", 1, 2*time.Second)
// This commit races the disconnected window. Either a reconnected listener
// receives its hint or the mandatory post-LISTEN catch-up generation finds
// it; the five-second correctness poll is intentionally too slow to help.
second, err := command.Enqueue(ctx, api, "notify/reconnect", runtimeArgs{})
if err != nil {
t.Fatalf("Enqueue(reconnect) error = %v", err)
}
waitForRunStatus(t, database.Schema, database.DB.Conn, second.RunID, "succeeded", 2*time.Second)
waitForObservation(t, observer, "notify_listener", "listening", 2, 2*time.Second)
stopRuntime(t, cancelRun, runResult)
}
func waitForObservation(t *testing.T, observer *recordingObserver, operation, outcome string, count int, timeout time.Duration) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
matched := 0
for _, observation := range observer.snapshot() {
if observation.Operation == operation && observation.Outcome == outcome {
matched++
}
}
if matched >= count {
return
}
time.Sleep(time.Millisecond)
}
t.Fatalf("did not observe %s/%s %d times: %#v", operation, outcome, count, observer.snapshot())
}