backbone-core 3.18.0

Backbone Framework Core - Foundation for generic CRUD system
Documentation
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
//! Live proof of the delivery path of `NotificationRequested` rows: a request staged on a write's
//! transaction is read back by the outbox relay and handed to a registered delivery exactly once, a
//! replayed row is not delivered again, a `Retry` leaves the row pending and kills it after the
//! limit, a `Reject` kills it at once with the delivery's reason, and two relays that hand the same
//! row at the same time deliver it once.
//!
//! Gated on `BACKBONE_ORM_RLS_DSN` (a superuser DSN): the tests create a schema and drop it. When the
//! variable is unset they skip, so a checkout without a database still passes.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use backbone_core::notification::{
    DeliveryOutcome, NotificationDelivery, NotificationDispatcher, PendingNotification,
    RecordingDelivery, RetryPolicy,
};
use backbone_core::write_hooks::NotificationRequested;
use backbone_orm::in_write_tx;
use backbone_outbox::{inbox, outbox, relay, OutboxRecord};
use chrono::Utc;
use serde_json::json;
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use uuid::Uuid;

struct Db {
    pool: PgPool,
    schema: String,
    company: Uuid,
}

impl Db {
    async fn open() -> Option<Self> {
        let dsn = std::env::var("BACKBONE_ORM_RLS_DSN").ok()?;
        let pool = PgPoolOptions::new()
            .max_connections(6)
            .connect(&dsn)
            .await
            .expect("connect");
        let schema = format!("notif_{}", &Uuid::new_v4().simple().to_string()[..8]);
        outbox::migrate(&pool, &schema)
            .await
            .expect("migrate the outbox");
        Some(Self {
            pool,
            schema,
            company: Uuid::new_v4(),
        })
    }

    async fn drop(self) {
        sqlx::query(&format!("DROP SCHEMA {} CASCADE", self.schema))
            .execute(&self.pool)
            .await
            .unwrap();
    }

    fn request(template: &str) -> NotificationRequested {
        NotificationRequested {
            channel: "email".into(),
            template: Some(template.into()),
            body: None,
            to: json!(["ops@example.test"]),
            data: json!({ "title": "Disk full" }),
            trigger: "Incident.after_create".into(),
        }
    }

    fn record(&self, request: &NotificationRequested) -> OutboxRecord {
        OutboxRecord::new(
            NotificationRequested::NAME,
            "Incident",
            "inc-1",
            self.company,
            serde_json::to_value(request).unwrap(),
            Utc::now(),
        )
        .with_version(NotificationRequested::VERSION)
        .with_correlation_id("corr-live")
        .with_causation_id("cause-live")
    }

    /// Stage `record` on a write transaction, as a hook does, and commit it.
    async fn stage(&self, record: &OutboxRecord) {
        let schema = self.schema.clone();
        let record = record.clone();
        in_write_tx::<_, _, sqlx::Error>(&self.pool, async move {
            outbox::stage_in_write_tx(&schema, &record)
                .await
                .map_err(|e| sqlx::Error::Protocol(e.to_string()))?;
            Ok(())
        })
        .await
        .expect("stage in a write transaction");
    }

    fn dispatcher(
        &self,
        delivery: impl NotificationDelivery + 'static,
        policy: RetryPolicy,
    ) -> Arc<NotificationDispatcher> {
        Arc::new(
            NotificationDispatcher::new(self.pool.clone(), self.schema.clone(), Arc::new(delivery))
                .with_retry(policy),
        )
    }

    /// One relay pass through `dispatcher`'s sink: how many rows it marked published.
    async fn pass(&self, dispatcher: &Arc<NotificationDispatcher>) -> usize {
        relay::drain_once(&self.pool, &self.schema, 50, dispatcher.sink())
            .await
            .expect("a relay pass")
    }

    async fn pending(&self) -> i64 {
        outbox::pending_count(&self.pool, &self.schema)
            .await
            .unwrap()
    }

    async fn dead(&self) -> Vec<relay::DeadRecord> {
        relay::list_dead(&self.pool, &self.schema, 10)
            .await
            .unwrap()
    }

    async fn consumed(&self, consumer: &str, id: Uuid) -> bool {
        inbox::was_consumed(&self.pool, &self.schema, consumer, id)
            .await
            .unwrap()
    }
}

