backbone-messaging 2.7.35

Event-driven messaging infrastructure for Backbone Framework
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Comprehensive tests for backbone-messaging
//!
//! Covers domain EventBus, IntegrationEventBus, EventEnvelope,
//! dead letter queue, retry logic, and error handling.

use backbone_messaging::{
    DomainEvent, EventBus, EventBusConfig, EventEnvelope, EventEnvelopeBuilder,
    EventError, CollectingHandler,
    IntegrationEvent, IntegrationEventBus, IntegrationBusConfig,
    IntegrationEventEnvelope, IntegrationEventHandler,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;

// ============================================================================
// Test Event Types
// ============================================================================

#[derive(Clone, Debug)]
struct OrderCreated {
    order_id: String,
    #[allow(dead_code)]
    customer: String,
}

impl DomainEvent for OrderCreated {
    fn event_type(&self) -> &'static str { "OrderCreated" }
    fn aggregate_id(&self) -> &str { &self.order_id }
}

/// A domain event that uses custom version
#[derive(Clone, Debug)]
struct CustomVersionEvent {
    id: String,
}

impl DomainEvent for CustomVersionEvent {
    fn event_type(&self) -> &'static str { "CustomVersionEvent" }
    fn aggregate_id(&self) -> &str { &self.id }
    fn version(&self) -> u32 { 3 }
}

// Integration event
#[derive(Clone, Debug, Serialize, Deserialize)]
struct UserRegistered {
    user_id: String,
    email: String,
    occurred_at: DateTime<Utc>,
}

impl IntegrationEvent for UserRegistered {
    fn event_type(&self) -> &'static str { "sapiens.user.registered" }
    fn source_context(&self) -> &'static str { "sapiens" }
    fn aggregate_id(&self) -> &str { &self.user_id }
    fn occurred_at(&self) -> DateTime<Utc> { self.occurred_at }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct UserDeleted {
    user_id: String,
    occurred_at: DateTime<Utc>,
}

impl IntegrationEvent for UserDeleted {
    fn event_type(&self) -> &'static str { "sapiens.user.deleted" }
    fn source_context(&self) -> &'static str { "sapiens" }
    fn aggregate_id(&self) -> &str { &self.user_id }
    fn occurred_at(&self) -> DateTime<Utc> { self.occurred_at }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct OrderPaid {
    order_id: String,
    occurred_at: DateTime<Utc>,
}

impl IntegrationEvent for OrderPaid {
    fn event_type(&self) -> &'static str { "billing.order.paid" }
    fn source_context(&self) -> &'static str { "billing" }
    fn aggregate_id(&self) -> &str { &self.order_id }
    fn occurred_at(&self) -> DateTime<Utc> { self.occurred_at }
}

// ============================================================================
// Test Handlers
// ============================================================================

/// A handler that always fails
struct FailingIntegrationHandler {
    attempt_count: Arc<AtomicU32>,
    patterns: Vec<&'static str>,
    retry: bool,
    retries: u32,
}

impl FailingIntegrationHandler {
    fn new(patterns: Vec<&'static str>) -> Self {
        Self {
            attempt_count: Arc::new(AtomicU32::new(0)),
            patterns,
            retry: true,
            retries: 3,
        }
    }

    fn no_retry(patterns: Vec<&'static str>) -> Self {
        Self {
            attempt_count: Arc::new(AtomicU32::new(0)),
            patterns,
            retry: false,
            retries: 1,
        }
    }

}

#[async_trait]
impl IntegrationEventHandler for FailingIntegrationHandler {
    async fn handle(&self, _envelope: IntegrationEventEnvelope) -> Result<(), EventError> {
        self.attempt_count.fetch_add(1, Ordering::SeqCst);
        Err(EventError::handler("FailingHandler", "intentional failure"))
    }

    fn event_patterns(&self) -> Vec<&'static str> {
        self.patterns.clone()
    }

    fn name(&self) -> &'static str {
        "FailingHandler"
    }

    fn should_retry(&self) -> bool {
        self.retry
    }

    fn max_retries(&self) -> u32 {
        self.retries
    }
}

/// A collecting handler for integration events
struct CollectingIntegrationHandler {
    count: Arc<AtomicU32>,
    patterns: Vec<&'static str>,
}

impl CollectingIntegrationHandler {
    fn new(patterns: Vec<&'static str>) -> Self {
        Self {
            count: Arc::new(AtomicU32::new(0)),
            patterns,
        }
    }

    fn count(&self) -> u32 {
        self.count.load(Ordering::SeqCst)
    }
}

#[async_trait]
impl IntegrationEventHandler for CollectingIntegrationHandler {
    async fn handle(&self, _envelope: IntegrationEventEnvelope) -> Result<(), EventError> {
        self.count.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }

    fn event_patterns(&self) -> Vec<&'static str> {
        self.patterns.clone()
    }

    fn name(&self) -> &'static str {
        "CollectingIntegrationHandler"
    }
}

