obix 0.4.3

Implementation of outbox backed by PG / sqlx
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
mod helpers;

use std::sync::{
    Arc, Mutex,
    atomic::{AtomicUsize, Ordering},
};

use es_entity::AtomicOperation;
use es_entity::hooks::{BoxFuture, HookOperation};
use futures::stream::StreamExt;
use obix::{
    MailboxConfig,
    out::{Outbox, PersistentOutboxEvent, PostPersistHook},
};
use serde::{Deserialize, Serialize};
use serial_test::file_serial;

use helpers::{TestTables, init_outbox, init_pool};

// All payload types intentionally share one serde shape: every outbox in
// these tests writes to the same physical test table, so every outbox's
// cache must be able to deserialize every row (gap-fill loads rows
// regardless of which outbox produced them).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
enum SourceEvent {
    Source(u64),
    Mapped(u64),
    Native(u64),
    Hop(u64),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
enum DestEvent {
    Source(u64),
    Mapped(u64),
    Native(u64),
    Hop(u64),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
enum HopEvent {
    Source(u64),
    Mapped(u64),
    Native(u64),
    Hop(u64),
}

async fn outbox_row_count(pool: &sqlx::PgPool) -> anyhow::Result<i64> {
    let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM persistent_outbox_events")
        .fetch_one(pool)
        .await?;
    Ok(count)
}

async fn payloads_by_sequence(pool: &sqlx::PgPool) -> anyhow::Result<Vec<serde_json::Value>> {
    let payloads: Vec<serde_json::Value> =
        sqlx::query_scalar("SELECT payload FROM persistent_outbox_events ORDER BY sequence")
            .fetch_all(pool)
            .await?;
    Ok(payloads)
}

fn default_config() -> MailboxConfig {
    MailboxConfig::builder()
        .build()
        .expect("Couldn't build MailboxConfig")
}

/// Records every chunk the hook sees (sequence + payload), plus the row
/// counts visible through the op's connection (inside the tx) and through
/// the pool (outside the tx) at invocation time.
struct RecordingHook {
    chunks: Arc<Mutex<Vec<Vec<(u64, SourceEvent)>>>>,
    in_tx_counts: Arc<Mutex<Vec<i64>>>,
    outside_tx_counts: Arc<Mutex<Vec<i64>>>,
    pool: sqlx::PgPool,
}

impl PostPersistHook<SourceEvent> for RecordingHook {
    fn on_persisted<'a>(
        &'a self,
        op: &'a mut HookOperation<'_>,
        events: &'a [PersistentOutboxEvent<SourceEvent>],
    ) -> BoxFuture<'a, Result<(), sqlx::Error>> {
        Box::pin(async move {
            let in_tx: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM persistent_outbox_events")
                .fetch_one(&mut *op.connection())
                .await?;
            let outside: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM persistent_outbox_events")
                .fetch_one(&self.pool)
                .await?;
            self.in_tx_counts.lock().unwrap().push(in_tx);
            self.outside_tx_counts.lock().unwrap().push(outside);
            self.chunks.lock().unwrap().push(
                events
                    .iter()
                    .map(|e| {
                        (
                            u64::from(e.sequence),
                            e.payload.clone().expect("payload present"),
                        )
                    })
                    .collect(),
            );
            Ok(())
        })
    }
}

/// Counts invocations — for asserting a hook did or did not fire.
#[derive(Clone)]
struct CountingHook {
    invocations: Arc<AtomicUsize>,
}

impl PostPersistHook<SourceEvent> for CountingHook {
    fn on_persisted<'a>(
        &'a self,
        _op: &'a mut HookOperation<'_>,
        _events: &'a [PersistentOutboxEvent<SourceEvent>],
    ) -> BoxFuture<'a, Result<(), sqlx::Error>> {
        self.invocations.fetch_add(1, Ordering::SeqCst);
        Box::pin(async { Ok(()) })
    }
}

/// Always errors — a hook failure must abort the whole transaction.
struct FailingHook;

impl PostPersistHook<SourceEvent> for FailingHook {
    fn on_persisted<'a>(
        &'a self,
        _op: &'a mut HookOperation<'_>,
        _events: &'a [PersistentOutboxEvent<SourceEvent>],
    ) -> BoxFuture<'a, Result<(), sqlx::Error>> {
        Box::pin(async { Err(sqlx::Error::Protocol("post-persist hook veto".into())) })
    }
}

/// The repost consumer from the design doc: map source events into the
/// destination outbox's payload type and publish them from inside the hook —
/// same transaction, immediate-insert path.
struct RepostHook {
    dest: Outbox<DestEvent, TestTables>,
}

