|
| 1 | +use std::os::unix::io::AsRawFd; |
| 2 | + |
| 3 | +use nix::{Error, Errno}; |
1 | 4 | use nix::sys::epoll::{EpollCreateFlags, EpollOp, EpollEvent};
|
2 | 5 | use nix::sys::epoll::{EPOLLIN, EPOLLERR};
|
3 | 6 | 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; |
5 | 10 |
|
6 | 11 | #[test]
|
7 | 12 | 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 | + |
8 | 16 | 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); |
10 | 18 | assert!(result.is_err());
|
11 | 19 | assert_eq!(result.unwrap_err(), Error::Sys(Errno::ENOENT));
|
12 | 20 |
|
13 |
| - let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, None); |
| 21 | + let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, sfd.as_raw_fd(), None); |
14 | 22 | assert!(result.is_err());
|
15 | 23 | 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 |
16 | 26 | }
|
17 | 27 |
|
18 | 28 | #[test]
|
19 | 29 | 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 | + |
20 | 33 | let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
|
21 | 34 | 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 |
24 | 39 | }
|
0 commit comments