From 2bee7fcd869d1ae5e270bc1f163e988fe0e15ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 8 Jan 2025 17:48:50 +0100 Subject: [PATCH 1/2] test: Make `test_relay_datagram_queue` less timing dependent --- iroh/src/magicsock.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/iroh/src/magicsock.rs b/iroh/src/magicsock.rs index a46ca25f4d..9657b0381a 100644 --- a/iroh/src/magicsock.rs +++ b/iroh/src/magicsock.rs @@ -4096,26 +4096,20 @@ mod tests { tasks.spawn({ let queue = queue.clone(); async move { - let mut expected_msgs = vec![false; capacity]; - - while let Ok(datagram) = tokio::time::timeout( - Duration::from_millis(100), - futures_lite::future::poll_fn(|cx| { + let mut expected_msgs: BTreeSet = (0..capacity).into_iter().collect(); + while !expected_msgs.is_empty() { + let datagram = futures_lite::future::poll_fn(|cx| { queue.poll_recv(cx).map(|result| result.unwrap()) - }), - ) - .await - { + }) + .await; + let msg_num = usize::from_le_bytes(datagram.buf.as_ref().try_into().unwrap()); + debug!("Received {msg_num}"); - if expected_msgs[msg_num] { - panic!("Received message number {msg_num} more than once (duplicated)"); + if !expected_msgs.remove(&msg_num) { + panic!("Received message number {msg_num} twice or more, but expected it only exactly once."); } - - expected_msgs[msg_num] = true; } - - assert!(expected_msgs.into_iter().all(|is_set| is_set)); } }); @@ -4124,6 +4118,7 @@ mod tests { let queue = queue.clone(); let url = url.clone(); async move { + debug!("Sending {i}"); queue .try_send(RelayRecvDatagram { url, @@ -4135,7 +4130,10 @@ mod tests { }); } - tasks.join_all().await; + // We expect all of this work to be done in 10 seconds max. + if let Err(_) = tokio::time::timeout(Duration::from_secs(10), tasks.join_all()).await { + panic!("Timeout - not all messages between 0 and {capacity} received."); + } } #[tokio::test] From 2224a10e2f9d6648883026a057133003c6a008fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 8 Jan 2025 17:53:33 +0100 Subject: [PATCH 2/2] clippy fixes --- iroh/src/magicsock.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/iroh/src/magicsock.rs b/iroh/src/magicsock.rs index 9657b0381a..e32d16ad65 100644 --- a/iroh/src/magicsock.rs +++ b/iroh/src/magicsock.rs @@ -4096,7 +4096,7 @@ mod tests { tasks.spawn({ let queue = queue.clone(); async move { - let mut expected_msgs: BTreeSet = (0..capacity).into_iter().collect(); + let mut expected_msgs: BTreeSet = (0..capacity).collect(); while !expected_msgs.is_empty() { let datagram = futures_lite::future::poll_fn(|cx| { queue.poll_recv(cx).map(|result| result.unwrap()) @@ -4131,7 +4131,10 @@ mod tests { } // We expect all of this work to be done in 10 seconds max. - if let Err(_) = tokio::time::timeout(Duration::from_secs(10), tasks.join_all()).await { + if tokio::time::timeout(Duration::from_secs(10), tasks.join_all()) + .await + .is_err() + { panic!("Timeout - not all messages between 0 and {capacity} received."); } }