Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ironpeak committed Dec 28, 2024
1 parent fd8d513 commit dac10ea
Show file tree
Hide file tree
Showing 4 changed files with 610 additions and 33 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use syn::{parse_macro_input, punctuated::Punctuated, spanned::Spanned, token::Se

enum TimeUnit {
Nanoseconds,
Microseconds,
Milliseconds,
Seconds,
Minutes,
Expand All @@ -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),
Expand All @@ -36,6 +38,7 @@ fn parse_time(expr: &Expr) -> Result<(u64, TimeUnit)> {
let amount = literal.base10_parse::<u64>()?;
let unit = match literal.suffix() {
"ns" => TimeUnit::Nanoseconds,
"us" | "μs" => TimeUnit::Microseconds,
"ms" => TimeUnit::Milliseconds,
"s" => TimeUnit::Seconds,
"m" => TimeUnit::Minutes,
Expand Down Expand Up @@ -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())
},
Expand All @@ -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())
},
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
29 changes: 8 additions & 21 deletions tests/slow_function_warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
})]
Expand All @@ -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);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ fn warn_impl() {
}

impl MyStruct {
#[slow_function_warning(10ms, {
#[slow_function_warning(1ms, {
println!("{module}::{function} {param}");
self.warned = true;
})]
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
})]
Expand All @@ -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);
}
Expand All @@ -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);
}
Loading

0 comments on commit dac10ea

Please sign in to comment.