rust_bus 3.0.6

bus — Lightweight CQRS Library for Rust
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
#[cfg(test)]
mod tests {
    use crate as rust_bus;
    use crate::bus;
    use crate::contracts::meta::BusMetadata;
    use crate::tests::base::setup_bus;
    use crate::workers::configuration::BusQueueConfiguration;
    use once_cell::sync::Lazy;
    use rust_bus::{BusEvent, BusEventHandler};
    use sea_orm::{ConnectionTrait, QueryResult, Statement, TransactionTrait};
    use tokio::sync::Mutex;
    use uuid::Uuid;

    static TEST_1: Lazy<Mutex<String>> = Lazy::new(|| Mutex::new(String::new()));
    static TEST_2: Lazy<Mutex<usize>> = Lazy::new(|| Mutex::new(0));
    static TEST_3: Lazy<Mutex<BusMetadata>> = Lazy::new(|| Mutex::new(BusMetadata::default()));

    #[BusEvent]
    pub struct Test1Event {
        pub msg: String,
    }

    #[BusEventHandler]
    async fn handle_test_event(
        _txn: &sea_orm::DatabaseTransaction,
        event: &Test1Event,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = TEST_1.lock().await;
        *data = event.msg.to_owned();
        Ok(())
    }

    #[tokio::test]
    async fn test_dispatch() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        bus::event(
            &txn,
            Test1Event {
                msg: "ok".to_string(),
            },
            &BusMetadata::default(),
        )
        .await
        .unwrap();

