distributed 4.2.0

CQRS/ES framework for Rust using Plain Old Rust Structs — append-only events, replay, snapshots, outbox, service bus, and pluggable infrastructure
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
use super::api::BACKLOG_STATS_SCAN_LIMIT;
use super::*;
use crate::{
    CommitBatch, InMemoryOutboxStore, InMemoryRepository, OutboxMessage, OutboxMessageStatus,
    RepositoryError, TransactionalCommit,
};
use std::future::Future;
use std::sync::{Arc, Barrier};
use std::thread;
use std::time::{Duration, SystemTime};

async fn store_message(repo: &InMemoryRepository, message: OutboxMessage) -> String {
    let id = message.id().to_string();
    let mut batch = CommitBatch::empty();
    batch.outbox_messages.push(message);
    repo.commit_batch(batch).await.unwrap();
    id
}

fn load_message(repo: &InMemoryRepository, id: &str) -> OutboxMessage {
    repo.outbox_storage()
        .read()
        .unwrap()
        .get(id)
        .unwrap()
        .clone()
}

#[tokio::test]
async fn claim_includes_expired_in_flight_messages() {
    let repo = InMemoryRepository::new();
    let mut message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    message
        .claim_at("worker-1", Duration::from_secs(1), SystemTime::UNIX_EPOCH)
        .unwrap();
    let id = store_message(&repo, message).await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-2",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();

    assert_eq!(claimed.len(), 1);
    assert_eq!(claimed[0].worker_id.as_deref(), Some("worker-2"));
    assert_eq!(claimed[0].attempts, 2);

    let stored = load_message(&repo, &id);
    assert_eq!(stored.worker_id.as_deref(), Some("worker-2"));
    assert_eq!(stored.attempts, 2);
    assert!(stored.is_in_flight());
}

#[tokio::test]
async fn claim_skips_unexpired_in_flight_messages() {
    let repo = InMemoryRepository::new();
    let mut message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    message
        .claim_for("worker-1", Duration::from_secs(60))
        .unwrap();
    let id = store_message(&repo, message).await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-2",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();

    assert!(claimed.is_empty());
    let stored = load_message(&repo, &id);
    assert_eq!(stored.worker_id.as_deref(), Some("worker-1"));
    assert_eq!(stored.attempts, 1);
}

#[tokio::test]
async fn claim_uses_created_at_before_message_id_order() {
    let repo = InMemoryRepository::new();
    let mut newer = OutboxMessage::create("msg-a", "Event", b"{}".to_vec()).unwrap();
    newer.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(10);
    let mut older = OutboxMessage::create("msg-z", "Event", b"{}".to_vec()).unwrap();
    older.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(1);
    store_message(&repo, newer).await;
    store_message(&repo, older).await;

    let claimed = repo
        .outbox_store()
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();

    assert_eq!(claimed[0].id(), "msg-z");
}

#[tokio::test]
async fn claim_by_explicit_ids_claims_only_requested() {
    let repo = InMemoryRepository::new();
    store_message(
        &repo,
        OutboxMessage::create("msg-a", "Event", b"{}".to_vec()).unwrap(),
    )
    .await;
    store_message(
        &repo,
        OutboxMessage::create("msg-b", "Event", b"{}".to_vec()).unwrap(),
    )
    .await;
    store_message(
        &repo,
        OutboxMessage::create("msg-c", "Event", b"{}".to_vec()).unwrap(),
    )
    .await;

    let claimed = repo
        .outbox_store()
        .claim(ClaimOutboxMessages::for_ids(
            "worker-1",
            vec!["msg-b".to_string(), "msg-c".to_string()],
            Duration::from_secs(60),
        ))
        .await
        .unwrap();

    let mut claimed_ids = claimed
        .iter()
        .map(|m| m.id().to_string())
        .collect::<Vec<_>>();
    claimed_ids.sort();
    assert_eq!(claimed_ids, vec!["msg-b".to_string(), "msg-c".to_string()]);
    // The unrequested row stays pending.
    assert!(load_message(&repo, "msg-a").is_pending());
}

#[tokio::test]
async fn claim_by_ids_skips_unclaimable_without_error() {
    let repo = InMemoryRepository::new();
    let mut leased = OutboxMessage::create("msg-a", "Event", b"{}".to_vec()).unwrap();
    leased
        .claim_for("other-worker", Duration::from_secs(60))
        .unwrap();
    store_message(&repo, leased).await;

    // Requesting a currently-leased id (and a missing id) yields no claim,
    // not an error.
    let claimed = repo
        .outbox_store()
        .claim(ClaimOutboxMessages::for_ids(
            "worker-1",
            vec!["msg-a".to_string(), "missing".to_string()],
            Duration::from_secs(60),
        ))
        .await
        .unwrap();

    assert!(claimed.is_empty());
}

