From dac10ea8a27498305d2c64299ffd6d480e0137ce Mon Sep 17 00:00:00 2001 From: Hrafn Orri Hrafnkelsson Date: Sat, 28 Dec 2024 14:31:55 +0000 Subject: [PATCH] tests --- README.md | 24 +- src/lib.rs | 15 + tests/slow_function_warning.rs | 29 +- tests/variables.rs | 575 +++++++++++++++++++++++++++++++++ 4 files changed, 610 insertions(+), 33 deletions(-) create mode 100644 tests/variables.rs diff --git a/README.md b/README.md index 2c32e83..82d1736 100644 --- a/README.md +++ b/README.md @@ -77,12 +77,12 @@ You can specify the duration using numeric literals followed by a suffix: * `function: String` - The name of the function * `elapsed: Duration` - The elapsed time * `elapsed_str: String` - The elapsed time using the limit unit specified (defaults to milliseconds) -* `elapsed_ns: u64` - The elapsed time in nanoseconds -* `elapsed_nanos: u64` - The elapsed time in nanoseconds -* `elapsed_nanoseconds: u64` - The elapsed time in nanoseconds -* `elapsed_ms: u64` - The elapsed time in milliseconds -* `elapsed_millis: u64` - The elapsed time in milliseconds -* `elapsed_milliseconds: u64` - The elapsed time in milliseconds +* `elapsed_ns: u128` - The elapsed time in nanoseconds +* `elapsed_nanos: u128` - The elapsed time in nanoseconds +* `elapsed_nanoseconds: u128` - The elapsed time in nanoseconds +* `elapsed_ms: u128` - The elapsed time in milliseconds +* `elapsed_millis: u128` - The elapsed time in milliseconds +* `elapsed_milliseconds: u128` - The elapsed time in milliseconds * `elapsed_s: u64` - The elapsed time in seconds * `elapsed_secs: u64` - The elapsed time in seconds * `elapsed_seconds: u64` - The elapsed time in seconds @@ -95,12 +95,12 @@ You can specify the duration using numeric literals followed by a suffix: * `elapsed_days: u64` - The elapsed time in days * `limit: Duration` - The name of the module * `limit_str: String` - The limit time using the limit unit specified (defaults to milliseconds) -* `limit_ns: u64` - The limit time in nanoseconds -* `limit_nanos: u64` - The limit time in nanoseconds -* `limit_nanoseconds: u64` - The limit time in nanoseconds -* `limit_ms: u64` - The limit time in milliseconds -* `limit_millis: u64` - The limit time in milliseconds -* `limit_milliseconds: u64` - The limit time in milliseconds +* `limit_ns: u128` - The limit time in nanoseconds +* `limit_nanos: u128` - The limit time in nanoseconds +* `limit_nanoseconds: u128` - The limit time in nanoseconds +* `limit_ms: u128` - The limit time in milliseconds +* `limit_millis: u128` - The limit time in milliseconds +* `limit_milliseconds: u128` - The limit time in milliseconds * `limit_s: u64` - The limit time in seconds * `limit_secs: u64` - The limit time in seconds * `limit_seconds: u64` - The limit time in seconds diff --git a/src/lib.rs b/src/lib.rs index 3183efc..13bf067 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ use syn::{parse_macro_input, punctuated::Punctuated, spanned::Spanned, token::Se enum TimeUnit { Nanoseconds, + Microseconds, Milliseconds, Seconds, Minutes, @@ -20,6 +21,7 @@ impl TimeUnit { fn to_duration(&self, amount: u64) -> Duration { match self { TimeUnit::Nanoseconds => Duration::from_nanos(amount), + TimeUnit::Microseconds => Duration::from_micros(amount), TimeUnit::Milliseconds => Duration::from_millis(amount), TimeUnit::Seconds => Duration::from_secs(amount), TimeUnit::Minutes => Duration::from_secs(amount * 60), @@ -36,6 +38,7 @@ fn parse_time(expr: &Expr) -> Result<(u64, TimeUnit)> { let amount = literal.base10_parse::()?; let unit = match literal.suffix() { "ns" => TimeUnit::Nanoseconds, + "us" | "μs" => TimeUnit::Microseconds, "ms" => TimeUnit::Milliseconds, "s" => TimeUnit::Seconds, "m" => TimeUnit::Minutes, @@ -154,6 +157,9 @@ pub fn slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStre TimeUnit::Nanoseconds => quote! { format!("{}ns", elapsed.as_nanos()) }, + TimeUnit::Microseconds => quote! { + format!("{}μs", elapsed.as_micros()) + }, TimeUnit::Milliseconds => quote! { format!("{}ms", elapsed.as_millis()) }, @@ -175,6 +181,9 @@ pub fn slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStre TimeUnit::Nanoseconds => quote! { format!("{}ns", limit.as_nanos()) }, + TimeUnit::Microseconds => quote! { + format!("{}μs", limit.as_micros()) + }, TimeUnit::Milliseconds => quote! { format!("{}ms", limit.as_millis()) }, @@ -209,6 +218,9 @@ pub fn slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStre let elapsed_ns = elapsed.as_nanos(); let elapsed_nanos = elapsed_ns; let elapsed_nanoseconds = elapsed_ns; + let elapsed_us = elapsed.as_micros(); + let elapsed_micros = elapsed_us; + let elapsed_microseconds = elapsed_us; let elapsed_ms = elapsed.as_millis(); let elapsed_millis = elapsed_ms; let elapsed_milliseconds = elapsed_ms; @@ -228,6 +240,9 @@ pub fn slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStre let limit_ns = limit.as_nanos(); let limit_nanos = limit_ns; let limit_nanoseconds = limit_ns; + let limit_us = limit.as_micros(); + let limit_micros = limit_us; + let limit_microseconds = limit_us; let limit_ms = limit.as_millis(); let limit_millis = limit_ms; let limit_milliseconds = limit_ms; diff --git a/tests/slow_function_warning.rs b/tests/slow_function_warning.rs index 900e120..e9a46bd 100644 --- a/tests/slow_function_warning.rs +++ b/tests/slow_function_warning.rs @@ -24,13 +24,13 @@ fn default_warning_compiles() { #[test] fn warn() { - #[slow_function_warning(10ms, {*warned = true;})] + #[slow_function_warning(1ms, {*warned = true;})] pub fn sleep(millis: u64, warned: &mut bool) { thread::sleep(Duration::from_millis(millis)); } let mut warned = false; - sleep(100, &mut warned); + sleep(2, &mut warned); assert!(warned); } @@ -50,7 +50,7 @@ fn no_warn() { #[test] fn warn_using_params() { - #[slow_function_warning(10ms, { + #[slow_function_warning(1ms, { println!("{module}::{function} {param}"); *warned = true; })] @@ -59,7 +59,7 @@ fn warn_using_params() { } let mut warned = false; - sleep(10, "trace id", &mut warned); + sleep(2, "trace id", &mut warned); assert!(warned); } @@ -87,7 +87,7 @@ fn warn_impl() { } impl MyStruct { - #[slow_function_warning(10ms, { + #[slow_function_warning(1ms, { println!("{module}::{function} {param}"); self.warned = true; })] @@ -97,7 +97,7 @@ fn warn_impl() { } let mut my_struct = MyStruct { warned: false }; - my_struct.sleep(10, "trace id"); + my_struct.sleep(2, "trace id"); assert!(my_struct.warned); } @@ -126,7 +126,7 @@ fn no_warn_impl() { #[tokio::test] async fn warn_async() { - #[slow_function_warning(10ms, { + #[slow_function_warning(1ms, { println!("{module}::{function} {param}"); *warned = true; })] @@ -135,7 +135,7 @@ async fn warn_async() { } let mut warned = false; - sleep(10, "trace id", &mut warned).await; + sleep(2, "trace id", &mut warned).await; assert!(warned); } @@ -155,16 +155,3 @@ async fn no_warn_async() { assert!(!warned); } - -#[test] -fn limit() { - #[slow_function_warning(10ms, {*duration = limit.clone();})] - pub fn sleep(millis: u64, duration: &mut Duration) { - thread::sleep(Duration::from_millis(millis)); - } - - let mut duration = Duration::default(); - sleep(100, &mut duration); - - assert_eq!(duration.as_millis(), 10); -} diff --git a/tests/variables.rs b/tests/variables.rs new file mode 100644 index 0000000..dc56116 --- /dev/null +++ b/tests/variables.rs @@ -0,0 +1,575 @@ +use std::{thread, time::Duration}; + +use slow_function_warning::*; + +#[test] +fn module() { + #[slow_function_warning(1ms, {*value = module.to_string();})] + pub fn sleep(millis: u64, value: &mut String) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = "".to_string(); + sleep(2, &mut value); + + assert_eq!(value, "variables"); +} + +#[test] +fn function() { + #[slow_function_warning(1ms, {*value = function.to_string();})] + pub fn sleep(millis: u64, value: &mut String) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = "".to_string(); + sleep(2, &mut value); + + assert_eq!(value, "sleep"); +} + +#[test] +fn elapsed() { + #[slow_function_warning(1ms, {*duration = elapsed.clone();})] + pub fn sleep(millis: u64, duration: &mut Duration) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut duration = Duration::default(); + sleep(2, &mut duration); + + assert_eq!(duration.as_millis(), 2); +} + +#[test] +fn elapsed_str() { + #[slow_function_warning(1ms, {*value = elapsed_str.clone();})] + pub fn sleep(millis: u64, value: &mut String) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = "".to_string(); + sleep(2, &mut value); + + assert_eq!(value, "2ms"); +} + +#[test] +fn elapsed_ns() { + #[slow_function_warning(1ms, {*value = elapsed_ns;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert!(value > 2000000); +} + +#[test] +fn elapsed_nanos() { + #[slow_function_warning(1ms, {*value = elapsed_nanos;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert!(value > 2000000); +} + +#[test] +fn elapsed_nanoseconds() { + #[slow_function_warning(1ms, {*value = elapsed_nanoseconds;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert!(value > 2000000); +} + +#[test] +fn elapsed_us() { + #[slow_function_warning(1ms, {*value = elapsed_us;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert!(value > 2000); +} + +#[test] +fn elapsed_micros() { + #[slow_function_warning(1ms, {*value = elapsed_micros;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert!(value > 2000); +} + +#[test] +fn elapsed_microseconds() { + #[slow_function_warning(1ms, {*value = elapsed_microseconds;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert!(value > 2000); +} + +#[test] +fn elapsed_ms() { + #[slow_function_warning(1ms, {*value = elapsed_ms;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 2); +} + +#[test] +fn elapsed_millis() { + #[slow_function_warning(1ms, {*value = elapsed_millis;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 2); +} + +#[test] +fn elapsed_milliseconds() { + #[slow_function_warning(1ms, {*value = elapsed_milliseconds;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 2); +} + +#[test] +fn elapsed_s() { + #[slow_function_warning(1ms, {*value = elapsed_s;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_secs() { + #[slow_function_warning(1ms, {*value = elapsed_secs;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_seconds() { + #[slow_function_warning(1ms, {*value = elapsed_seconds;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_m() { + #[slow_function_warning(1ms, {*value = elapsed_m;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_min() { + #[slow_function_warning(1ms, {*value = elapsed_min;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_minutes() { + #[slow_function_warning(1ms, {*value = elapsed_minutes;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_h() { + #[slow_function_warning(1ms, {*value = elapsed_h;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_hours() { + #[slow_function_warning(1ms, {*value = elapsed_hours;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_d() { + #[slow_function_warning(1ms, {*value = elapsed_d;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn elapsed_days() { + #[slow_function_warning(1ms, {*value = elapsed_days;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit() { + #[slow_function_warning(1ms, {*duration = limit.clone();})] + pub fn sleep(millis: u64, duration: &mut Duration) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut duration = Duration::default(); + sleep(2, &mut duration); + + assert_eq!(duration.as_millis(), 1); +} + +#[test] +fn limit_str() { + #[slow_function_warning(1ms, {*value = limit_str.clone();})] + pub fn sleep(millis: u64, value: &mut String) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = "".to_string(); + sleep(2, &mut value); + + assert_eq!(value, "1ms"); +} + +#[test] +fn limit_ns() { + #[slow_function_warning(1ms, {*value = limit_ns;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1000000); +} + +#[test] +fn limit_nanos() { + #[slow_function_warning(1ms, {*value = limit_nanos;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1000000); +} + +#[test] +fn limit_nanoseconds() { + #[slow_function_warning(1ms, {*value = limit_nanoseconds;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1000000); +} + +#[test] +fn limit_us() { + #[slow_function_warning(1ms, {*value = limit_us;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1000); +} + +#[test] +fn limit_micros() { + #[slow_function_warning(1ms, {*value = limit_micros;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1000); +} + +#[test] +fn limit_microseconds() { + #[slow_function_warning(1ms, {*value = limit_microseconds;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1000); +} + +#[test] +fn limit_ms() { + #[slow_function_warning(1ms, {*value = limit_ms;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1); +} + +#[test] +fn limit_millis() { + #[slow_function_warning(1ms, {*value = limit_millis;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1); +} + +#[test] +fn limit_milliseconds() { + #[slow_function_warning(1ms, {*value = limit_milliseconds;})] + pub fn sleep(millis: u64, value: &mut u128) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u128::default(); + sleep(2, &mut value); + + assert_eq!(value, 1); +} + +#[test] +fn limit_s() { + #[slow_function_warning(1ms, {*value = limit_s;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_secs() { + #[slow_function_warning(1ms, {*value = limit_secs;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_seconds() { + #[slow_function_warning(1ms, {*value = limit_seconds;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_m() { + #[slow_function_warning(1ms, {*value = limit_m;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_min() { + #[slow_function_warning(1ms, {*value = limit_min;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_minutes() { + #[slow_function_warning(1ms, {*value = limit_minutes;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_h() { + #[slow_function_warning(1ms, {*value = limit_h;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_hours() { + #[slow_function_warning(1ms, {*value = limit_hours;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_d() { + #[slow_function_warning(1ms, {*value = limit_d;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +} + +#[test] +fn limit_days() { + #[slow_function_warning(1ms, {*value = limit_days;})] + pub fn sleep(millis: u64, value: &mut u64) { + thread::sleep(Duration::from_millis(millis)); + } + + let mut value = u64::default(); + sleep(2, &mut value); + + assert_eq!(value, 0); +}