        assert_eq!(TEST_1.lock().await.as_str(), "ok");
    }

    #[BusEvent]
    pub struct Test2Event {}

    #[BusEventHandler]
    async fn handle2test1(
        _txn: &sea_orm::DatabaseTransaction,
        _event: &Test2Event,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = TEST_2.lock().await;
        *data += 1;
        Ok(())
    }

    #[BusEventHandler]
    async fn handle2test2(
        _txn: &sea_orm::DatabaseTransaction,
        _event: &Test2Event,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = TEST_2.lock().await;
        *data += 2;
        Ok(())
    }

    #[tokio::test]
    async fn test_dispatch_multiple_handlers() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        bus::event(&txn, Test2Event {}, &BusMetadata::default())
            .await
            .unwrap();
        assert_eq!(TEST_2.lock().await.clone(), 3);
    }

    #[BusEvent]
    pub struct Test3Event {}

    #[BusEventHandler]
    async fn test_context_handle<'a>(
        _txn: &sea_orm::DatabaseTransaction,
        _event: &Test3Event,
        meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = TEST_3.lock().await;
        *data = meta.clone();
        Ok(())
    }

    #[tokio::test]
    async fn test_context() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();

        let request_id = Uuid::now_v7();
        let correlation_id = Uuid::now_v7();
        let causation_id = Uuid::now_v7();
        let extra_1 = Uuid::now_v7();
        let extra_2 = Uuid::now_v7();

        let mut meta = BusMetadata::default();
        meta.add("request_id", request_id).unwrap();
        meta.add("correlation_id", correlation_id).unwrap();
        meta.add("causation_id", causation_id).unwrap();
        meta.add("extra_1", extra_1).unwrap();
        meta.add("extra_2", extra_2).unwrap();

        let txn = pool.begin().await.unwrap();

        bus::event(&txn, Test3Event {}, &meta).await.unwrap();
        let res_meta = TEST_3.lock().await.clone();

        assert_eq!(res_meta.request_id, Some(request_id));
        assert_eq!(res_meta.correlation_id, Some(correlation_id));
        assert_eq!(res_meta.causation_id, Some(causation_id));
        assert_eq!(res_meta.get::<Uuid>("request_id"), None);
        assert_eq!(res_meta.get::<Uuid>("correlation_id"), None);
        assert_eq!(res_meta.get::<Uuid>("causation_id"), None);
        assert_eq!(res_meta.get::<Uuid>("extra_1"), Some(extra_1));
        assert_eq!(res_meta.get::<Uuid>("extra_2"), Some(extra_2));
    }

    #[BusEvent]
    pub struct DbSuccessEvent {
        pub id: i32,
        pub data: String,
    }

    #[BusEventHandler]
    async fn db_success_handle(
        txn: &sea_orm::DatabaseTransaction,
        event: &DbSuccessEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        #[cfg(feature = "sea-orm-postgres")]
        let sql = "INSERT INTO temp_test (id, val) VALUES ($1, $2)";
        #[cfg(feature = "sea-orm-mysql")]
        let sql = "INSERT INTO temp_test (id, val) VALUES (?, ?)";

        txn.execute_raw(Statement::from_sql_and_values(
            txn.get_database_backend(),
            sql,
            vec![event.id.into(), event.data.clone().into()],
        ))
        .await?;
        Ok(())
    }

    #[BusEvent]
    pub struct DbRollbackEvent {
        pub id: i32,
    }

    #[BusEventHandler]
    async fn db_rollback_handle<'a>(
        txn: &sea_orm::DatabaseTransaction,
        event: &DbRollbackEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        #[cfg(feature = "sea-orm-postgres")]
        let sql = "INSERT INTO temp_test (id, val) VALUES ($1, $2)";
        #[cfg(feature = "sea-orm-mysql")]
        let sql = "INSERT INTO temp_test (id, val) VALUES (?, ?)";

        txn.execute_raw(Statement::from_sql_and_values(
            txn.get_database_backend(),
            sql,
            vec![event.id.into(), "should_be_rolled_back".into()],
        ))
        .await?;

        Err("error executing DBROLLBACK".into())
    }

    async fn setup_temp_table() {
        let pool = BusQueueConfiguration::global().unwrap().get_connection();

        pool.execute_unprepared(
            &"CREATE TABLE IF NOT EXISTS temp_test (id INT PRIMARY KEY, val TEXT)",
        )
        .await
        .unwrap();

        pool.execute_unprepared(&"truncate temp_test")
            .await
            .unwrap();

        #[cfg(feature = "sea-orm-mysql")]
        pool.execute_unprepared(&"ALTER TABLE temp_test AUTO_INCREMENT = 1")
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_transaction_commit_behavior() {
        setup_bus().await;
        setup_temp_table().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();

        let txn = pool.begin().await.unwrap();

        {
            bus::event(
                &txn,
                DbSuccessEvent {
                    id: 1,
                    data: "hello".into(),
                },
                &BusMetadata::default(),
            )
            .await
            .unwrap();
        }

        let row: Option<QueryResult> = txn
            .query_one_raw(Statement::from_string(
                txn.get_database_backend(),
                "SELECT val FROM temp_test WHERE id = 1",
            ))
            .await
            .unwrap();

        let query_res = row.unwrap();
        let val: String = query_res.try_get("", "val").unwrap();
        assert_eq!(val, "hello".to_string());

        txn.commit().await.unwrap();

        let row: Option<QueryResult> = pool
            .query_one_raw(Statement::from_string(
                pool.get_database_backend(),
                "SELECT val FROM temp_test WHERE id = 1",
            ))
            .await
            .unwrap();

        let query_res = row.unwrap();
        let val: String = query_res.try_get("", "val").unwrap();
        assert_eq!(val, "hello".to_string());
    }

    #[tokio::test]
    async fn test_transaction_rollback_behavior() {
        setup_bus().await;
        setup_temp_table().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        {
            let result =
                bus::event(&txn, DbRollbackEvent { id: 99 }, &BusMetadata::default()).await;

            assert!(result.is_err());
        }

        txn.rollback().await.unwrap();

        let check_txn = pool.begin().await.unwrap();

        let exists_row: Option<QueryResult> = check_txn
            .query_one_raw(Statement::from_string(
                check_txn.get_database_backend(),
                "SELECT id FROM temp_test WHERE id = 99",
            ))
            .await
            .unwrap();

        let exists = exists_row.is_some();

        assert!(!exists, "Дані не повинні були зберегтися після rollback");
    }

    #[BusEvent]
    pub struct ParentEvent {}
    #[BusEvent]
    pub struct ChildEvent {}

    static NESTED_COUNTER: Lazy<Mutex<usize>> = Lazy::new(|| Mutex::new(0));

    #[BusEventHandler]
    async fn handle_parent(
        txn: &sea_orm::DatabaseTransaction,
        _e: &ParentEvent,
        meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        bus::event(txn, ChildEvent {}, meta).await?;
        Ok(())
    }

    #[BusEventHandler]
    async fn handle_child(
        _txn: &sea_orm::DatabaseTransaction,
        _e: &ChildEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut count = NESTED_COUNTER.lock().await;
        *count += 1;
        Ok(())
    }

    #[tokio::test]
    async fn test_nested_dispatch() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        bus::event(&txn, ParentEvent {}, &BusMetadata::default())
            .await
            .unwrap();

        assert_eq!(
            *NESTED_COUNTER.lock().await,
            1,
            "Child handler should be called by Parent handler"
        );
    }

    #[tokio::test]
    async fn test_event_lifetime_safety() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        {
            let local_event = Test1Event {
                msg: "temporary".to_string(),
            };
            bus::event(&txn, local_event, &BusMetadata::default())
                .await
                .unwrap();
        }

        assert_eq!(TEST_1.lock().await.as_str(), "temporary");
    }

    #[tokio::test]
    async fn test_deep_nested_dispatch() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        *NESTED_COUNTER.lock().await = 0;

        for _ in 0..100 {
            bus::event(&txn, ParentEvent {}, &BusMetadata::default())
                .await
                .unwrap();
        }

        assert_eq!(*NESTED_COUNTER.lock().await, 100);
    }

    static STOP_COUNTER: Lazy<Mutex<usize>> = Lazy::new(|| Mutex::new(0));

    #[BusEvent]
    pub struct StopChainEvent {}

    #[BusEventHandler]
    async fn handle_fail_a(
        _txn: &sea_orm::DatabaseTransaction,
        _e: &StopChainEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut count = STOP_COUNTER.lock().await;
        *count += 1;
        Err("Fail A".into())
    }

    #[BusEventHandler]
    async fn handle_fail_b(
        _txn: &sea_orm::DatabaseTransaction,
        _e: &StopChainEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut count = STOP_COUNTER.lock().await;
        *count += 1;
        Err("Fail B".into())
    }

    #[tokio::test]
    async fn test_handler_chain_stops_immediately() {
        setup_bus().await;
        {
            let mut count = STOP_COUNTER.lock().await;
            *count = 0;
        }

        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        let result = bus::event(&txn, StopChainEvent {}, &BusMetadata::default()).await;
        assert!(result.is_err());

        assert_eq!(
            *STOP_COUNTER.lock().await,
            1,
            "Має відпрацювати рівно один хендлер до зупинки ланцюжка"
        );
    }

    #[BusEvent]
    pub struct PanicEvent {}

    #[BusEventHandler]
    async fn handle_panic_1(
        _txn: &sea_orm::DatabaseTransaction,
        _e: &PanicEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        panic!("Boom!");
    }

    #[tokio::test]
    async fn test_handler_panic_safety() {
        setup_bus().await;

        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let txn = pool.begin().await.unwrap();

        let result = bus::event(&txn, PanicEvent {}, &BusMetadata::default()).await;

        assert!(
            result.is_err(),
            "Диспетчер мав повернути Err після паніки хендлера"
        );

        let err_msg = format!("{:?}", result.err().unwrap());
        assert!(
            err_msg.contains("Boom!"),
            "Помилка повинна містити текст паніки"
        );
    }

    #[BusEvent]
    pub struct SimpleMutEvent {
        pub new_val: usize,
    }

    #[BusEventHandler]
    async fn handle_simple_mut<'a>(
        _txn: &sea_orm::DatabaseTransaction,
        event: &SimpleMutEvent,
        _meta: &BusMetadata,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = TEST_2.lock().await;
        *data = event.new_val;

        Ok(())
    }

    #[tokio::test]
    async fn test_basic_mutable_context_passing() {
        setup_bus().await;
        let pool = BusQueueConfiguration::global().unwrap().get_connection();
        let mut txn = pool.begin().await.unwrap();

        bus::event(
            &mut txn,
            SimpleMutEvent { new_val: 42 },
            &BusMetadata::default(),
        )
        .await
        .unwrap();

        assert_eq!(*TEST_2.lock().await, 42);
    }
}