//! Regression: a readiness wake landing while a selecting actor is preempted between //! registering its arms and parking must not be lost. `alloc_interval(1)` and a 1-cycle //! timeslice make every allocation preempt, so the window is hit on every run. use std::os::fd::RawFd; use std::time::{Duration, Instant}; fn pipe() -> (RawFd, RawFd) { let mut fds = [0; 2]; let r = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_NONBLOCK | libc::O_CLOEXEC) }; assert_eq!(r, 0); (fds[0], fds[1]) } fn run_case(wait: fn(RawFd) -> bool) { let rt = smarm::init( smarm::Config::exact(1) .alloc_interval(1) .timeslice_cycles(1), ); rt.run(move || { let mut worst = Duration::ZERO; for _ in 0..10 { let (r, w) = pipe(); let writer = smarm::spawn(move || { let b = [1u8]; assert_eq!(unsafe { libc::write(w, b.as_ptr() as *const _, 1) }, 1); // Hold the only scheduler thread (an OS-level sleep, not a park) so the reader // stays Queued while the epoll thread consumes the readiness and wakes it. std::thread::sleep(Duration::from_millis(20)); }); let t0 = Instant::now(); assert!(wait(r), "wait reported a timeout with the pipe readable"); worst = worst.max(t0.elapsed()); writer.join().unwrap(); unsafe { libc::close(r); libc::close(w); } } // A lost wake usually still returns `true`: the timeout path re-polls the fd, finds // it readable, and reports ready. So the latency bound is the real check. assert!( worst < Duration::from_millis(500), "worst wake latency {worst:?}: the readiness wake was lost (waited out the timeout)" ); }); } #[test] fn wait_readable_timeout_survives_preemption_before_park() { run_case(|fd| smarm::wait_readable_timeout(fd, Duration::from_secs(2)).unwrap()); } #[test] fn select_with_fd_arm_survives_preemption_before_park() { run_case(|fd| { let arm = smarm::FdArm::readable(fd); smarm::try_select_timeout(&[&arm], Duration::from_secs(2)) .unwrap() .is_some() }); } #[test] fn wait_readable_survives_preemption_before_park() { // The guarded path, as a control: wait_fd holds NoPreempt across register-to-park. run_case(|fd| { smarm::wait_readable(fd).unwrap(); true }); }