#[test]
fn sort_by_claim_order_uses_message_id_tiebreaker() {
    let mut later = OutboxMessage::create("msg-c", "Event", b"{}".to_vec()).unwrap();
    later.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(10);
    let mut second = OutboxMessage::create("msg-b", "Event", b"{}".to_vec()).unwrap();
    second.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(1);
    let mut first = OutboxMessage::create("msg-a", "Event", b"{}".to_vec()).unwrap();
    first.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(1);
    let mut messages = vec![later, second, first];

    sort_by_claim_order(&mut messages);

    assert_eq!(
        messages
            .iter()
            .map(|message| message.id())
            .collect::<Vec<_>>(),
        vec!["msg-a", "msg-b", "msg-c"]
    );
}

#[tokio::test]
async fn backlog_stats_counts_pending_and_tracks_oldest_created_at() {
    let repo = InMemoryRepository::new();
    let mut older = OutboxMessage::create("msg-a", "Event", b"{}".to_vec()).unwrap();
    older.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(1);
    let mut newer = OutboxMessage::create("msg-b", "Event", b"{}".to_vec()).unwrap();
    newer.created_at = SystemTime::UNIX_EPOCH + Duration::from_secs(10);
    let mut settled = OutboxMessage::create("msg-c", "Event", b"{}".to_vec()).unwrap();
    settled.fail("done".to_string()).unwrap();
    store_message(&repo, newer).await;
    store_message(&repo, settled).await;
    store_message(&repo, older).await;

    let stats = repo.outbox_store().backlog_stats().await.unwrap();

    assert_eq!(
        stats,
        OutboxBacklogStats {
            pending: 2,
            oldest_created_at: Some(SystemTime::UNIX_EPOCH + Duration::from_secs(1)),
        }
    );
}

/// The default `backlog_stats` must honor the mandatory listing bound —
/// a store that only implements the required methods must never see an
/// unbounded (`usize::MAX`) page-in from the gauge path.
#[tokio::test]
async fn default_backlog_stats_scan_is_bounded() {
    struct LimitProbeStore;

    impl OutboxStore for LimitProbeStore {
        fn messages_by_status(
            &self,
            _status: OutboxMessageStatus,
            limit: usize,
        ) -> impl Future<Output = Result<Vec<OutboxMessage>, RepositoryError>> + Send + '_ {
            async move {
                assert_eq!(limit, BACKLOG_STATS_SCAN_LIMIT);
                Ok(Vec::new())
            }
        }

        fn claim<'a>(
            &'a self,
            _request: ClaimOutboxMessages,
        ) -> impl Future<Output = Result<Vec<OutboxMessage>, RepositoryError>> + Send + 'a {
            async move { unimplemented!("probe store only lists") }
        }

        fn complete<'a>(
            &'a self,
            _claim: &'a OutboxClaimRef,
        ) -> impl Future<Output = Result<(), RepositoryError>> + Send + 'a {
            async move { unimplemented!("probe store only lists") }
        }

        fn release<'a>(
            &'a self,
            _claim: &'a OutboxClaimRef,
            _error: &'a str,
        ) -> impl Future<Output = Result<(), RepositoryError>> + Send + 'a {
            async move { unimplemented!("probe store only lists") }
        }

        fn fail<'a>(
            &'a self,
            _claim: &'a OutboxClaimRef,
            _error: &'a str,
        ) -> impl Future<Output = Result<(), RepositoryError>> + Send + 'a {
            async move { unimplemented!("probe store only lists") }
        }
    }

    let stats = LimitProbeStore.backlog_stats().await.unwrap();
    assert_eq!(stats, OutboxBacklogStats::default());
}

#[tokio::test]
async fn competing_workers_only_claim_message_once() {
    let repo = InMemoryRepository::new();
    let message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    let id = store_message(&repo, message).await;

    let barrier = Arc::new(Barrier::new(3));
    let store_a = repo.outbox_store();
    let store_b = repo.outbox_store();
    let barrier_a = Arc::clone(&barrier);
    let barrier_b = Arc::clone(&barrier);

    let worker_a = thread::spawn(move || {
        barrier_a.wait();
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(store_a.claim(ClaimOutboxMessages::new(
            "worker-a",
            1,
            Duration::from_secs(60),
        )))
        .unwrap()
        .len()
    });
    let worker_b = thread::spawn(move || {
        barrier_b.wait();
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(store_b.claim(ClaimOutboxMessages::new(
            "worker-b",
            1,
            Duration::from_secs(60),
        )))
        .unwrap()
        .len()
    });

    barrier.wait();
    let total_claimed = worker_a.join().unwrap() + worker_b.join().unwrap();

    assert_eq!(total_claimed, 1);
    let stored = load_message(&repo, &id);
    assert!(stored.is_in_flight());
    assert_eq!(stored.attempts, 1);
}

#[tokio::test]
async fn publish_failure_releases_until_retry_ceiling_then_fails() {
    let repo = InMemoryRepository::new();
    let message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    let id = store_message(&repo, message).await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let claim = OutboxClaimRef::from_message(&claimed[0]).unwrap();
    let action = store
        .record_failure(&claim, "first failure", 2)
        .await
        .unwrap();
    assert_eq!(action, OutboxPublishFailureAction::Released);

    let stored = load_message(&repo, &id);
    assert!(stored.is_pending());
    assert_eq!(stored.attempts, 1);
    assert_eq!(stored.last_error.as_deref(), Some("first failure"));

    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let claim = OutboxClaimRef::from_message(&claimed[0]).unwrap();
    let action = store
        .record_failure(&claim, "second failure", 2)
        .await
        .unwrap();
    assert_eq!(action, OutboxPublishFailureAction::Failed);

    let stored = load_message(&repo, &id);
    assert!(stored.is_failed());
    assert_eq!(stored.attempts, 2);
    assert_eq!(stored.last_error.as_deref(), Some("second failure"));
}