impl PostPersistHook<SourceEvent> for RepostHook {
    fn on_persisted<'a>(
        &'a self,
        op: &'a mut HookOperation<'_>,
        events: &'a [PersistentOutboxEvent<SourceEvent>],
    ) -> BoxFuture<'a, Result<(), sqlx::Error>> {
        Box::pin(async move {
            let mapped: Vec<DestEvent> = events
                .iter()
                .filter_map(|e| match e.payload.as_ref()? {
                    SourceEvent::Source(n) => Some(DestEvent::Mapped(*n)),
                    _ => None,
                })
                .collect();
            if mapped.is_empty() {
                return Ok(());
            }
            self.dest.publish_all_persisted(op, mapped).await
        })
    }
}

/// Second hop for the chain test: dest events repost onwards into a third
/// outbox.
struct HopHook {
    next: Outbox<HopEvent, TestTables>,
}

impl PostPersistHook<DestEvent> for HopHook {
    fn on_persisted<'a>(
        &'a self,
        op: &'a mut HookOperation<'_>,
        events: &'a [PersistentOutboxEvent<DestEvent>],
    ) -> BoxFuture<'a, Result<(), sqlx::Error>> {
        Box::pin(async move {
            let mapped: Vec<HopEvent> = events
                .iter()
                .filter_map(|e| match e.payload.as_ref()? {
                    DestEvent::Mapped(n) => Some(HopEvent::Hop(*n)),
                    _ => None,
                })
                .collect();
            if mapped.is_empty() {
                return Ok(());
            }
            self.next.publish_all_persisted(op, mapped).await
        })
    }
}

#[tokio::test]
#[file_serial]
async fn hook_sees_persisted_events_pre_commit() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let outbox = init_outbox::<SourceEvent>(&pool, default_config()).await?;

    let chunks = Arc::new(Mutex::new(Vec::new()));
    let in_tx_counts = Arc::new(Mutex::new(Vec::new()));
    let outside_tx_counts = Arc::new(Mutex::new(Vec::new()));
    outbox.add_post_persist_hook(RecordingHook {
        chunks: chunks.clone(),
        in_tx_counts: in_tx_counts.clone(),
        outside_tx_counts: outside_tx_counts.clone(),
        pool: pool.clone(),
    });

    let mut op = outbox.begin_op().await?;
    outbox
        .publish_all_persisted(&mut op, [SourceEvent::Source(1), SourceEvent::Source(2)])
        .await?;

    // On a hook-supporting op nothing persists (and no hook fires) until
    // commit.
    assert!(chunks.lock().unwrap().is_empty());

    op.commit().await?;

    let chunks = chunks.lock().unwrap();
    assert_eq!(chunks.len(), 1, "one chunk for a small publish");
    assert_eq!(
        chunks[0],
        [(1, SourceEvent::Source(1)), (2, SourceEvent::Source(2))],
        "hook sees persisted events with assigned sequences"
    );
    assert_eq!(
        in_tx_counts.lock().unwrap().as_slice(),
        [2],
        "rows visible through the op's connection inside the tx"
    );
    assert_eq!(
        outside_tx_counts.lock().unwrap().as_slice(),
        [0],
        "rows not visible outside the tx before commit"
    );
    assert_eq!(outbox_row_count(&pool).await?, 2);
    Ok(())
}

#[tokio::test]
#[file_serial]
async fn hook_error_aborts_commit() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let outbox = init_outbox::<SourceEvent>(&pool, default_config()).await?;
    outbox.add_post_persist_hook(FailingHook);

    let mut op = outbox.begin_op().await?;
    outbox
        .publish_persisted_in_op(&mut op, SourceEvent::Source(1))
        .await?;
    assert!(
        op.commit().await.is_err(),
        "a hook error must fail the commit"
    );
    assert_eq!(
        outbox_row_count(&pool).await?,
        0,
        "the tx rolled back — including the events that triggered the hook"
    );
    Ok(())
}

#[tokio::test]
#[file_serial]
async fn rollback_never_invokes_hook() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let outbox = init_outbox::<SourceEvent>(&pool, default_config()).await?;

    let invocations = Arc::new(AtomicUsize::new(0));
    outbox.add_post_persist_hook(CountingHook {
        invocations: invocations.clone(),
    });

    let mut op = outbox.begin_op().await?;
    outbox
        .publish_persisted_in_op(&mut op, SourceEvent::Source(1))
        .await?;
    drop(op); // rollback

    assert_eq!(invocations.load(Ordering::SeqCst), 0);
    assert_eq!(outbox_row_count(&pool).await?, 0);
    Ok(())
}

