Don't set socket as blocking in recvmmsg for non Linux targets (#2611)
* Don't set socket as blocking in recvmmsg for non Linux targets - The user of the function is controling this flag * added a test
This commit is contained in:
parent
c536a0bf04
commit
a746969995
|
@ -10,7 +10,6 @@ pub const NUM_RCVMMSGS: usize = 16;
|
|||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn recv_mmsg(socket: &UdpSocket, packets: &mut [Packet]) -> io::Result<usize> {
|
||||
let mut i = 0;
|
||||
socket.set_nonblocking(false)?;
|
||||
let count = cmp::min(NUM_RCVMMSGS, packets.len());
|
||||
for p in packets.iter_mut().take(count) {
|
||||
p.meta.size = 0;
|
||||
|
@ -87,6 +86,7 @@ pub fn recv_mmsg(sock: &UdpSocket, packets: &mut [Packet]) -> io::Result<usize>
|
|||
mod tests {
|
||||
use crate::packet::PACKET_DATA_SIZE;
|
||||
use crate::recvmmsg::*;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[test]
|
||||
pub fn test_recv_mmsg_one_iter() {
|
||||
|
@ -137,6 +137,38 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_recv_mmsg_multi_iter_timeout() {
|
||||
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
||||
let addr = reader.local_addr().unwrap();
|
||||
reader.set_read_timeout(Some(Duration::new(5, 0))).unwrap();
|
||||
reader.set_nonblocking(false).unwrap();
|
||||
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
||||
let saddr = sender.local_addr().unwrap();
|
||||
let sent = NUM_RCVMMSGS + 10;
|
||||
for _ in 0..sent {
|
||||
let data = [0; PACKET_DATA_SIZE];
|
||||
sender.send_to(&data[..], &addr).unwrap();
|
||||
}
|
||||
|
||||
let start = Instant::now();
|
||||
let mut packets = vec![Packet::default(); NUM_RCVMMSGS * 2];
|
||||
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap();
|
||||
assert_eq!(NUM_RCVMMSGS, recv);
|
||||
for i in 0..recv {
|
||||
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
||||
assert_eq!(packets[i].meta.addr(), saddr);
|
||||
}
|
||||
|
||||
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap();
|
||||
assert_eq!(sent - NUM_RCVMMSGS, recv);
|
||||
for i in 0..recv {
|
||||
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
||||
assert_eq!(packets[i].meta.addr(), saddr);
|
||||
}
|
||||
assert!(start.elapsed().as_secs() < 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_recv_mmsg_multi_addrs() {
|
||||
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
||||
|
|
Loading…
Reference in New Issue