#[tokio::test]
async fn a_request_staged_in_a_write_tx_is_delivered_once_and_a_replay_is_deduped() {
    let Some(db) = Db::open().await else { return };
    let delivery = RecordingDelivery::new();
    let dispatcher = db.dispatcher(delivery.clone(), RetryPolicy::immediate(3));
    let record = db.record(&Db::request("incident_opened"));

    // A write that fails after staging leaves nothing to deliver.
    let schema = db.schema.clone();
    let doomed = record.clone();
    let failed = in_write_tx::<_, (), sqlx::Error>(&db.pool, async move {
        outbox::stage_in_write_tx(&schema, &doomed)
            .await
            .map_err(|e| sqlx::Error::Protocol(e.to_string()))?;
        Err(sqlx::Error::Protocol(
            "the write failed after staging".into(),
        ))
    })
    .await;
    assert!(failed.is_err());
    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(delivery.calls(), 0, "a rolled-back write requests nothing");

    db.stage(&record).await;
    assert_eq!(db.pending().await, 1);
    assert_eq!(
        db.pass(&dispatcher).await,
        1,
        "the relay marks the delivered row published"
    );
    assert_eq!(delivery.calls(), 1, "the delivery was called exactly once");
    assert_eq!(db.pending().await, 0);
    assert!(
        db.consumed("notification-delivery", record.id).await,
        "the claim committed with the delivery"
    );

    let got = &delivery.received()[0];
    assert_eq!(got.id, record.id, "the event id is the idempotency key");
    assert_eq!(got.attempt, 1);
    assert_eq!(got.request, Db::request("incident_opened"));
    assert_eq!(
        got.correlation_id.as_deref(),
        Some("corr-live"),
        "correlation is preserved"
    );
    assert_eq!(
        got.causation_id.as_deref(),
        Some("cause-live"),
        "causation is preserved"
    );
    assert_eq!(
        (got.aggregate_type.as_str(), got.aggregate_id.as_str()),
        ("Incident", "inc-1")
    );
    assert_eq!(got.company_id, db.company);

    // The relay hands the same row again (a crash between the delivery and the mark): the claim holds.
    sqlx::query(&format!(
        "UPDATE {}.outbox_events SET published_at = NULL",
        db.schema
    ))
    .execute(&db.pool)
    .await
    .unwrap();
    assert_eq!(db.pending().await, 1);
    assert_eq!(
        db.pass(&dispatcher).await,
        1,
        "a replay is accepted and marked published"
    );
    assert_eq!(
        delivery.calls(),
        1,
        "the replay was deduped: the delivery is not called again"
    );
    db.drop().await;
}

#[tokio::test]
async fn a_retry_leaves_the_row_pending_then_dead_after_the_limit() {
    let Some(db) = Db::open().await else { return };
    let delivery = RecordingDelivery::always(DeliveryOutcome::Retry("provider busy".into()));
    let dispatcher = db.dispatcher(delivery.clone(), RetryPolicy::immediate(3));
    let record = db.record(&Db::request("incident_opened"));
    db.stage(&record).await;

    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(
        (delivery.calls(), db.pending().await, db.dead().await.len()),
        (1, 1, 0),
        "after the first retry the row waits"
    );
    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(
        (delivery.calls(), db.pending().await, db.dead().await.len()),
        (2, 1, 0),
        "after the second it still waits"
    );
    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(delivery.calls(), 3);
    assert_eq!(
        db.pending().await,
        0,
        "the limit was reached: the row is no longer pending"
    );
    let dead = db.dead().await;
    assert_eq!(dead.len(), 1, "it is dead, not published");
    let reason = dead[0].failure_reason.clone().unwrap_or_default();
    assert!(
        reason.contains("gave up after 3 attempts") && reason.contains("provider busy"),
        "{reason}"
    );
    assert_eq!(
        delivery
            .received()
            .iter()
            .map(|n| n.attempt)
            .collect::<Vec<_>>(),
        vec![1, 2, 3],
        "each offer is counted"
    );
    assert!(
        !db.consumed("notification-delivery", record.id).await,
        "a retry never claims the row"
    );

    db.pass(&dispatcher).await;
    assert_eq!(delivery.calls(), 3, "a dead row is not offered again");

    // Once the cause is fixed the row is handed back and delivered.
    let fixed = RecordingDelivery::new();
    let dispatcher = db.dispatcher(fixed.clone(), RetryPolicy::immediate(3));
    assert!(relay::reemit(&db.pool, &db.schema, record.id)
        .await
        .unwrap());
    assert_eq!(db.pass(&dispatcher).await, 1);
    assert_eq!(fixed.calls(), 1);
    assert!(db.consumed("notification-delivery", record.id).await);
    db.drop().await;
}

#[tokio::test]
async fn a_retry_waits_out_its_backoff_before_the_delivery_is_called_again() {
    let Some(db) = Db::open().await else { return };
    let delivery = RecordingDelivery::always(DeliveryOutcome::Retry("rate limited".into()));
    let policy = RetryPolicy {
        max_attempts: 4,
        base_delay: Duration::from_secs(3600),
        max_delay: Duration::from_secs(3600),
    };
    let dispatcher = db.dispatcher(delivery.clone(), policy);
    db.stage(&db.record(&Db::request("incident_opened"))).await;

    db.pass(&dispatcher).await;
    db.pass(&dispatcher).await;
    db.pass(&dispatcher).await;
    assert_eq!(
        delivery.calls(),
        1,
        "inside the wait the row is left pending without calling the delivery"
    );
    assert_eq!((db.pending().await, db.dead().await.len()), (1, 0));
    db.drop().await;
}

