//! Setting a read timeout on a Unix socket whose peer has already gone. use std::io::{ErrorKind, Read, Write}; use std::os::unix::net::UnixStream; use std::time::Duration; use loopd::socket::set_read_timeout; #[test] fn a_peer_that_wrote_and_closed_still_delivers_what_it_wrote() { let (mut ours, mut theirs) = UnixStream::pair().unwrap(); theirs.write_all(b"frame").unwrap(); drop(theirs); set_read_timeout(&ours, Duration::from_secs(5)).unwrap(); let mut got = Vec::new(); ours.read_to_end(&mut got).unwrap(); assert_eq!(got, b"frame"); } #[test] fn a_peer_that_closed_without_writing_reads_as_the_end() { let (mut ours, theirs) = UnixStream::pair().unwrap(); drop(theirs); set_read_timeout(&ours, Duration::from_secs(5)).unwrap(); let mut byte = [0u8; 1]; assert_eq!(ours.read(&mut byte).unwrap(), 0); } #[test] fn a_live_peer_gets_the_timeout() { let (mut ours, _theirs) = UnixStream::pair().unwrap(); set_read_timeout(&ours, Duration::from_millis(20)).unwrap(); let mut byte = [0u8; 1]; let err = ours.read(&mut byte).unwrap_err(); assert!( matches!(err.kind(), ErrorKind::WouldBlock | ErrorKind::TimedOut), "{err:?}" ); } #[test] fn a_zero_timeout_is_still_refused() { let (ours, theirs) = UnixStream::pair().unwrap(); drop(theirs); let err = set_read_timeout(&ours, Duration::ZERO).unwrap_err(); assert_eq!(err.kind(), ErrorKind::InvalidInput); }