Skip to content

Commit 3fedee9

Browse files
authored
test(iroh): Make test_relay_datagram_queue less timing dependent (#3106)
## Description <!-- A summary of what this pull request achieves and a rough list of changes. --> I've seen `test_relay_datagram_queue` fail on windows recently: https://github.com/n0-computer/iroh/actions/runs/12669553316/job/35307313406 Given that - when the test failed, it took 720ms, - when the test succeeded, it took 150ms and 320ms, - we've got a 100ms timeout on one of the futures, and - the tokio scheduler (I think) doesn't optimize for average job time, I'm not convinced there's a race condition. So, instead I'm rewriting the test to be less timing-dependent. And less confusing as well *and* have better logging. ## 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 2c42eff commit 3fedee9

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

iroh/src/magicsock.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -4096,26 +4096,20 @@ mod tests {
40964096
tasks.spawn({
40974097
let queue = queue.clone();
40984098
async move {
4099-
let mut expected_msgs = vec![false; capacity];
4100-
4101-
while let Ok(datagram) = tokio::time::timeout(
4102-
Duration::from_millis(100),
4103-
futures_lite::future::poll_fn(|cx| {
4099+
let mut expected_msgs: BTreeSet<usize> = (0..capacity).collect();
4100+
while !expected_msgs.is_empty() {
4101+
let datagram = futures_lite::future::poll_fn(|cx| {
41044102
queue.poll_recv(cx).map(|result| result.unwrap())
4105-
}),
4106-
)
4107-
.await
4108-
{
4103+
})
4104+
.await;
4105+
41094106
let msg_num = usize::from_le_bytes(datagram.buf.as_ref().try_into().unwrap());
4107+
debug!("Received {msg_num}");
41104108

4111-
if expected_msgs[msg_num] {
4112-
panic!("Received message number {msg_num} more than once (duplicated)");
4109+
if !expected_msgs.remove(&msg_num) {
4110+
panic!("Received message number {msg_num} twice or more, but expected it only exactly once.");
41134111
}
4114-
4115-
expected_msgs[msg_num] = true;
41164112
}
4117-
4118-
assert!(expected_msgs.into_iter().all(|is_set| is_set));
41194113
}
41204114
});
41214115

@@ -4124,6 +4118,7 @@ mod tests {
41244118
let queue = queue.clone();
41254119
let url = url.clone();
41264120
async move {
4121+
debug!("Sending {i}");
41274122
queue
41284123
.try_send(RelayRecvDatagram {
41294124
url,
@@ -4135,7 +4130,13 @@ mod tests {
41354130
});
41364131
}
41374132

4138-
tasks.join_all().await;
4133+
// We expect all of this work to be done in 10 seconds max.
4134+
if tokio::time::timeout(Duration::from_secs(10), tasks.join_all())
4135+
.await
4136+
.is_err()
4137+
{
4138+
panic!("Timeout - not all messages between 0 and {capacity} received.");
4139+
}
41394140
}
41404141

41414142
#[tokio::test]

0 commit comments

Comments
 (0)