ruststream 0.2.2

Async messaging framework for Rust: broker-agnostic traits, router, codecs, and a conformance harness for broker authors.
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
//! Integration tests for `RustStream` lifecycle and dispatch, using `MemoryBroker`.

use std::{
    sync::{
        Arc, Mutex,
        atomic::{AtomicU32, Ordering},
    },
    time::Duration,
};

use ruststream::codec::JsonCodec;
use ruststream::memory::MemoryBroker;
use ruststream::runtime::{
    AppInfo, Context, Handler, HandlerMetadata, HandlerResult, Layer, Outgoing, PublishMiddleware,
    PublishNext, Router, RustStream, State,
};
use ruststream::{Name, OutgoingMessage, Publisher};
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::pin::Pin;
use tokio::sync::Notify;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Order {
    id: u32,
    total: f64,
}

fn order_bytes(id: u32, total: f64) -> Vec<u8> {
    serde_json::to_vec(&Order { id, total }).unwrap()
}

/// A test layer that counts every invocation, to prove the global stack wraps handlers.
#[derive(Clone)]
struct CountLayer(Arc<AtomicU32>);

struct CountHandler<H> {
    inner: H,
    count: Arc<AtomicU32>,
}

impl<H> Layer<H> for CountLayer {
    type Handler = CountHandler<H>;

    fn layer(&self, inner: H) -> CountHandler<H> {
        CountHandler {
            inner,
            count: Arc::clone(&self.0),
        }
    }
}

