Skip to content

Commit e575af2

Browse files
authored
tests(iroh): Packet loss is expected with socket rebinding (#3001)
## Description This allows packet loss when rebinding sockets. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions I think it would also be reasonable to use e.g.: ``` rust enum ExpectedLoss { Low, Medium, } ``` And then accept like 50 lost packets in Medium. ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent a3f0497 commit e575af2

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

iroh/src/magicsock.rs

+61-29
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,7 @@ mod tests {
29952995
}
29962996

29972997
#[instrument(skip_all, fields(me = %ep.endpoint.node_id().fmt_short()))]
2998-
async fn echo_receiver(ep: MagicStack) -> Result<()> {
2998+
async fn echo_receiver(ep: MagicStack, loss: ExpectedLoss) -> Result<()> {
29992999
info!("accepting conn");
30003000
let conn = ep.endpoint.accept().await.expect("no conn");
30013001

@@ -3026,10 +3026,12 @@ mod tests {
30263026
let stats = conn.stats();
30273027
info!("stats: {:#?}", stats);
30283028
// TODO: ensure panics in this function are reported ok
3029-
assert!(
3030-
stats.path.lost_packets < 10,
3031-
"[receiver] should not loose many packets",
3032-
);
3029+
if matches!(loss, ExpectedLoss::AlmostNone) {
3030+
assert!(
3031+
stats.path.lost_packets < 10,
3032+
"[receiver] should not loose many packets",
3033+
);
3034+
}
30333035

30343036
info!("close");
30353037
conn.close(0u32.into(), b"done");
@@ -3040,7 +3042,12 @@ mod tests {
30403042
}
30413043

30423044
#[instrument(skip_all, fields(me = %ep.endpoint.node_id().fmt_short()))]
3043-
async fn echo_sender(ep: MagicStack, dest_id: PublicKey, msg: &[u8]) -> Result<()> {
3045+
async fn echo_sender(
3046+
ep: MagicStack,
3047+
dest_id: PublicKey,
3048+
msg: &[u8],
3049+
loss: ExpectedLoss,
3050+
) -> Result<()> {
30443051
info!("connecting to {}", dest_id.fmt_short());
30453052
let dest = NodeAddr::new(dest_id);
30463053
let conn = ep
@@ -3071,10 +3078,12 @@ mod tests {
30713078

30723079
let stats = conn.stats();
30733080
info!("stats: {:#?}", stats);
3074-
assert!(
3075-
stats.path.lost_packets < 10,
3076-
"[sender] should not loose many packets",
3077-
);
3081+
if matches!(loss, ExpectedLoss::AlmostNone) {
3082+
assert!(
3083+
stats.path.lost_packets < 10,
3084+
"[sender] should not loose many packets",
3085+
);
3086+
}
30783087

30793088
info!("close");
30803089
conn.close(0u32.into(), b"done");
@@ -3083,14 +3092,25 @@ mod tests {
30833092
Ok(())
30843093
}
30853094

3095+
#[derive(Debug, Copy, Clone)]
3096+
enum ExpectedLoss {
3097+
AlmostNone,
3098+
YeahSure,
3099+
}
3100+
30863101
/// Runs a roundtrip between the [`echo_sender`] and [`echo_receiver`].
3087-
async fn run_roundtrip(sender: MagicStack, receiver: MagicStack, payload: &[u8]) {
3102+
async fn run_roundtrip(
3103+
sender: MagicStack,
3104+
receiver: MagicStack,
3105+
payload: &[u8],
3106+
loss: ExpectedLoss,
3107+
) {
30883108
let send_node_id = sender.endpoint.node_id();
30893109
let recv_node_id = receiver.endpoint.node_id();
30903110
info!("\nroundtrip: {send_node_id:#} -> {recv_node_id:#}");
30913111

3092-
let receiver_task = tokio::spawn(echo_receiver(receiver));
3093-
let sender_res = echo_sender(sender, recv_node_id, payload).await;
3112+
let receiver_task = tokio::spawn(echo_receiver(receiver, loss));
3113+
let sender_res = echo_sender(sender, recv_node_id, payload, loss).await;
30943114
let sender_is_err = match sender_res {
30953115
Ok(()) => false,
30963116
Err(err) => {
@@ -3129,14 +3149,26 @@ mod tests {
31293149

31303150
for i in 0..5 {
31313151
info!("\n-- round {i}");
3132-
run_roundtrip(m1.clone(), m2.clone(), b"hello m1").await;
3133-
run_roundtrip(m2.clone(), m1.clone(), b"hello m2").await;
3152+
run_roundtrip(
3153+
m1.clone(),
3154+
m2.clone(),
3155+
b"hello m1",
3156+
ExpectedLoss::AlmostNone,
3157+
)
3158+
.await;
3159+
run_roundtrip(
3160+
m2.clone(),
3161+
m1.clone(),
3162+
b"hello m2",
3163+
ExpectedLoss::AlmostNone,
3164+
)
3165+
.await;
31343166

31353167
info!("\n-- larger data");
31363168
let mut data = vec![0u8; 10 * 1024];
31373169
rand::thread_rng().fill_bytes(&mut data);
3138-
run_roundtrip(m1.clone(), m2.clone(), &data).await;
3139-
run_roundtrip(m2.clone(), m1.clone(), &data).await;
3170+
run_roundtrip(m1.clone(), m2.clone(), &data, ExpectedLoss::AlmostNone).await;
3171+
run_roundtrip(m2.clone(), m1.clone(), &data, ExpectedLoss::AlmostNone).await;
31403172
}
31413173

31423174
Ok(())
@@ -3223,14 +3255,14 @@ mod tests {
32233255

32243256
for i in 0..rounds {
32253257
println!("-- [m1 changes] round {}", i + 1);
3226-
run_roundtrip(m1.clone(), m2.clone(), b"hello m1").await;
3227-
run_roundtrip(m2.clone(), m1.clone(), b"hello m2").await;
3258+
run_roundtrip(m1.clone(), m2.clone(), b"hello m1", ExpectedLoss::YeahSure).await;
3259+
run_roundtrip(m2.clone(), m1.clone(), b"hello m2", ExpectedLoss::YeahSure).await;
32283260

32293261
println!("-- [m1 changes] larger data");
32303262
let mut data = vec![0u8; 10 * 1024];
32313263
rand::thread_rng().fill_bytes(&mut data);
3232-
run_roundtrip(m1.clone(), m2.clone(), &data).await;
3233-
run_roundtrip(m2.clone(), m1.clone(), &data).await;
3264+
run_roundtrip(m1.clone(), m2.clone(), &data, ExpectedLoss::YeahSure).await;
3265+
run_roundtrip(m2.clone(), m1.clone(), &data, ExpectedLoss::YeahSure).await;
32343266
}
32353267

32363268
std::mem::drop(m1_network_change_guard);
@@ -3252,14 +3284,14 @@ mod tests {
32523284

32533285
for i in 0..rounds {
32543286
println!("-- [m2 changes] round {}", i + 1);
3255-
run_roundtrip(m1.clone(), m2.clone(), b"hello m1").await;
3256-
run_roundtrip(m2.clone(), m1.clone(), b"hello m2").await;
3287+
run_roundtrip(m1.clone(), m2.clone(), b"hello m1", ExpectedLoss::YeahSure).await;
3288+
run_roundtrip(m2.clone(), m1.clone(), b"hello m2", ExpectedLoss::YeahSure).await;
32573289

32583290
println!("-- [m2 changes] larger data");
32593291
let mut data = vec![0u8; 10 * 1024];
32603292
rand::thread_rng().fill_bytes(&mut data);
3261-
run_roundtrip(m1.clone(), m2.clone(), &data).await;
3262-
run_roundtrip(m2.clone(), m1.clone(), &data).await;
3293+
run_roundtrip(m1.clone(), m2.clone(), &data, ExpectedLoss::YeahSure).await;
3294+
run_roundtrip(m2.clone(), m1.clone(), &data, ExpectedLoss::YeahSure).await;
32633295
}
32643296

32653297
std::mem::drop(m2_network_change_guard);
@@ -3282,14 +3314,14 @@ mod tests {
32823314

32833315
for i in 0..rounds {
32843316
println!("-- [m1 & m2 changes] round {}", i + 1);
3285-
run_roundtrip(m1.clone(), m2.clone(), b"hello m1").await;
3286-
run_roundtrip(m2.clone(), m1.clone(), b"hello m2").await;
3317+
run_roundtrip(m1.clone(), m2.clone(), b"hello m1", ExpectedLoss::YeahSure).await;
3318+
run_roundtrip(m2.clone(), m1.clone(), b"hello m2", ExpectedLoss::YeahSure).await;
32873319

32883320
println!("-- [m1 & m2 changes] larger data");
32893321
let mut data = vec![0u8; 10 * 1024];
32903322
rand::thread_rng().fill_bytes(&mut data);
3291-
run_roundtrip(m1.clone(), m2.clone(), &data).await;
3292-
run_roundtrip(m2.clone(), m1.clone(), &data).await;
3323+
run_roundtrip(m1.clone(), m2.clone(), &data, ExpectedLoss::YeahSure).await;
3324+
run_roundtrip(m2.clone(), m1.clone(), &data, ExpectedLoss::YeahSure).await;
32933325
}
32943326

32953327
std::mem::drop(m1_m2_network_change_guard);

0 commit comments

Comments
 (0)