Skip to content

Commit

Permalink
adds more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dilawar committed Nov 12, 2024
1 parent bc8644c commit d22bf45
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
48 changes: 43 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl FromStr for CronWithRandomness {
}

impl CronWithRandomness {
pub fn upcoming<'a, Z>(&'a self, timezone: Z) -> impl Iterator<Item = chrono::DateTime<Z>> + '_
pub fn upcoming<'a, Z>(&'a self, timezone: Z) -> impl Iterator<Item = chrono::DateTime<Z>> + 'a
where
Z: chrono::TimeZone + 'a,
{
Expand All @@ -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");
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
}
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:?}");
Expand Down

0 comments on commit d22bf45

Please sign in to comment.