#[tokio::test]
async fn a_retry_then_a_delivery_is_delivered_once() {
    let Some(db) = Db::open().await else { return };
    let delivery = RecordingDelivery::answering(vec![DeliveryOutcome::Retry("timeout".into())]);
    let dispatcher = db.dispatcher(delivery.clone(), RetryPolicy::immediate(5));
    let record = db.record(&Db::request("incident_opened"));
    db.stage(&record).await;

    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(db.pass(&dispatcher).await, 1);
    assert_eq!(db.pass(&dispatcher).await, 0, "nothing is left to offer");
    assert_eq!(delivery.calls(), 2);
    assert_eq!(delivery.received()[1].attempt, 2);
    assert!(db.consumed("notification-delivery", record.id).await);
    assert!(db.dead().await.is_empty());
    db.drop().await;
}

#[tokio::test]
async fn a_reject_dead_letters_with_the_reason() {
    let Some(db) = Db::open().await else { return };
    let delivery =
        RecordingDelivery::always(DeliveryOutcome::Reject("no template named `ghost`".into()));
    let dispatcher = db.dispatcher(delivery.clone(), RetryPolicy::default());
    let record = db.record(&Db::request("ghost"));
    db.stage(&record).await;

    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(delivery.calls(), 1, "a rejection is not retried");
    assert_eq!(db.pending().await, 0);
    let dead = db.dead().await;
    assert_eq!(dead.len(), 1);
    assert_eq!(dead[0].id, record.id);
    assert_eq!(
        dead[0].failure_reason.as_deref(),
        Some("no template named `ghost`")
    );
    assert!(
        !db.consumed("notification-delivery", record.id).await,
        "a rejection never claims the row"
    );
    db.drop().await;
}

#[tokio::test]
async fn records_that_are_not_requests_are_ignored_and_a_malformed_request_is_dead() {
    let Some(db) = Db::open().await else { return };
    let delivery = RecordingDelivery::new();
    let dispatcher = db.dispatcher(delivery.clone(), RetryPolicy::default());

    // The sink can share a relay: another event type is accepted and left to its own consumer.
    let other = OutboxRecord::new(
        "IncidentOpened",
        "Incident",
        "inc-1",
        db.company,
        json!({}),
        Utc::now(),
    );
    db.stage(&other).await;
    assert_eq!(db.pass(&dispatcher).await, 1);
    assert_eq!(delivery.calls(), 0);

    let garbage = OutboxRecord::new(
        NotificationRequested::NAME,
        "Incident",
        "inc-2",
        db.company,
        json!({ "nonsense": 1 }),
        Utc::now(),
    );
    db.stage(&garbage).await;
    assert_eq!(db.pass(&dispatcher).await, 0);
    assert_eq!(
        delivery.calls(),
        0,
        "a payload that is not a request never reaches the delivery"
    );
    let dead = db.dead().await;
    assert_eq!(dead.len(), 1);
    assert!(dead[0]
        .failure_reason
        .clone()
        .unwrap_or_default()
        .contains("not a NotificationRequested"));
    db.drop().await;
}

/// A delivery that takes a while, so two relays are inside it together.
struct Slow(RecordingDelivery);

#[async_trait]
impl NotificationDelivery for Slow {
    async fn deliver(&self, notification: &PendingNotification) -> DeliveryOutcome {
        tokio::time::sleep(Duration::from_millis(250)).await;
        self.0.deliver(notification).await
    }
}

#[tokio::test]
async fn two_relays_handing_the_same_row_at_once_deliver_it_once() {
    let Some(db) = Db::open().await else { return };
    let recording = RecordingDelivery::new();
    let first = db.dispatcher(Slow(recording.clone()), RetryPolicy::default());
    let second = db.dispatcher(Slow(recording.clone()), RetryPolicy::default());
    let record = db.record(&Db::request("incident_opened"));

    let (a, b) = tokio::join!(
        first.deliver(record.clone()),
        second.deliver(record.clone())
    );
    assert!(a.is_ok() && b.is_ok(), "{a:?} {b:?}");
    assert_eq!(
        recording.calls(),
        1,
        "the second relay waited on the claim, then found it taken"
    );
    assert!(db.consumed("notification-delivery", record.id).await);
    db.drop().await;
}

#[tokio::test]
async fn deliveries_named_apart_each_handle_the_request_once() {
    let Some(db) = Db::open().await else { return };
    let mail = RecordingDelivery::new().named("mail");
    let mirror = RecordingDelivery::new().named("mirror");
    let by_mail = db.dispatcher(mail.clone(), RetryPolicy::default());
    let by_mirror = db.dispatcher(mirror.clone(), RetryPolicy::default());
    let record = db.record(&Db::request("incident_opened"));

    for _ in 0..2 {
        by_mail.deliver(record.clone()).await.unwrap();
        by_mirror.deliver(record.clone()).await.unwrap();
    }
    assert_eq!((mail.calls(), mirror.calls()), (1, 1));
    assert!(db.consumed("mail", record.id).await && db.consumed("mirror", record.id).await);
    db.drop().await;
}