// ============================================================================
// Domain EventBus Tests
// ============================================================================

// We need a single event type enum for the bus since EventBus<E> is generic
#[derive(Clone, Debug)]
enum TestDomainEvent {
    Created { id: String, #[allow(dead_code)] customer: String },
    Shipped { id: String },
}

impl DomainEvent for TestDomainEvent {
    fn event_type(&self) -> &'static str {
        match self {
            TestDomainEvent::Created { .. } => "OrderCreated",
            TestDomainEvent::Shipped { .. } => "OrderShipped",
        }
    }

    fn aggregate_id(&self) -> &str {
        match self {
            TestDomainEvent::Created { id, .. } => id,
            TestDomainEvent::Shipped { id } => id,
        }
    }
}

#[tokio::test]
async fn test_events_by_type() {
    let bus = EventBus::<TestDomainEvent>::with_config(EventBusConfig::with_persistence());

    bus.publish(TestDomainEvent::Created { id: "o1".into(), customer: "c1".into() }).await.unwrap();
    bus.publish(TestDomainEvent::Shipped { id: "o2".into() }).await.unwrap();
    bus.publish(TestDomainEvent::Created { id: "o3".into(), customer: "c2".into() }).await.unwrap();

    let created = bus.events_by_type("OrderCreated").await;
    assert_eq!(created.len(), 2, "Should have 2 OrderCreated events");

    let shipped = bus.events_by_type("OrderShipped").await;
    assert_eq!(shipped.len(), 1, "Should have 1 OrderShipped event");

    let none = bus.events_by_type("NonExistent").await;
    assert_eq!(none.len(), 0);
}

#[tokio::test]
async fn test_events_in_range() {
    let bus = EventBus::<OrderCreated>::with_config(EventBusConfig::with_persistence());

    let before = Utc::now();
    sleep(Duration::from_millis(50)).await;

    bus.publish(OrderCreated { order_id: "o1".into(), customer: "c1".into() }).await.unwrap();

    sleep(Duration::from_millis(50)).await;
    let middle = Utc::now();
    sleep(Duration::from_millis(50)).await;

    bus.publish(OrderCreated { order_id: "o2".into(), customer: "c2".into() }).await.unwrap();

    sleep(Duration::from_millis(50)).await;
    let after = Utc::now();

    // Full range
    let all = bus.events_in_range(before, after).await;
    assert_eq!(all.len(), 2, "Full range should contain all events");

    // Partial range (only first event)
    let first_half = bus.events_in_range(before, middle).await;
    assert_eq!(first_half.len(), 1, "First half should contain 1 event");
}

#[tokio::test]
async fn test_publish_all() {
    let bus = EventBus::<OrderCreated>::with_config(EventBusConfig::with_persistence());

    let events = vec![
        OrderCreated { order_id: "o1".into(), customer: "c1".into() },
        OrderCreated { order_id: "o2".into(), customer: "c2".into() },
        OrderCreated { order_id: "o3".into(), customer: "c3".into() },
    ];

    bus.publish_all(events).await.unwrap();

    let history = bus.history().await;
    assert_eq!(history.len(), 3, "publish_all should store all 3 events");
}

