use std::io::Write;
use std::os::fd::AsFd;
use std::time::Duration;
use super::{is_readable, poll_readable};
#[skuld::test]
fn is_readable_follows_the_descriptor() {
let (rx, mut tx) = std::io::pipe().unwrap();
assert!(!is_readable(rx.as_fd()).unwrap());
tx.write_all(&[1]).unwrap();
assert!(is_readable(rx.as_fd()).unwrap());
assert!(is_readable(rx.as_fd()).unwrap());
}
#[skuld::test]
fn poll_readable_returns_when_the_descriptor_is_readable() {
let (rx, mut tx) = std::io::pipe().unwrap();
tx.write_all(&[1]).unwrap();
assert!(poll_readable(rx.as_fd(), None).unwrap());
}
#[skuld::test]
fn poll_readable_records_the_wait_it_arms() {
let (rx, _tx) = std::io::pipe().unwrap();
crate::wait::block_probe::take();
assert!(!poll_readable(rx.as_fd(), Some(crate::wait::now())).unwrap());
assert_eq!(crate::wait::block_probe::take().len(), 1);
}
#[skuld::test]
fn poll_readable_with_a_passed_deadline_says_not_readable() {
let (rx, _tx) = std::io::pipe().unwrap();
assert!(!poll_readable(rx.as_fd(), Some(crate::wait::now())).unwrap());
}
#[skuld::test]
fn poll_readable_arms_no_block_longer_than_poll_takes() {
let (rx, mut tx) = std::io::pipe().unwrap();
tx.write_all(&[1]).unwrap();
let far = crate::wait::now() + Duration::from_secs(100 * 24 * 3600);
crate::wait::block_probe::take();
assert!(poll_readable(rx.as_fd(), Some(far)).unwrap());
let armed = crate::wait::block_probe::take();
assert_eq!(armed, [Some(Duration::from_millis(i32::MAX as u64))]);
}
#[skuld::test]
fn poll_readable_without_a_deadline_records_an_unbounded_block() {
let (rx, mut tx) = std::io::pipe().unwrap();
tx.write_all(&[1]).unwrap();
crate::wait::block_probe::take();
assert!(poll_readable(rx.as_fd(), None).unwrap());
assert_eq!(crate::wait::block_probe::take(), [None]);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn poll_readable_without_a_deadline_reports_pollerr_as_an_error() {
let (rx, tx) = std::io::pipe().unwrap();
drop(rx);
assert!(poll_readable(tx.as_fd(), None).is_err());
}