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
//! Loopback integration test over real UDP sockets — wires a Caller and
//! Listener [`SrtSocket`] over ephemeral 127.0.0.1 ports.
//!
//! Drives the full stack: UDP bind → HSv5 handshake → ARQ data transfer
//! (N >= 20 payloads) → TSBPD-ordered delivery.
//!
//! HANG GUARD (issue #807): the test body is wrapped in
//! [`tokio::time::timeout`] so a deadlock FAILS fast instead of hanging
//! forever. Real loopback UDP + a real handshake + ARQ transfer normally
//! completes in well under a second; the bound is generous on purpose and
//! makes no latency claim.
#![cfg(feature = "tokio")]
use core::time::Duration;
use srt_runtime::io::{SrtListener, SrtSocket, SrtSocketConfig};
const CALLER_ISN: u32 = 500;
const LISTENER_ISN: u32 = 1000;
const NUM_PAYLOADS: usize = 20;
const TEST_TIMEOUT: Duration = Duration::from_secs(600);
/// Loopback test: listener binds, caller connects, they exchange N payloads,
/// receiver gets them all in order, byte-identical.
#[tokio::test]
async fn loopback_call_to_recv() {
tokio::time::timeout(TEST_TIMEOUT, async {
// --- Listener ---
let listener_addr = "127.0.0.1:0".parse::<std::net::SocketAddr>().unwrap();
let listener_config = crate::without_tlpktdrop().with_initial_seq_number(LISTENER_ISN);
let mut listener = SrtListener::bind(
listener_addr,
SrtSocketConfig::new(
listener_config,
crate::io_support::io_config(),
broadcast_net::net::UdpOptions::new(),
),
)
.await
.expect("listener bind");
let bound_addr = listener.local_addr().expect("listener local addr");
// --- Caller ---
let caller_config = crate::without_tlpktdrop().with_initial_seq_number(CALLER_ISN);
// Spawn the listener accept in a background task.
let jh = tokio::spawn(async move { listener.accept().await.expect("listener accept") });
let mut caller = SrtSocket::connect(
bound_addr,
SrtSocketConfig::new(
caller_config,
crate::io_support::io_config(),
broadcast_net::net::UdpOptions::new(),
),
)
.await
.expect("caller connect");
// Wait for the accepted socket.
let mut receiver = jh.await.expect("join listener");
// --- Send N distinct payloads from caller ---
let payloads: Vec<Vec<u8>> = (0..NUM_PAYLOADS)
.map(|i| {
let mut p = vec![0u8; 100 + i * 10];
// Fill with a recognizable pattern unique per payload.
for (j, byte) in p.iter_mut().enumerate() {
*byte = (i as u8).wrapping_add(j as u8);
}
p
})
.collect();
for payload in &payloads {
caller.send(payload).await.expect("caller send");
}
// --- Receive all N payloads from the receiver ---
let mut received: Vec<Vec<u8>> = Vec::with_capacity(NUM_PAYLOADS);
while received.len() < NUM_PAYLOADS {
match receiver.recv().await.expect("receiver recv") {
Some(payload) => {
received.push(payload.to_vec());
}
None => break,
}
}
assert_eq!(
received.len(),
NUM_PAYLOADS,
"expected {NUM_PAYLOADS} payloads, got {}",
received.len()
);
// Verify byte-identical delivery in order.
for (i, (sent, recvd)) in payloads.iter().zip(received.iter()).enumerate() {
assert_eq!(
sent,
recvd,
"payload {i} mismatch: sent {} bytes, recv {} bytes",
sent.len(),
recvd.len()
);
}
// Deterministic teardown: drop both sockets so `SrtSocket::Drop`
// aborts their background driver tasks (and each driver also exits
// cooperatively once its command channel closes). Without this a live
// driver task — parked on `udp.recv` with a periodic 2 ms tick — can
// keep the current-thread test runtime from shutting down, which under
// `cargo test`'s parallel binary execution shows up as this test's
// binary never exiting.
drop(caller);
drop(receiver);
})
.await
.expect("test timed out — deadlock or stalled handshake");
}