Skip to content

Commit 1ba8ebc

Browse files
committed
Write some tests for DatagramReBatcher
1 parent b9f61ed commit 1ba8ebc

File tree

1 file changed

+40
-1
lines changed
  • iroh/src/magicsock/transports/relay

1 file changed

+40
-1
lines changed

iroh/src/magicsock/transports/relay/actor.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,13 +1268,15 @@ impl Iterator for DatagramReBatcher {
12681268
#[cfg(test)]
12691269
mod tests {
12701270
use std::{
1271+
num::NonZeroU16,
12711272
sync::{
12721273
Arc,
12731274
atomic::{AtomicBool, AtomicUsize},
12741275
},
12751276
time::Duration,
12761277
};
12771278

1279+
use bytes::Bytes;
12781280
use iroh_base::{NodeId, RelayUrl, SecretKey};
12791281
use iroh_relay::{PingTracker, protos::relay::Datagrams};
12801282
use n0_snafu::{Error, Result, ResultExt};
@@ -1288,7 +1290,9 @@ mod tests {
12881290
RELAY_INACTIVE_CLEANUP_TIME, RelayConnectionOptions, RelayRecvDatagram, RelaySendItem,
12891291
UNDELIVERABLE_DATAGRAM_TIMEOUT,
12901292
};
1291-
use crate::{dns::DnsResolver, test_utils};
1293+
use crate::{
1294+
dns::DnsResolver, magicsock::transports::relay::actor::DatagramReBatcher, test_utils,
1295+
};
12921296

12931297
/// Starts a new [`ActiveRelayActor`].
12941298
#[allow(clippy::too_many_arguments)]
@@ -1614,4 +1618,39 @@ mod tests {
16141618
let res = tokio::time::timeout(Duration::from_secs(10), tracker.timeout()).await;
16151619
assert!(res.is_err(), "ping timeout should only happen once");
16161620
}
1621+
1622+
fn run_datagram_re_batcher(max_segments: usize, expected_lengths: Vec<usize>) {
1623+
let contents = Bytes::from_static(
1624+
b"Hello world! There's lots of stuff to talk about when you need a big buffer.",
1625+
);
1626+
let datagrams = Datagrams {
1627+
contents: contents.clone(),
1628+
ecn: None,
1629+
segment_size: NonZeroU16::new(10),
1630+
};
1631+
1632+
let re_batched_lengths = DatagramReBatcher {
1633+
datagrams,
1634+
max_segments,
1635+
}
1636+
.map(|d| d.contents.len())
1637+
.collect::<Vec<_>>();
1638+
1639+
assert_eq!(expected_lengths, re_batched_lengths);
1640+
}
1641+
1642+
#[test]
1643+
fn test_datagram_re_batcher_small_batches() {
1644+
run_datagram_re_batcher(3, vec![30, 30, 16]);
1645+
}
1646+
1647+
#[test]
1648+
fn test_datagram_re_batcher_batch_full() {
1649+
run_datagram_re_batcher(10, vec![76]);
1650+
}
1651+
1652+
#[test]
1653+
fn test_datagram_re_batcher_unbatch() {
1654+
run_datagram_re_batcher(1, vec![10, 10, 10, 10, 10, 10, 10, 6]);
1655+
}
16171656
}

0 commit comments

Comments
 (0)