use crate::runtime;
use std::time::Duration;
pub async fn sleep(duration: Duration) {
let result = runtime::syscall(
runtime::io_uring::opcode::Timeout::new(
&runtime::io_uring::types::Timespec::new()
.sec(duration.as_secs())
.nsec(duration.subsec_nanos()),
)
.build(),
)
.await;
assert_eq!(result.unwrap_err().raw_os_error().unwrap(), libc::ETIME);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime;
use std::time::Instant;
mod sleep {
use super::*;
#[test]
fn returns_immediately_with_zero() {
runtime::block_on(async {
let before = Instant::now();
sleep(Duration::from_millis(0)).await;
assert!(before.elapsed() <= Duration::from_millis(5));
});
}
#[test]
fn passes_time() {
runtime::block_on(async {
let before = Instant::now();
sleep(Duration::from_millis(5)).await;
assert!(before.elapsed() >= Duration::from_millis(5));
});
}
}
}