#[tokio::test]
async fn test_multiple_handlers_same_event() {
    let bus = EventBus::<OrderCreated>::new();
    let handler_a = Arc::new(CollectingHandler::<OrderCreated>::new());
    let handler_b = Arc::new(CollectingHandler::<OrderCreated>::new());

    bus.register_handler(handler_a.clone()).await;
    bus.register_handler(handler_b.clone()).await;

    bus.publish(OrderCreated { order_id: "o1".into(), customer: "c1".into() }).await.unwrap();

    sleep(Duration::from_millis(50)).await;

    assert_eq!(handler_a.count().await, 1, "Handler A should receive event");
    assert_eq!(handler_b.count().await, 1, "Handler B should receive event");
}

#[tokio::test]
async fn test_handler_count() {
    let bus = EventBus::<OrderCreated>::new();

    assert_eq!(bus.handler_count().await, 0);

    bus.register_handler(Arc::new(CollectingHandler::<OrderCreated>::new())).await;
    assert_eq!(bus.handler_count().await, 1);

    bus.register_handler(Arc::new(CollectingHandler::<OrderCreated>::new())).await;
    assert_eq!(bus.handler_count().await, 2);
}

#[tokio::test]
async fn test_clear_history() {
    let bus = EventBus::<OrderCreated>::with_config(EventBusConfig::with_persistence());

    bus.publish(OrderCreated { order_id: "o1".into(), customer: "c1".into() }).await.unwrap();
    bus.publish(OrderCreated { order_id: "o2".into(), customer: "c2".into() }).await.unwrap();

    assert_eq!(bus.history().await.len(), 2);

    bus.clear_history().await;
    assert_eq!(bus.history().await.len(), 0, "History should be empty after clear");
}

#[tokio::test]
async fn test_config_max_history_size() {
    let config = EventBusConfig {
        persist_events: true,
        max_history_size: 3,
        ..EventBusConfig::default()
    };
    let bus = EventBus::<OrderCreated>::with_config(config);

    for i in 0..5 {
        bus.publish(OrderCreated {
            order_id: format!("o{}", i),
            customer: format!("c{}", i),
        }).await.unwrap();
    }

    let history = bus.history().await;
    assert!(history.len() <= 3, "History should be trimmed to max_history_size");
}

// ============================================================================
// EventEnvelope Tests
// ============================================================================

#[tokio::test]
async fn test_envelope_builder() {
    let event = OrderCreated { order_id: "o1".into(), customer: "c1".into() };

    let envelope = EventEnvelopeBuilder::new(event)
        .correlation_id("corr-123")
        .causation_id("cause-456")
        .metadata("source", "test")
        .metadata("env", "dev")
        .build();

    assert_eq!(envelope.correlation_id, Some("corr-123".to_string()));
    assert_eq!(envelope.causation_id, Some("cause-456".to_string()));
    assert_eq!(envelope.metadata.get("source"), Some(&"test".to_string()));
    assert_eq!(envelope.metadata.get("env"), Some(&"dev".to_string()));
    assert_eq!(envelope.event_type, "OrderCreated");
    assert_eq!(envelope.aggregate_id, "o1");
}

#[tokio::test]
async fn test_envelope_correlation_tracking() {
    let event_a = OrderCreated { order_id: "o1".into(), customer: "c1".into() };
    let event_b = OrderCreated { order_id: "o2".into(), customer: "c2".into() };
    let event_c = OrderCreated { order_id: "o3".into(), customer: "c3".into() };

    let envelope_a = EventEnvelope::new(event_a)
        .with_correlation_id("corr-1");
    let envelope_b = EventEnvelope::new(event_b)
        .with_correlation_id("corr-1");
    let envelope_c = EventEnvelope::new(event_c)
        .with_correlation_id("corr-2");

    assert!(envelope_a.is_correlated_with(&envelope_b), "Same correlation ID");
    assert!(!envelope_a.is_correlated_with(&envelope_c), "Different correlation ID");

    // Causation tracking
    let parent = EventEnvelope::new(OrderCreated { order_id: "p1".into(), customer: "c".into() });
    let parent_id = parent.id.clone();
    let child = EventEnvelope::new(OrderCreated { order_id: "c1".into(), customer: "c".into() })
        .with_causation_id(parent_id);

    assert!(child.was_caused_by(&parent), "Child should be caused by parent");
}