/// Publishes on an op without commit-hook support (a bare `sqlx::Transaction`)
/// insert immediately — post-persist hooks must fire on that path too, which
/// is what makes "every persist path" literally true.
#[tokio::test]
#[file_serial]
async fn force_path_fires_hooks() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let outbox = init_outbox::<SourceEvent>(&pool, default_config()).await?;

    let invocations = Arc::new(AtomicUsize::new(0));
    outbox.add_post_persist_hook(CountingHook {
        invocations: invocations.clone(),
    });

    let mut tx = pool.begin().await?;
    outbox
        .publish_persisted_in_op(&mut tx, SourceEvent::Source(1))
        .await?;
    assert_eq!(
        invocations.load(Ordering::SeqCst),
        1,
        "on the force path the hook fires during the publish call itself"
    );
    tx.commit().await?;
    assert_eq!(outbox_row_count(&pool).await?, 1);
    Ok(())
}

#[tokio::test]
#[file_serial]
async fn repost_commits_atomically() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let source = init_outbox::<SourceEvent>(&pool, default_config()).await?;
    let dest = Outbox::<DestEvent, TestTables>::init(&pool, default_config()).await?;

    source.add_post_persist_hook(RepostHook { dest: dest.clone() });

    let mut op = source.begin_op().await?;
    source
        .publish_persisted_in_op(&mut op, SourceEvent::Source(7))
        .await?;
    op.commit().await?;

    assert_eq!(
        payloads_by_sequence(&pool).await?,
        [
            serde_json::json!({"Source": 7}),
            serde_json::json!({"Mapped": 7}),
        ],
        "source event and its reposted projection committed in one tx"
    );
    Ok(())
}

#[tokio::test]
#[file_serial]
async fn repost_rolls_back_atomically() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let source = init_outbox::<SourceEvent>(&pool, default_config()).await?;
    let dest = Outbox::<DestEvent, TestTables>::init(&pool, default_config()).await?;

    source.add_post_persist_hook(RepostHook { dest: dest.clone() });

    let mut op = source.begin_op().await?;
    source
        .publish_persisted_in_op(&mut op, SourceEvent::Source(7))
        .await?;
    drop(op); // rollback

    assert_eq!(
        outbox_row_count(&pool).await?,
        0,
        "neither source nor reposted rows survive a rollback"
    );
    Ok(())
}

/// A hook publishing into outbox B runs B's own post-persist hooks (force
/// path) — chains compose.
#[tokio::test]
#[file_serial]
async fn chained_reposts_compose() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let source = init_outbox::<SourceEvent>(&pool, default_config()).await?;
    let dest = Outbox::<DestEvent, TestTables>::init(&pool, default_config()).await?;
    let hop = Outbox::<HopEvent, TestTables>::init(&pool, default_config()).await?;

    source.add_post_persist_hook(RepostHook { dest: dest.clone() });
    dest.add_post_persist_hook(HopHook { next: hop.clone() });

    let mut op = source.begin_op().await?;
    source
        .publish_persisted_in_op(&mut op, SourceEvent::Source(3))
        .await?;
    op.commit().await?;

    assert_eq!(
        payloads_by_sequence(&pool).await?,
        [
            serde_json::json!({"Source": 3}),
            serde_json::json!({"Mapped": 3}),
            serde_json::json!({"Hop": 3}),
        ],
        "A→B→C: each hop persisted in the same tx, in causal order"
    );
    Ok(())
}

/// Multiple publishes on one op merge into a single commit hook; the hook
/// fires once per persisted chunk and sees every event exactly once.
#[tokio::test]
#[file_serial]
async fn merged_publishes_chunked_exactly_once() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let outbox = init_outbox::<SourceEvent>(
        &pool,
        MailboxConfig::builder()
            .persist_events_batch_size(2usize)
            .build()
            .expect("Couldn't build MailboxConfig"),
    )
    .await?;

    let chunks = Arc::new(Mutex::new(Vec::new()));
    outbox.add_post_persist_hook(RecordingHook {
        chunks: chunks.clone(),
        in_tx_counts: Arc::new(Mutex::new(Vec::new())),
        outside_tx_counts: Arc::new(Mutex::new(Vec::new())),
        pool: pool.clone(),
    });

    let mut op = outbox.begin_op().await?;
    outbox
        .publish_all_persisted(
            &mut op,
            [
                SourceEvent::Source(1),
                SourceEvent::Source(2),
                SourceEvent::Source(3),
            ],
        )
        .await?;
    outbox
        .publish_all_persisted(&mut op, [SourceEvent::Source(4), SourceEvent::Source(5)])
        .await?;
    op.commit().await?;

    let chunks = chunks.lock().unwrap();
    assert_eq!(
        chunks.iter().map(Vec::len).collect::<Vec<_>>(),
        [2, 2, 1],
        "batch_size 2 over 5 merged events → chunks of 2, 2, 1"
    );
    let flattened: Vec<_> = chunks.iter().flatten().cloned().collect();
    assert_eq!(
        flattened,
        (1..=5)
            .map(|n| (n, SourceEvent::Source(n)))
            .collect::<Vec<_>>(),
        "chunks cover all merged events exactly once, in publish order"
    );
    Ok(())
}

