|
| 1 | +#[cfg(target_os = "linux")] |
| 2 | +mod tests { |
| 3 | + use pyroscope::timer::epoll::{ |
| 4 | + epoll_create1, epoll_ctl, epoll_wait, timerfd_create, timerfd_settime, |
| 5 | + }; |
| 6 | + |
| 7 | + #[test] |
| 8 | + fn test_timerfd_create() { |
| 9 | + let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap(); |
| 10 | + assert!(timer_fd > 0); |
| 11 | + } |
| 12 | + |
| 13 | + #[test] |
| 14 | + fn test_timerfd_settime() { |
| 15 | + let mut new_value = libc::itimerspec { |
| 16 | + it_interval: libc::timespec { |
| 17 | + tv_sec: 10, |
| 18 | + tv_nsec: 0, |
| 19 | + }, |
| 20 | + it_value: libc::timespec { |
| 21 | + tv_sec: 0, |
| 22 | + tv_nsec: 0, |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + let mut old_value = libc::itimerspec { |
| 27 | + it_interval: libc::timespec { |
| 28 | + tv_sec: 0, |
| 29 | + tv_nsec: 0, |
| 30 | + }, |
| 31 | + it_value: libc::timespec { |
| 32 | + tv_sec: 0, |
| 33 | + tv_nsec: 0, |
| 34 | + }, |
| 35 | + }; |
| 36 | + |
| 37 | + let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap(); |
| 38 | + let void = timerfd_settime( |
| 39 | + timer_fd, |
| 40 | + libc::TFD_TIMER_ABSTIME, |
| 41 | + &mut new_value, |
| 42 | + &mut old_value, |
| 43 | + ) |
| 44 | + .unwrap(); |
| 45 | + assert!(void == ()); |
| 46 | + } |
| 47 | + |
| 48 | + #[test] |
| 49 | + fn test_epoll_create1() { |
| 50 | + let epoll_fd = epoll_create1(0).unwrap(); |
| 51 | + assert!(epoll_fd > 0); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_epoll_ctl() { |
| 56 | + let mut event = libc::epoll_event { |
| 57 | + events: libc::EPOLLIN as u32, |
| 58 | + u64: 1, |
| 59 | + }; |
| 60 | + |
| 61 | + let epoll_fd = epoll_create1(0).unwrap(); |
| 62 | + let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap(); |
| 63 | + let void = epoll_ctl(epoll_fd, libc::EPOLL_CTL_ADD, timer_fd, &mut event).unwrap(); |
| 64 | + assert!(void == ()); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_epoll_wait() { |
| 69 | + let mut event = libc::epoll_event { |
| 70 | + events: libc::EPOLLIN as u32, |
| 71 | + u64: 1, |
| 72 | + }; |
| 73 | + |
| 74 | + let epoll_fd = epoll_create1(0).unwrap(); |
| 75 | + let timer_fd = timerfd_create(libc::CLOCK_REALTIME, libc::TFD_NONBLOCK).unwrap(); |
| 76 | + epoll_ctl(epoll_fd, libc::EPOLL_CTL_ADD, timer_fd, &mut event).unwrap(); |
| 77 | + |
| 78 | + let mut events = vec![libc::epoll_event { events: 0, u64: 0 }]; |
| 79 | + |
| 80 | + // Expire in 1ms |
| 81 | + let void = epoll_wait(epoll_fd, events.as_mut_ptr(), 1, 1).unwrap(); |
| 82 | + |
| 83 | + assert!(void == ()); |
| 84 | + } |
| 85 | +} |
0 commit comments