#[tokio::test]
async fn test_event_error_constructors() {
    let handler_err = EventError::handler("MyHandler", "something failed");
    assert!(format!("{}", handler_err).contains("MyHandler"));
    assert!(format!("{}", handler_err).contains("something failed"));

    let publish_err = EventError::publish("channel full");
    assert!(format!("{}", publish_err).contains("channel full"));

    let ser_err = EventError::serialization("invalid JSON");
    assert!(format!("{}", ser_err).contains("invalid JSON"));
}

#[tokio::test]
async fn test_domain_event_defaults() {
    let event = OrderCreated { order_id: "o1".into(), customer: "c1".into() };

    // Default version is 1
    assert_eq!(event.version(), 1);

    // aggregate_type() defaults to event_type for events ending with known suffixes
    let agg_type = event.aggregate_type();
    assert_eq!(agg_type, "OrderCreated");

    // Custom version
    let custom = CustomVersionEvent { id: "x".into() };
    assert_eq!(custom.version(), 3);
}

// ============================================================================
// IntegrationEventBus Tests
// ============================================================================

#[tokio::test]
async fn test_dead_letter_queue() {
    let bus = IntegrationEventBus::new(); // default config has dead letter enabled

    let handler = Arc::new(FailingIntegrationHandler::new(vec!["sapiens.*"]));
    bus.register_handler(handler.clone()).await;

    let event = UserRegistered {
        user_id: "u1".into(),
        email: "test@test.com".into(),
        occurred_at: Utc::now(),
    };
    bus.publish(event).await.unwrap();

    // Wait for retry backoff to complete
    sleep(Duration::from_millis(1000)).await;

    let dead_letters = bus.dead_letters().await;
    assert_eq!(dead_letters.len(), 1, "Should have 1 dead letter entry");
    assert_eq!(dead_letters[0].handler_name, "FailingHandler");
    assert!(dead_letters[0].error.contains("intentional failure"));

    // Clear dead letters
    bus.clear_dead_letters().await;
    assert_eq!(bus.dead_letters().await.len(), 0, "Dead letters should be cleared");
}

#[tokio::test]
async fn test_handler_retry_exhausted() {
    let bus = IntegrationEventBus::new();

    let handler = Arc::new(FailingIntegrationHandler::new(vec!["sapiens.*"]));
    let attempt_count = handler.attempt_count.clone();
    bus.register_handler(handler).await;

    let event = UserRegistered {
        user_id: "u2".into(),
        email: "retry@test.com".into(),
        occurred_at: Utc::now(),
    };
    bus.publish(event).await.unwrap();

    sleep(Duration::from_millis(1000)).await;

    // Default max_retries is 3, so should have 3 attempts
    assert_eq!(attempt_count.load(Ordering::SeqCst), 3, "Should retry 3 times");
}

#[tokio::test]
async fn test_handler_no_retry() {
    let bus = IntegrationEventBus::new();

    let handler = Arc::new(FailingIntegrationHandler::no_retry(vec!["sapiens.*"]));
    let attempt_count = handler.attempt_count.clone();
    bus.register_handler(handler).await;

    let event = UserRegistered {
        user_id: "u3".into(),
        email: "noretry@test.com".into(),
        occurred_at: Utc::now(),
    };
    bus.publish(event).await.unwrap();

    sleep(Duration::from_millis(100)).await;

    assert_eq!(attempt_count.load(Ordering::SeqCst), 1, "Should only attempt once (no retry)");
}

