velo 0.3.0

Velo distributed-systems runtime: active messaging, peer discovery, streaming, rendezvous, and queue backends
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
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Local integration tests for `velo::streaming::mpsc`.
//!
//! No messenger required — every attach is same-worker, so the AnchorManager
//! can be driven entirely through `create_mpsc_anchor*` / `attach_mpsc_stream_anchor`.

mod common;

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use common::MockFrameTransport;
use futures::StreamExt;
use velo::streaming::{
    AnchorKind, AnchorManager, AttachError, MpscAnchorConfig, MpscFrame, MpscStreamAnchor, SenderId,
};
use velo_ext::WorkerId;

fn make_manager() -> Arc<AnchorManager> {
    Arc::new(AnchorManager::new(
        WorkerId::from_u64(1),
        Arc::new(MockFrameTransport::new()),
    ))
}

/// Pull frames with a hard timeout so tests never hang when we've miscounted
/// expected events.
async fn next_frame<T: serde::de::DeserializeOwned>(
    anchor: &mut MpscStreamAnchor<T>,
) -> Option<Result<(SenderId, MpscFrame<T>), velo::streaming::StreamError>> {
    match tokio::time::timeout(Duration::from_secs(3), anchor.next()).await {
        Ok(v) => v,
        Err(_) => panic!("stream stalled: no frame in 3s"),
    }
}

/// Test 1: two interleaved senders — both sender_ids appear with correct attribution.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_two_local_senders_interleaved() {
    let mgr = make_manager();
    let mut anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();

    let s1 = mgr
        .attach_mpsc_stream_anchor::<u32>(handle)
        .await
        .expect("attach s1");
    let s2 = mgr
        .attach_mpsc_stream_anchor::<u32>(handle)
        .await
        .expect("attach s2");
    assert_eq!(s1.sender_id(), SenderId(1));
    assert_eq!(s2.sender_id(), SenderId(2));

    for i in 0u32..5 {
        s1.send(i).await.expect("s1 send");
        s2.send(100 + i).await.expect("s2 send");
    }

    let mut s1_items = Vec::new();
    let mut s2_items = Vec::new();
    let mut remaining = 10;
    while remaining > 0 {
        match next_frame(&mut anchor).await.expect("frame") {
            Ok((SenderId(1), MpscFrame::Item(v))) => {
                s1_items.push(v);
                remaining -= 1;
            }
            Ok((SenderId(2), MpscFrame::Item(v))) => {
                s2_items.push(v);
                remaining -= 1;
            }
            Ok((sid, MpscFrame::Item(v))) => panic!("unexpected sender {sid} item {v}"),
            Ok((sid, MpscFrame::Detached | MpscFrame::Dropped(_))) => {
                panic!("unexpected early exit from {sid}");
            }
            Ok((_, MpscFrame::SenderError(m))) => panic!("unexpected sender error: {m}"),
            Err(e) => panic!("stream error: {e}"),
        }
    }
    assert_eq!(s1_items, (0u32..5).collect::<Vec<_>>());
    assert_eq!(s2_items, (100u32..105).collect::<Vec<_>>());

    anchor.cancel();
}

/// Test 2: dropping one of two senders is non-terminal — the other keeps flowing.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_sender_drop_not_terminal() {
    let mgr = make_manager();
    let mut anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();

    let s1 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let s2 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();

    s1.send(1).await.unwrap();
    s2.send(2).await.unwrap();

    // Drop s1 → produces a Dropped event for SenderId(1). Anchor stays alive.
    drop(s1);

    // s2 is still usable.
    s2.send(3).await.unwrap();
    s2.send(4).await.unwrap();

    // Consume 4 items + 1 Dropped event for SenderId(1).
    let mut items_by_sender: HashMap<SenderId, Vec<u32>> = HashMap::new();
    let mut dropped_sids: Vec<SenderId> = Vec::new();
    let mut items_seen = 0;
    while items_seen < 4 || dropped_sids.is_empty() {
        match next_frame(&mut anchor).await.expect("frame") {
            Ok((sid, MpscFrame::Item(v))) => {
                items_by_sender.entry(sid).or_default().push(v);
                items_seen += 1;
            }
            Ok((sid, MpscFrame::Dropped(_))) => dropped_sids.push(sid),
            Ok((sid, MpscFrame::Detached)) => panic!("unexpected Detached from {sid}"),
            Ok((sid, MpscFrame::SenderError(m))) => panic!("sender error {sid}: {m}"),
            Err(e) => panic!("stream error: {e}"),
        }
    }
    assert_eq!(
        items_by_sender.get(&SenderId(1)).map(|v| v.as_slice()),
        Some(&[1u32][..])
    );
    assert_eq!(
        items_by_sender.get(&SenderId(2)).map(|v| v.as_slice()),
        Some(&[2u32, 3, 4][..])
    );
    assert_eq!(dropped_sids, vec![SenderId(1)]);

    // Anchor is still alive; new senders can attach.
    let s3 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    assert_eq!(s3.sender_id(), SenderId(3));
    drop(s3);
    drop(s2);
    anchor.cancel();
}