/// Hooks are snapshotted when a publish constructs the op's commit hook.
/// Registration after that point does not affect the in-flight op (later
/// publishes merge into the existing hook) — only subsequent ops see it.
#[tokio::test]
#[file_serial]
async fn late_registration_affects_only_subsequent_ops() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let outbox = init_outbox::<SourceEvent>(&pool, default_config()).await?;

    let invocations = Arc::new(AtomicUsize::new(0));

    let mut op = outbox.begin_op().await?;
    outbox
        .publish_persisted_in_op(&mut op, SourceEvent::Source(1))
        .await?;
    outbox.add_post_persist_hook(CountingHook {
        invocations: invocations.clone(),
    });
    outbox
        .publish_persisted_in_op(&mut op, SourceEvent::Source(2))
        .await?;
    op.commit().await?;
    assert_eq!(
        invocations.load(Ordering::SeqCst),
        0,
        "registration after the op's first publish is not seen by that op"
    );

    let mut op = outbox.begin_op().await?;
    outbox
        .publish_persisted_in_op(&mut op, SourceEvent::Source(3))
        .await?;
    op.commit().await?;
    assert_eq!(
        invocations.load(Ordering::SeqCst),
        1,
        "the next op snapshots the registered hook"
    );
    Ok(())
}

/// With es-entity ≥ 0.11.7 commit hooks run in registration order, so the
/// relative order of destination-native vs reposted events follows the order
/// of each outbox's first publish in the op — deterministically, in both
/// directions.
#[tokio::test]
#[file_serial]
async fn ordering_follows_first_publish_order() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let source = init_outbox::<SourceEvent>(&pool, default_config()).await?;
    let dest = Outbox::<DestEvent, TestTables>::init(&pool, default_config()).await?;

    source.add_post_persist_hook(RepostHook { dest: dest.clone() });

    // Dest-native publish first → its commit hook registers (and runs) first.
    let mut op = source.begin_op().await?;
    dest.publish_persisted_in_op(&mut op, DestEvent::Native(1))
        .await?;
    source
        .publish_persisted_in_op(&mut op, SourceEvent::Source(2))
        .await?;
    op.commit().await?;

    assert_eq!(
        payloads_by_sequence(&pool).await?,
        [
            serde_json::json!({"Native": 1}),
            serde_json::json!({"Source": 2}),
            serde_json::json!({"Mapped": 2}),
        ],
        "dest-native published first ⇒ dest-native sequences < reposted"
    );

    // Inverted: source publish first → reposted events precede dest-native.
    let mut op = source.begin_op().await?;
    source
        .publish_persisted_in_op(&mut op, SourceEvent::Source(4))
        .await?;
    dest.publish_persisted_in_op(&mut op, DestEvent::Native(5))
        .await?;
    op.commit().await?;

    assert_eq!(
        payloads_by_sequence(&pool).await?[3..],
        [
            serde_json::json!({"Source": 4}),
            serde_json::json!({"Mapped": 4}),
            serde_json::json!({"Native": 5}),
        ],
        "source published first ⇒ reposted sequences < dest-native"
    );
    Ok(())
}

/// The immediate-insert path drops the destination's in-process broadcast
/// hook, so delivery to destination listeners is healed by pg NOTIFY (fires
/// at commit) and the poller/gap-fill — reposted events must still arrive.
#[tokio::test]
#[file_serial]
async fn reposted_events_reach_destination_listener() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let source = init_outbox::<SourceEvent>(&pool, default_config()).await?;
    let dest = Outbox::<DestEvent, TestTables>::init(&pool, default_config()).await?;

    source.add_post_persist_hook(RepostHook { dest: dest.clone() });

    let mut listener = dest.listen_persisted(None);

    let mut op = source.begin_op().await?;
    source
        .publish_persisted_in_op(&mut op, SourceEvent::Source(9))
        .await?;
    op.commit().await?;

    let deadline = std::time::Duration::from_secs(5);
    let received = tokio::time::timeout(deadline, async {
        while let Some(event) = listener.next().await {
            if event.payload == Some(DestEvent::Mapped(9)) {
                return true;
            }
        }
        false
    })
    .await
    .unwrap_or(false);
    assert!(
        received,
        "reposted event must reach a destination listener via NOTIFY/gap-fill \
         despite the dropped in-process broadcast"
    );
    Ok(())
}