impl<M, H> Handler<M> for CountHandler<H>
where
    M: Sync,
    H: Handler<M>,
{
    async fn handle(&self, msg: &M, ctx: &mut Context<'_>) -> HandlerResult {
        self.count.fetch_add(1, Ordering::SeqCst);
        self.inner.handle(msg, ctx).await
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn app_dispatches_typed_messages() {
    let broker = MemoryBroker::new();
    let publisher = broker.publisher();

    let received = Arc::new(AtomicU32::new(0));
    let received_clone = Arc::clone(&received);

    let handler =
        ruststream::runtime::typed(JsonCodec, move |order: &Order, _ctx: &mut Context| {
            let received = Arc::clone(&received_clone);
            let total = order.total;
            let id = order.id;
            async move {
                assert!(total > 0.0);
                received.fetch_add(id, Ordering::SeqCst);
                HandlerResult::Ack
            }
        });

    let app = RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(broker, |b| {
        // Subscribe up front so messages published after run() starts are buffered, not lost.
        let subscriber = b.broker().subscribe("orders");
        b.handle(
            subscriber,
            handler,
            HandlerMetadata::typed::<Order>("orders"),
        );
    });

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    publisher
        .publish(OutgoingMessage::new("orders", &order_bytes(7, 9.99)))
        .await
        .unwrap();
    publisher
        .publish(OutgoingMessage::new("orders", &order_bytes(3, 1.0)))
        .await
        .unwrap();

    wait_for(
        || received.load(Ordering::SeqCst) == 10,
        Duration::from_secs(1),
    )
    .await;

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn app_subscribes_via_descriptor_after_connect() {
    let broker = MemoryBroker::new();
    let publisher = broker.publisher();

    let seen = Arc::new(AtomicU32::new(0));
    let seen_clone = Arc::clone(&seen);

    let app = RustStream::new(AppInfo::new("events", "0.1.0")).with_broker(broker, |b| {
        b.subscribe(
            Name::new("events"),
            move |_msg: &_, _ctx: &mut Context| {
                let seen = Arc::clone(&seen_clone);
                async move {
                    seen.fetch_add(1, Ordering::SeqCst);
                    HandlerResult::Ack
                }
            },
            HandlerMetadata::raw("events"),
        );
    });

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    // The descriptor subscribes inside run(); retry publishing until the subscription is live.
    wait_for_published(&publisher, &seen, Duration::from_secs(1)).await;

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn included_router_handlers_dispatch() {
    let broker = MemoryBroker::new();
    let publisher = broker.publisher();

    let seen = Arc::new(AtomicU32::new(0));
    let seen_clone = Arc::clone(&seen);

    // Router defined independently of any live broker, then mounted.
    let mut router = Router::<MemoryBroker>::new();
    router.subscribe(
        Name::new("events"),
        move |_msg: &_, _ctx: &mut Context| {
            let seen = Arc::clone(&seen_clone);
            async move {
                seen.fetch_add(1, Ordering::SeqCst);
                HandlerResult::Ack
            }
        },
        HandlerMetadata::raw("events"),
    );

    let app = RustStream::new(AppInfo::new("events", "0.1.0"))
        .with_broker(broker, |b| b.include_router(router));

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    wait_for_published(&publisher, &seen, Duration::from_secs(1)).await;

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn global_layer_wraps_handlers() {
    let broker = MemoryBroker::new();
    let publisher = broker.publisher();

    let layer_hits = Arc::new(AtomicU32::new(0));
    let handler_hits = Arc::new(AtomicU32::new(0));
    let handler_hits_clone = Arc::clone(&handler_hits);

    let app = RustStream::new(AppInfo::new("svc", "0.1.0"))
        .layer(CountLayer(Arc::clone(&layer_hits)))
        .with_broker(broker, |b| {
            let subscriber = b.broker().subscribe("orders");
            b.handle(
                subscriber,
                move |_msg: &_, _ctx: &mut Context| {
                    let handler_hits = Arc::clone(&handler_hits_clone);
                    async move {
                        handler_hits.fetch_add(1, Ordering::SeqCst);
                        HandlerResult::Ack
                    }
                },
                HandlerMetadata::raw("orders"),
            );
        });

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    publisher
        .publish(OutgoingMessage::new("orders", b"x"))
        .await
        .unwrap();

    wait_for(
        || handler_hits.load(Ordering::SeqCst) == 1 && layer_hits.load(Ordering::SeqCst) == 1,
        Duration::from_secs(1),
    )
    .await;

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cross_broker_publish_via_named_publisher() {
    let ingress = MemoryBroker::new();
    let egress = MemoryBroker::new();
    let ingress_pub = ingress.publisher();

    let received = Arc::new(AtomicU32::new(0));
    let received_clone = Arc::clone(&received);

    let app = RustStream::new(AppInfo::new("bridge", "0.1.0"))
        .publisher("egress", egress.publisher())
        .with_broker(ingress, |b| {
            let out = b.publisher("egress").expect("egress registered");
            b.subscribe(
                Name::new("orders"),
                move |_msg: &_, _ctx: &mut Context| {
                    let out = Arc::clone(&out);
                    async move {
                        let _ = out.publish_bytes("responses", b"reply").await;
                        HandlerResult::Ack
                    }
                },
                HandlerMetadata::raw("orders"),
            );
        })
        .with_broker(egress, |b| {
            let subscriber = b.broker().subscribe("responses");
            b.handle(
                subscriber,
                move |_msg: &_, _ctx: &mut Context| {
                    let received = Arc::clone(&received_clone);
                    async move {
                        received.fetch_add(1, Ordering::SeqCst);
                        HandlerResult::Ack
                    }
                },
                HandlerMetadata::raw("responses"),
            );
        });

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    // ingress "orders" subscribes inside run() (deferred); retry until the bridge fires.
    let result = tokio::time::timeout(Duration::from_secs(1), async {
        loop {
            let _ = ingress_pub
                .publish(OutgoingMessage::new("orders", b"x"))
                .await;
            tokio::time::sleep(Duration::from_millis(10)).await;
            if received.load(Ordering::SeqCst) >= 1 {
                break;
            }
        }
    })
    .await;
    assert!(
        result.is_ok(),
        "cross-broker publish did not arrive on egress"
    );

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}

struct Config {
    greeting: String,
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn handler_reads_context_topic_and_state() {
    let broker = MemoryBroker::new();
    let publisher = broker.publisher();

    let seen = Arc::new(Mutex::new(None::<(String, String)>));
    let seen_clone = Arc::clone(&seen);

    let app = RustStream::new(AppInfo::new("svc", "0.1.0"))
        .insert_state(Config {
            greeting: "hello".to_owned(),
        })
        .with_broker(broker, |b| {
            let subscriber = b.broker().subscribe("orders");
            b.handle(
                subscriber,
                move |_msg: &_, ctx: &mut Context| {
                    let name = ctx.name().to_owned();
                    let greeting = ctx.get::<Config>().map(|c| c.greeting.clone());
                    // Middleware/handlers may enrich the working headers.
                    ctx.headers_mut().insert("x-seen", b"1".to_vec());
                    let seen = Arc::clone(&seen_clone);
                    async move {
                        *seen.lock().expect("poisoned") =
                            Some((name, greeting.unwrap_or_default()));
                        HandlerResult::Ack
                    }
                },
                HandlerMetadata::raw("orders"),
            );
        });

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    publisher
        .publish(OutgoingMessage::new("orders", b"x"))
        .await
        .unwrap();

    wait_for(
        || seen.lock().expect("poisoned").is_some(),
        Duration::from_secs(1),
    )
    .await;
    assert_eq!(
        *seen.lock().expect("poisoned"),
        Some(("orders".to_owned(), "hello".to_owned())),
    );

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifespan_hooks_run_in_order() {
    let order = Arc::new(Mutex::new(Vec::<&'static str>::new()));
    let (o1, o2, o3, o4) = (
        Arc::clone(&order),
        Arc::clone(&order),
        Arc::clone(&order),
        Arc::clone(&order),
    );

    let app = RustStream::new(AppInfo::new("svc", "0.1.0"))
        .shutdown_timeout(Duration::from_secs(5))
        .on_startup(move |mut state: State| {
            let o1 = Arc::clone(&o1);
            async move {
                state.insert(Config {
                    greeting: "lazy".to_owned(),
                });
                o1.lock().expect("poisoned").push("startup");
                Ok::<State, std::convert::Infallible>(state)
            }
        })
        .after_startup(move |_state| {
            let o2 = Arc::clone(&o2);
            async move {
                o2.lock().expect("poisoned").push("after_startup");
                Ok::<(), std::convert::Infallible>(())
            }
        })
        .on_shutdown(move |_state| {
            let o3 = Arc::clone(&o3);
            async move {
                o3.lock().expect("poisoned").push("on_shutdown");
                Ok::<(), std::convert::Infallible>(())
            }
        })
        .after_shutdown(move |state| {
            let o4 = Arc::clone(&o4);
            let greeting = state.get::<Config>().map(|c| c.greeting.clone());
            async move {
                assert_eq!(greeting.as_deref(), Some("lazy"));
                o4.lock().expect("poisoned").push("after_shutdown");
                Ok::<(), std::convert::Infallible>(())
            }
        })
        .with_broker(MemoryBroker::new(), |_b| {});

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    wait_for(
        || order.lock().expect("poisoned").contains(&"after_startup"),
        Duration::from_secs(1),
    )
    .await;
    shutdown.notify_one();
    run.await.unwrap().unwrap();

    assert_eq!(
        *order.lock().expect("poisoned"),
        vec!["startup", "after_startup", "on_shutdown", "after_shutdown"],
    );
}

#[test]
fn app_records_handler_metadata() {
    let broker = MemoryBroker::new();
    let app = RustStream::new(AppInfo::new("svc", "0.1.0")).with_broker(broker, |b| {
        let subscriber = b.broker().subscribe("orders");
        b.handle(
            subscriber,
            |_msg: &_, _ctx: &mut Context| async { HandlerResult::Ack },
            HandlerMetadata::typed::<Order>("orders").with_description("processes orders"),
        );
        let alerts = b.broker().subscribe("alerts");
        b.handle(
            alerts,
            |_msg: &_, _ctx: &mut Context| async { HandlerResult::Ack },
            HandlerMetadata::raw("alerts"),
        );
    });

    assert_eq!(app.handlers().len(), 2);
    assert_eq!(app.handlers()[0].name, "orders");
    assert_eq!(
        app.handlers()[0].description.as_deref(),
        Some("processes orders"),
    );
    assert_eq!(app.handlers()[1].input_type, "bytes");
    assert_eq!(app.info().title, "svc");
}

async fn wait_for(mut cond: impl FnMut() -> bool, timeout: Duration) {
    let result = tokio::time::timeout(timeout, async {
        while !cond() {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await;
    assert!(result.is_ok(), "condition not met within {timeout:?}");
}

async fn wait_for_published(publisher: &impl Publisher, seen: &AtomicU32, timeout: Duration) {
    let result = tokio::time::timeout(timeout, async {
        loop {
            let _ = publisher
                .publish(OutgoingMessage::new("events", b"ping"))
                .await;
            tokio::time::sleep(Duration::from_millis(10)).await;
            if seen.load(Ordering::SeqCst) >= 1 {
                break;
            }
        }
    })
    .await;
    assert!(result.is_ok(), "no delivery within {timeout:?}");
}

/// A publish middleware that tags every outgoing message with a header.
struct Tagger;

impl PublishMiddleware for Tagger {
    fn on_publish<'a>(
        &'a self,
        out: &'a mut Outgoing,
        next: PublishNext<'a>,
    ) -> Pin<
        Box<dyn Future<Output = Result<(), Box<dyn std::error::Error + Send + Sync>>> + Send + 'a>,
    > {
        Box::pin(async move {
            out.headers_mut().insert("x-envelope", b"1".to_vec());
            next.run(out).await
        })
    }
}

/// A handler that publishes manually via `ctx.publisher`. A struct (not a closure) so the future
/// may borrow `ctx` across the await.
struct Bridge;

impl<M: Send + Sync> Handler<M> for Bridge {
    async fn handle(&self, _msg: &M, ctx: &mut Context<'_>) -> HandlerResult {
        if let Some(out) = ctx.publisher("egress") {
            let _ = out
                .publish(Outgoing::new("responses", b"reply".to_vec()))
                .await;
        }
        HandlerResult::Ack
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ctx_publisher_runs_through_pipeline() {
    let ingress = MemoryBroker::new();
    let egress = MemoryBroker::new();
    let ingress_pub = ingress.publisher();

    let tagged = Arc::new(AtomicU32::new(0));
    let tagged_clone = Arc::clone(&tagged);

    let app = RustStream::new(AppInfo::new("bridge", "0.1.0"))
        .publisher("egress", egress.publisher())
        .publish_layer(Tagger)
        .with_broker(ingress, |b| {
            b.subscribe(Name::new("orders"), Bridge, HandlerMetadata::raw("orders"));
        })
        .with_broker(egress, |b| {
            let subscriber = b.broker().subscribe("responses");
            b.handle(
                subscriber,
                move |msg: &_, _ctx: &mut Context| {
                    let tagged = Arc::clone(&tagged_clone);
                    // The Tagger middleware must have run on the manual publish.
                    let has_header = ruststream::IncomingMessage::headers(msg)
                        .get("x-envelope")
                        .is_some();
                    async move {
                        if has_header {
                            tagged.fetch_add(1, Ordering::SeqCst);
                        }
                        HandlerResult::Ack
                    }
                },
                HandlerMetadata::raw("responses"),
            );
        });

    let shutdown = Arc::new(Notify::new());
    let shutdown_signal = Arc::clone(&shutdown);
    let run = tokio::spawn(app.run_until(async move { shutdown_signal.notified().await }));

    let result = tokio::time::timeout(Duration::from_secs(1), async {
        loop {
            let _ = ingress_pub
                .publish(OutgoingMessage::new("orders", b"x"))
                .await;
            tokio::time::sleep(Duration::from_millis(10)).await;
            if tagged.load(Ordering::SeqCst) >= 1 {
                break;
            }
        }
    })
    .await;
    assert!(
        result.is_ok(),
        "manual publish did not reach egress with the pipeline header",
    );

    shutdown.notify_one();
    run.await.unwrap().unwrap();
}