#[tokio::test]
async fn test_handler_deduplication() {
    let bus = IntegrationEventBus::new();

    // Handler matches both patterns, but should be called only once per event
    let handler = Arc::new(CollectingIntegrationHandler::new(vec!["sapiens.*", "sapiens.user.*"]));
    bus.register_handler(handler.clone()).await;

    let event = UserRegistered {
        user_id: "u4".into(),
        email: "dedup@test.com".into(),
        occurred_at: Utc::now(),
    };
    bus.publish(event).await.unwrap();

    sleep(Duration::from_millis(50)).await;

    assert_eq!(handler.count(), 1, "Handler should be called exactly once despite matching multiple patterns");
}

#[tokio::test]
async fn test_events_by_pattern() {
    let bus = IntegrationEventBus::new();

    bus.publish(UserRegistered {
        user_id: "u1".into(), email: "a@test.com".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    bus.publish(UserDeleted {
        user_id: "u2".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    bus.publish(OrderPaid {
        order_id: "o1".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    let user_events = bus.events_by_pattern("sapiens.user.*").await;
    assert_eq!(user_events.len(), 2, "Should match both sapiens.user.* events");

    let billing_events = bus.events_by_pattern("billing.*").await;
    assert_eq!(billing_events.len(), 1, "Should match 1 billing event");

    let all_events = bus.events_by_pattern("*").await;
    assert_eq!(all_events.len(), 3, "Wildcard should match all events");
}

#[tokio::test]
async fn test_events_for_aggregate() {
    let bus = IntegrationEventBus::new();

    bus.publish(UserRegistered {
        user_id: "user-1".into(), email: "a@test.com".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    bus.publish(UserDeleted {
        user_id: "user-1".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    bus.publish(UserRegistered {
        user_id: "user-2".into(), email: "b@test.com".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    let events = bus.events_for_aggregate("user-1").await;
    assert_eq!(events.len(), 2, "Should find 2 events for user-1");

    let events = bus.events_for_aggregate("user-2").await;
    assert_eq!(events.len(), 1, "Should find 1 event for user-2");
}

#[tokio::test]
async fn test_concurrent_publishing() {
    let bus = Arc::new(IntegrationEventBus::new());
    let mut handles = vec![];

    for i in 0..10 {
        let bus = bus.clone();
        let handle = tokio::spawn(async move {
            bus.publish(UserRegistered {
                user_id: format!("concurrent-{}", i),
                email: format!("user{}@test.com", i),
                occurred_at: Utc::now(),
            }).await.unwrap();
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.await.unwrap();
    }

    let history = bus.history().await;
    assert_eq!(history.len(), 10, "All 10 concurrent events should be in history");
}

#[tokio::test]
async fn test_config_disables_persistence() {
    let config = IntegrationBusConfig {
        persist_events: false,
        ..IntegrationBusConfig::default()
    };
    let bus = IntegrationEventBus::with_config(config);

    bus.publish(UserRegistered {
        user_id: "u1".into(), email: "a@test.com".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    let history = bus.history().await;
    assert_eq!(history.len(), 0, "History should be empty when persistence is disabled");
}

#[tokio::test]
async fn test_config_disables_dead_letter() {
    let config = IntegrationBusConfig {
        enable_dead_letter_queue: false,
        ..IntegrationBusConfig::default()
    };
    let bus = IntegrationEventBus::with_config(config);

    let handler = Arc::new(FailingIntegrationHandler::new(vec!["sapiens.*"]));
    bus.register_handler(handler).await;

    bus.publish(UserRegistered {
        user_id: "u1".into(), email: "a@test.com".into(), occurred_at: Utc::now(),
    }).await.unwrap();

    sleep(Duration::from_millis(1000)).await;

    let dead_letters = bus.dead_letters().await;
    assert_eq!(dead_letters.len(), 0, "No dead letters when dead letter queue is disabled");
}