use tokio::time::sleep;
use std::time::Duration;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sync_function() {
#[timer_macro::timer]
fn sync_function(pause: u64) -> Result<(), Box<dyn std::error::Error>> {
std::thread::sleep(std::time::Duration::from_millis(pause));
Ok(())
}
assert!(sync_function(1000).is_ok());
}
#[tokio::test]
async fn test_async_function() {
#[timer_macro::timer]
async fn async_function(pause: u64) -> Result<(), Box<dyn std::error::Error>> {
sleep(Duration::from_millis(pause)).await;
Ok(())
}
assert!(async_function(1000).await.is_ok());
}
#[tokio::test]
async fn test_async_function_with_error() {
#[timer_macro::timer]
async fn async_function_with_error(pause: u64) -> Result<(), Box<dyn std::error::Error>> {
sleep(Duration::from_millis(pause)).await;
Err("An error occurred".into())
}
let result = async_function_with_error(1000).await;
assert!(result.is_err());
}
}