/// Test 3: detach returns handle; a fresh attach allocates a new SenderId.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_sender_detach_reattach() {
    let mgr = make_manager();
    let mut anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();

    let s1 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    assert_eq!(s1.sender_id(), SenderId(1));
    s1.send(10).await.unwrap();
    let returned = s1.detach().await.expect("detach");
    assert_eq!(returned, handle);

    let s2 = mgr
        .attach_mpsc_stream_anchor::<u32>(returned)
        .await
        .unwrap();
    assert_eq!(
        s2.sender_id(),
        SenderId(2),
        "reattach must allocate fresh id"
    );
    s2.send(20).await.unwrap();
    drop(s2);

    // Expect: Item(SenderId(1), 10), Detached(SenderId(1)), Item(SenderId(2), 20),
    // Dropped(SenderId(2)). Order of the first two may vary.
    let mut got_item1 = false;
    let mut got_item2 = false;
    let mut got_detached1 = false;
    let mut got_dropped2 = false;
    while !(got_item1 && got_item2 && got_detached1 && got_dropped2) {
        match next_frame(&mut anchor).await.expect("frame") {
            Ok((SenderId(1), MpscFrame::Item(10))) => got_item1 = true,
            Ok((SenderId(2), MpscFrame::Item(20))) => got_item2 = true,
            Ok((SenderId(1), MpscFrame::Detached)) => got_detached1 = true,
            Ok((SenderId(2), MpscFrame::Dropped(_))) => got_dropped2 = true,
            Ok(other) => panic!("unexpected frame: {:?}", other),
            Err(e) => panic!("stream error: {e}"),
        }
    }
    anchor.cancel();
}

/// Test 4: all senders gone + unattached timeout ⇒ stream eventually ends.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_all_senders_gone_timeout() {
    let mgr = make_manager();
    let config = MpscAnchorConfig {
        unattached_timeout: Some(Duration::from_millis(100)),
        ..Default::default()
    };
    let mut anchor = mgr.create_mpsc_anchor_with_config::<u32>(config);
    let handle = anchor.handle();

    let s1 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    s1.send(42).await.unwrap();
    drop(s1);

    // Drain whatever the sender produced; expect the stream to eventually
    // yield None after the unattached-timeout auto-removes the anchor.
    let mut items = Vec::new();
    let mut terminated = false;
    for _ in 0..20 {
        match tokio::time::timeout(Duration::from_secs(2), anchor.next()).await {
            Ok(Some(Ok((_sid, MpscFrame::Item(v))))) => items.push(v),
            Ok(Some(Ok((_, MpscFrame::Dropped(_))))) => {}
            Ok(Some(Ok(_))) => {}
            Ok(Some(Err(e))) => panic!("stream error: {e}"),
            Ok(None) => {
                terminated = true;
                break;
            }
            Err(_) => panic!("stream did not terminate after unattached_timeout"),
        }
    }
    assert!(terminated, "stream must have yielded None");
    assert_eq!(items, vec![42u32]);
}

/// Test 5: max_senders cap enforced.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_max_senders() {
    let mgr = make_manager();
    let config = MpscAnchorConfig {
        max_senders: Some(2),
        ..Default::default()
    };
    let anchor = mgr.create_mpsc_anchor_with_config::<u32>(config);
    let handle = anchor.handle();

    let _s1 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let _s2 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let result = mgr.attach_mpsc_stream_anchor::<u32>(handle).await;
    assert!(
        matches!(result, Err(AttachError::MaxSendersReached { limit: 2, .. })),
        "expected MaxSendersReached, got {result:?}"
    );
}