#[tokio::test]
async fn missing_message_updates_return_not_found() {
    let store = InMemoryOutboxStore {
        storage: Default::default(),
    };
    let claim = OutboxClaimRef {
        message_id: "missing".into(),
        worker_id: "worker-1".into(),
        leased_until: SystemTime::now(),
        attempt: 1,
    };

    let is_missing =
        |err: RepositoryError| matches!(&err, RepositoryError::NotFound { id } if id == "missing");
    assert!(is_missing(store.complete(&claim).await.unwrap_err()));
    assert!(is_missing(
        store.release(&claim, "error").await.unwrap_err()
    ));
    assert!(is_missing(store.fail(&claim, "error").await.unwrap_err()));
}

#[tokio::test]
async fn stale_or_mismatched_claims_cannot_be_completed() {
    let repo = InMemoryRepository::new();
    let message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    let _id = store_message(&repo, message).await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let mut claim = OutboxClaimRef::from_message(&claimed[0]).unwrap();
    claim.worker_id = "worker-2".into();
    let err = store.complete(&claim).await.unwrap_err();
    assert!(matches!(err, RepositoryError::InvalidState { .. }));

    let mut expired = OutboxMessage::create("msg-2", "Event", b"{}".to_vec()).unwrap();
    expired
        .claim_at("worker-1", Duration::from_secs(1), SystemTime::UNIX_EPOCH)
        .unwrap();
    let expired_id = store_message(&repo, expired).await;
    let expired = load_message(&repo, &expired_id);
    let claim = OutboxClaimRef::from_message(&expired).unwrap();
    let err = store.complete(&claim).await.unwrap_err();
    assert!(matches!(err, RepositoryError::InvalidState { .. }));
}

#[tokio::test]
async fn stale_attempt_claims_cannot_complete_later_claims() {
    let repo = InMemoryRepository::new();
    let message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    let _id = store_message(&repo, message).await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let stale_claim = OutboxClaimRef::from_message(&claimed[0]).unwrap();
    store.release(&stale_claim, "retry").await.unwrap();

    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let current_claim = OutboxClaimRef::from_message(&claimed[0]).unwrap();

    let err = store.complete(&stale_claim).await.unwrap_err();
    assert!(matches!(err, RepositoryError::InvalidState { .. }));
    store.complete(&current_claim).await.unwrap();
}

#[tokio::test]
async fn complete_many_completes_the_whole_batch() {
    let repo = InMemoryRepository::new();
    for id in ["msg-1", "msg-2", "msg-3"] {
        store_message(
            &repo,
            OutboxMessage::create(id, "Event", b"{}".to_vec()).unwrap(),
        )
        .await;
    }

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            3,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let claims = claimed
        .iter()
        .map(OutboxClaimRef::from_message)
        .collect::<Result<Vec<_>, _>>()
        .unwrap();

    store.complete_many(&claims).await.unwrap();

    for id in ["msg-1", "msg-2", "msg-3"] {
        assert!(load_message(&repo, id).is_published());
    }
}

#[tokio::test]
async fn complete_many_rejects_stale_and_missing_claims() {
    let repo = InMemoryRepository::new();
    store_message(
        &repo,
        OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap(),
    )
    .await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let claims = vec![OutboxClaimRef::from_message(&claimed[0]).unwrap()];

    store.complete_many(&claims).await.unwrap();
    // Re-settling the now-published row is a stale claim, same as `complete`.
    let err = store.complete_many(&claims).await.unwrap_err();
    assert!(matches!(err, RepositoryError::InvalidState { .. }));

    let missing = vec![OutboxClaimRef {
        message_id: "missing".into(),
        worker_id: "worker-1".into(),
        leased_until: SystemTime::now(),
        attempt: 1,
    }];
    let err = store.complete_many(&missing).await.unwrap_err();
    assert!(matches!(err, RepositoryError::NotFound { id } if id == "missing"));
}

#[tokio::test]
async fn already_published_message_is_not_completed_again() {
    let repo = InMemoryRepository::new();
    let message = OutboxMessage::create("msg-1", "Event", b"{}".to_vec()).unwrap();
    let _id = store_message(&repo, message).await;

    let store = repo.outbox_store();
    let claimed = store
        .claim(ClaimOutboxMessages::new(
            "worker-1",
            1,
            Duration::from_secs(60),
        ))
        .await
        .unwrap();
    let claim = OutboxClaimRef::from_message(&claimed[0]).unwrap();
    store.complete(&claim).await.unwrap();

    let err = store.complete(&claim).await.unwrap_err();
    assert!(matches!(err, RepositoryError::InvalidState { .. }));
}