From d22bf45dff0eb9324cd5f88d36072a2c4cf62263 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Tue, 12 Nov 2024 17:22:29 +0530 Subject: [PATCH] adds more unit tests --- Cargo.toml | 3 +++ src/lib.rs | 48 +++++++++++++++++++++++++++++++++++++++++++----- src/main.rs | 6 ++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ddcf328..b57b406 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ lazy_static = "1.5" rand = "0.8" rand_chacha = "0.3.1" regex = "1.10" +tracing = "0.1.40" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } [dev-dependencies] simple_accumulator = "0.7.0" +tracing-test = "0.2.5" diff --git a/src/lib.rs b/src/lib.rs index f9652a9..12db573 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,7 +99,7 @@ impl FromStr for CronWithRandomness { } impl CronWithRandomness { - pub fn upcoming<'a, Z>(&'a self, timezone: Z) -> impl Iterator> + '_ + pub fn upcoming<'a, Z>(&'a self, timezone: Z) -> impl Iterator> + 'a where Z: chrono::TimeZone + 'a, { @@ -116,8 +116,10 @@ impl CronWithRandomness { let mut rng = ChaCha8Rng::seed_from_u64(SEED); let mut result_datetime = datetime.clone(); - // pick a random minute. We have to reduce one hour from the hour range after this. - result_datetime += chrono::Duration::minutes(rng.gen_range(0..60)); + // // pick a random minute. We have to reduce one hour from the hour range after this. + // if noisy_minute { + // result_datetime += chrono::Duration::minutes(rng.gen_range(0..60)); + // } if let Some(hours) = self.constraints.get("h") { let chosen_internval = hours.choose(&mut rng).expect("chose one"); @@ -223,12 +225,13 @@ mod tests { } #[test] + #[tracing_test::traced_test] fn test_cron_sanity() { let sch = Schedule::from_str("@daily").unwrap(); let mut schedules = vec![]; for datetime in sch.upcoming(Utc).take(10) { schedules.push(datetime); - println!("2-> {datetime:?}"); + tracing::debug!("2-> {datetime:?}"); } assert_eq!(schedules.len(), 10); for i in 1..schedules.len() { @@ -238,9 +241,44 @@ mod tests { } #[test] + #[tracing_test::traced_test] fn test_cron_standard() { + use chrono::Timelike; + use chrono::Datelike; + // second, min, hour, day, week, month let sch = CronWithRandomness::from_str("0 0/5 1/7 * *").unwrap(); - println!("{sch:?}"); + tracing::debug!("{sch:?}"); + for datetime in sch.upcoming(Utc).take(10) { + tracing::debug!("2-> {datetime:?}"); + let time = datetime.time(); + assert_eq!(time.minute(), 0); + assert_eq!(time.hour().rem_euclid(5), 0); + + let date = datetime.day(); + assert_eq!(date.rem_euclid(7), 1); + } + } + + #[test] + #[tracing_test::traced_test] + fn test_cron_fixed_time() { + use chrono::Timelike; + + // second, min, hour, day, week, month + let sch = CronWithRandomness::from_str("0 12 * * *").unwrap(); + for datetime in sch.upcoming(Utc).take(10) { + tracing::debug!("{datetime:?}, time: {:?}", datetime.time()); + assert_eq!(datetime.time().minute(), 0); + } + + let sch = CronWithRandomness::from_str("* 12 * * *").unwrap(); + + let mut min = 0; + for datetime in sch.upcoming(Utc).take(10) { + tracing::debug!("{datetime:?}, time: {:?}", datetime.time()); + assert_eq!(datetime.time().minute(), min); + min += 1; + } } } diff --git a/src/main.rs b/src/main.rs index f7a6d9d..1c4d43e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,13 @@ use chrono::Utc; use cron_with_randomness::CronWithRandomness; use std::str::FromStr; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; fn main() { + tracing_subscriber::registry() + .with(fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); let pattern = std::env::args().nth(1).expect("no pattern given"); let num_samples = std::env::args() .nth(2) @@ -13,6 +18,7 @@ fn main() { .unwrap(); let schedule = CronWithRandomness::from_str(&pattern).unwrap(); + tracing::debug!("Schedule: {schedule:?}"); for datetime in schedule.upcoming(Utc).take(num_samples) { println!(" --> {datetime:?}");