/// Test 6: controller.cancel() poisons every attached sender.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_controller_cancel_propagates() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    let controller = anchor.controller();

    let s1 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let s2 = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();

    controller.cancel();

    // Give the cancel poison a beat to propagate.
    tokio::time::sleep(Duration::from_millis(20)).await;

    assert!(
        matches!(
            s1.send(1).await,
            Err(velo::streaming::SendError::ChannelClosed)
        ),
        "s1 send must fail after cancel"
    );
    assert!(
        matches!(
            s2.send(2).await,
            Err(velo::streaming::SendError::ChannelClosed)
        ),
        "s2 send must fail after cancel"
    );

    // Re-attach must fail (anchor removed from registry).
    let result = mgr.attach_mpsc_stream_anchor::<u32>(handle).await;
    assert!(
        matches!(result, Err(AttachError::AnchorNotFound { .. })),
        "reattach after cancel must be AnchorNotFound, got {result:?}"
    );

    drop(anchor);
}

/// Local drop must still surface one `Dropped` event even when the shared
/// frame channel is full.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_local_drop_preserved_under_backpressure() {
    let mgr = make_manager();
    let config = MpscAnchorConfig {
        channel_capacity: Some(1),
        ..Default::default()
    };
    let mut anchor = mgr.create_mpsc_anchor_with_config::<u32>(config);
    let handle = anchor.handle();

    let sender = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let sid = sender.sender_id();
    sender.send(1).await.unwrap();

    let drop_task = tokio::spawn(async move {
        drop(sender);
    });
    tokio::time::sleep(Duration::from_millis(20)).await;
    assert!(
        !drop_task.is_finished(),
        "drop should block while the bounded channel is full"
    );

    match next_frame(&mut anchor).await.expect("frame") {
        Ok((got_sid, MpscFrame::Item(1))) => assert_eq!(got_sid, sid),
        other => panic!("expected Item(1), got {:?}", other),
    }

    tokio::time::timeout(Duration::from_secs(2), drop_task)
        .await
        .expect("drop task should finish once the queue drains")
        .expect("drop task join");

    match next_frame(&mut anchor).await.expect("frame") {
        Ok((got_sid, MpscFrame::Dropped(None))) => assert_eq!(got_sid, sid),
        other => panic!("expected Dropped(None), got {:?}", other),
    }

    anchor.cancel();
}

/// A pending `anchor.next()` must terminate promptly when the controller
/// cancels, even if sender handles are still alive.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_pending_next_wakes_on_controller_cancel() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    let controller = anchor.controller();
    let sender = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();

    let next_task = tokio::spawn(async move {
        let mut anchor = anchor;
        anchor.next().await
    });
    tokio::time::sleep(Duration::from_millis(20)).await;

    controller.cancel();

    let result = tokio::time::timeout(Duration::from_secs(2), next_task)
        .await
        .expect("pending next() must wake after cancel")
        .expect("next task join");
    assert!(
        result.is_none(),
        "cancelled anchor.next() must resolve to None, got {result:?}"
    );

    drop(sender);
}

/// An MPSC handle passed to the SPSC attach path is rejected client-side
/// with [`AttachError::WrongHandleKind`] — no AM round-trip required.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_handle_rejected_by_spsc_attach() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    assert!(handle.is_mpsc_stream());
    assert_eq!(handle.kind(), AnchorKind::Mpsc);

    let result = mgr.attach_stream_anchor::<u32>(handle).await;
    match result {
        Err(AttachError::WrongHandleKind {
            handle: h,
            expected,
        }) => {
            assert_eq!(h, handle);
            assert_eq!(expected, AnchorKind::Spsc);
        }
        other => panic!("expected WrongHandleKind(Spsc), got {other:?}"),
    }

    // Sanity: the MPSC anchor is still usable.
    let s = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    drop(s);
    drop(anchor);
}

/// An SPSC handle passed to the MPSC attach path is rejected client-side.
#[tokio::test(flavor = "multi_thread")]
async fn test_spsc_handle_rejected_by_mpsc_attach() {
    let mgr = make_manager();
    let anchor = mgr.create_anchor::<u32>();
    let handle = anchor.handle();
    assert!(handle.is_spsc_stream());
    assert_eq!(handle.kind(), AnchorKind::Spsc);

    let result = mgr.attach_mpsc_stream_anchor::<u32>(handle).await;
    match result {
        Err(AttachError::WrongHandleKind {
            handle: h,
            expected,
        }) => {
            assert_eq!(h, handle);
            assert_eq!(expected, AnchorKind::Mpsc);
        }
        other => panic!("expected WrongHandleKind(Mpsc), got {other:?}"),
    }

    // Sanity: the SPSC anchor still accepts an SPSC attach.
    let s = mgr.attach_stream_anchor::<u32>(handle).await.unwrap();
    drop(s);
    drop(anchor);
}

