Skip to content

Commit 55f9571

Browse files
committed
Rework epoll tests to use custom file descriptor
They previously used stdin, which may cause problems on some systems during testing. We can use signalfd() however to create a new file descriptor that can work with epoll, so let's do that instead and see if the intermittent failures found in testing go away.:
1 parent a6377e3 commit 55f9571

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

test/sys/test_epoll.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1+
use std::os::unix::io::AsRawFd;
2+
3+
use nix::{Error, Errno};
14
use nix::sys::epoll::{EpollCreateFlags, EpollOp, EpollEvent};
25
use nix::sys::epoll::{EPOLLIN, EPOLLERR};
36
use nix::sys::epoll::{epoll_create1, epoll_ctl};
4-
use nix::{Error, Errno};
7+
use nix::sys::signal::SigSet;
8+
use nix::sys::signalfd::SignalFd;
9+
use nix::unistd::close;
510

611
#[test]
712
pub fn test_epoll_errno() {
13+
// Create a new signalfd to use for testing. This is better than using stdin or other such fd.
14+
let sfd = SignalFd::new(&SigSet::empty()).unwrap();
15+
816
let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
9-
let result = epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None);
17+
let result = epoll_ctl(efd, EpollOp::EpollCtlDel, sfd.as_raw_fd(), None);
1018
assert!(result.is_err());
1119
assert_eq!(result.unwrap_err(), Error::Sys(Errno::ENOENT));
1220

13-
let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, None);
21+
let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, sfd.as_raw_fd(), None);
1422
assert!(result.is_err());
1523
assert_eq!(result.unwrap_err(), Error::Sys(Errno::EINVAL));
24+
25+
close(efd).unwrap(); // epoll_create1's file descriptors are supposed to be closed afterwards
1626
}
1727

1828
#[test]
1929
pub fn test_epoll_ctl() {
30+
// Create a new signalfd to use for testing. This is better than using stdin or other such fd.
31+
let sfd = SignalFd::new(&SigSet::empty()).unwrap();
32+
2033
let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
2134
let mut event = EpollEvent::new(EPOLLIN | EPOLLERR, 1);
22-
epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, &mut event).unwrap();
23-
epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None).unwrap();
35+
epoll_ctl(efd, EpollOp::EpollCtlAdd, sfd.as_raw_fd(), &mut event).unwrap();
36+
epoll_ctl(efd, EpollOp::EpollCtlDel, sfd.as_raw_fd(), None).unwrap();
37+
38+
close(efd).unwrap(); // epoll_create1's file descriptors are supposed to be closed afterwards
2439
}

0 commit comments

Comments
 (0)