@@ -1268,13 +1268,15 @@ impl Iterator for DatagramReBatcher {
1268
1268
#[ cfg( test) ]
1269
1269
mod tests {
1270
1270
use std:: {
1271
+ num:: NonZeroU16 ,
1271
1272
sync:: {
1272
1273
Arc ,
1273
1274
atomic:: { AtomicBool , AtomicUsize } ,
1274
1275
} ,
1275
1276
time:: Duration ,
1276
1277
} ;
1277
1278
1279
+ use bytes:: Bytes ;
1278
1280
use iroh_base:: { NodeId , RelayUrl , SecretKey } ;
1279
1281
use iroh_relay:: { PingTracker , protos:: relay:: Datagrams } ;
1280
1282
use n0_snafu:: { Error , Result , ResultExt } ;
@@ -1288,7 +1290,9 @@ mod tests {
1288
1290
RELAY_INACTIVE_CLEANUP_TIME , RelayConnectionOptions , RelayRecvDatagram , RelaySendItem ,
1289
1291
UNDELIVERABLE_DATAGRAM_TIMEOUT ,
1290
1292
} ;
1291
- use crate :: { dns:: DnsResolver , test_utils} ;
1293
+ use crate :: {
1294
+ dns:: DnsResolver , magicsock:: transports:: relay:: actor:: DatagramReBatcher , test_utils,
1295
+ } ;
1292
1296
1293
1297
/// Starts a new [`ActiveRelayActor`].
1294
1298
#[ allow( clippy:: too_many_arguments) ]
@@ -1614,4 +1618,39 @@ mod tests {
1614
1618
let res = tokio:: time:: timeout ( Duration :: from_secs ( 10 ) , tracker. timeout ( ) ) . await ;
1615
1619
assert ! ( res. is_err( ) , "ping timeout should only happen once" ) ;
1616
1620
}
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
+ }
1617
1656
}
0 commit comments