/// SPSC and MPSC handles for the same worker have distinct encoded forms.
#[tokio::test(flavor = "multi_thread")]
async fn test_handle_kind_roundtrip_via_manager() {
    let mgr = make_manager();
    let spsc_anchor = mgr.create_anchor::<u32>();
    let mpsc_anchor = mgr.create_mpsc_anchor::<u32>();

    let spsc_handle = spsc_anchor.handle();
    let mpsc_handle = mpsc_anchor.handle();

    assert!(spsc_handle.is_spsc_stream());
    assert!(mpsc_handle.is_mpsc_stream());
    assert_ne!(
        spsc_handle, mpsc_handle,
        "SPSC and MPSC handles must not collide"
    );

    drop(spsc_anchor);
    drop(mpsc_anchor);
}

/// `send_err` surfaces a non-terminal `SenderError` to the consumer. The
/// sender stays attached and can continue sending items afterwards.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_send_err_is_non_terminal() {
    let mgr = make_manager();
    let mut anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    let sender = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();

    sender.send(1).await.unwrap();
    sender.send_err("transient glitch").await.unwrap();
    sender.send(2).await.unwrap();

    let mut items = Vec::new();
    let mut got_err = false;
    while items.len() < 2 || !got_err {
        match next_frame(&mut anchor).await.expect("frame") {
            Ok((_, MpscFrame::Item(v))) => items.push(v),
            Ok((_, MpscFrame::SenderError(msg))) => {
                assert_eq!(msg, "transient glitch");
                got_err = true;
            }
            Ok((sid, MpscFrame::Detached | MpscFrame::Dropped(_))) => {
                panic!("unexpected exit from {sid}")
            }
            Err(e) => panic!("stream error: {e}"),
        }
    }
    assert_eq!(items, vec![1u32, 2]);

    drop(sender);
    anchor.cancel();
}

/// `MpscStreamSender::cancellation_token()` fires when the consumer cancels.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_cancellation_token_fires_on_cancel() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    let controller = anchor.controller();
    let sender = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let token = sender.cancellation_token();
    assert!(!token.is_cancelled());

    // Cancel from the consumer side.
    controller.cancel();

    // Poison propagation should fire the user-facing token too.
    tokio::time::timeout(Duration::from_secs(2), token.cancelled())
        .await
        .expect("cancellation_token must fire after controller.cancel()");

    drop(sender);
    drop(anchor);
}

/// `MpscStreamAnchor::cancel(self)` consume method (distinct from
/// `controller.cancel()`) cleans up the registry and blocks reattach.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_anchor_consume_cancel() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    let _controller = anchor.cancel(); // consume
    // Wait a beat for cancel propagation.
    tokio::time::sleep(Duration::from_millis(20)).await;

    let result = mgr.attach_mpsc_stream_anchor::<u32>(handle).await;
    assert!(
        matches!(result, Err(AttachError::AnchorNotFound { .. })),
        "anchor must be gone after consume-cancel, got {result:?}"
    );
}

/// Debug impl of `MpscStreamSender` is stable and includes sender_id + handle.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_sender_debug_impl() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let handle = anchor.handle();
    let sender = mgr.attach_mpsc_stream_anchor::<u32>(handle).await.unwrap();
    let dbg = format!("{sender:?}");
    assert!(dbg.contains("MpscStreamSender"));
    assert!(dbg.contains("SenderId(1)"));
    assert!(dbg.contains(&format!("{handle:?}")));
    drop(sender);
    drop(anchor);
}

/// `cancel()` via [`MpscStreamController`] is idempotent — multiple calls
/// short-circuit after the first one wins the AtomicBool gate.
#[tokio::test(flavor = "multi_thread")]
async fn test_mpsc_controller_cancel_idempotent() {
    let mgr = make_manager();
    let anchor = mgr.create_mpsc_anchor::<u32>();
    let controller = anchor.controller();
    let c2 = controller.clone();

    controller.cancel();
    c2.cancel(); // second call must not panic or double-remove
    drop(anchor);
}