From 90317132ab9f1f46a8170f5d038b39e03b64955a Mon Sep 17 00:00:00 2001 From: Ben Lovy Date: Thu, 19 Sep 2024 19:37:50 -0400 Subject: [PATCH] add non-doc tests pass --- src/job.rs | 14 +++++++------- src/scheduler.rs | 23 +++++++++++++++-------- src/time.rs | 4 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/job.rs b/src/job.rs index 8b2c107..235201a 100644 --- a/src/job.rs +++ b/src/job.rs @@ -1132,31 +1132,31 @@ mod tests { fn test_reject_weekday_multiple_weeks() { assert_eq!( every(2).monday().unwrap_err().to_string(), - "Scheduling jobs on Mon is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Monday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); assert_eq!( every(2).tuesday().unwrap_err().to_string(), - "Scheduling jobs on Tue is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Tuesday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); assert_eq!( every(2).wednesday().unwrap_err().to_string(), - "Scheduling jobs on Wed is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Wednesday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); assert_eq!( every(2).thursday().unwrap_err().to_string(), - "Scheduling jobs on Thu is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Thursday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); assert_eq!( every(2).friday().unwrap_err().to_string(), - "Scheduling jobs on Fri is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Friday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); assert_eq!( every(2).saturday().unwrap_err().to_string(), - "Scheduling jobs on Sat is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Saturday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); assert_eq!( every(2).sunday().unwrap_err().to_string(), - "Scheduling jobs on Sun is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() + "Scheduling jobs on Sunday is only allowed for weekly jobs. Using specific days on a job scheduled to run every 2 or more weeks is not supported".to_string() ); } diff --git a/src/scheduler.rs b/src/scheduler.rs index 30bc8b7..1c85b92 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -175,7 +175,7 @@ impl Scheduler { /// ``` #[must_use] pub fn idle_seconds(&self) -> Option { - Some(self.next_run()?.until(&self.now()).unwrap().get_seconds()) + Some(self.next_run()?.since(&self.now()).unwrap().get_seconds()) } /// Get the most recently added job, for testing @@ -240,12 +240,15 @@ mod tests { ); scheduler.add_duration(17.seconds()); + scheduler.run_pending()?; + println!("after one: {}", scheduler.now()); assert_eq!( scheduler.next_run(), Some(START.checked_add((17 * 2).seconds()).unwrap()) ); scheduler.add_duration(17.seconds()); + scheduler.run_pending()?; assert_eq!( scheduler.next_run(), Some(START.checked_add((17 * 3).seconds()).unwrap()) @@ -253,6 +256,7 @@ mod tests { // This time, we should hit the minute mark next, not the next 17 second mark scheduler.add_duration(17.seconds()); + scheduler.run_pending()?; assert_eq!(scheduler.idle_seconds(), Some(9)); assert_eq!( scheduler.next_run(), @@ -261,6 +265,7 @@ mod tests { // Afterwards, back to the 17 second job scheduler.add_duration(9.seconds()); + scheduler.run_pending()?; assert_eq!(scheduler.idle_seconds(), Some(8)); assert_eq!( scheduler.next_run(), @@ -393,10 +398,11 @@ mod tests { ); // Make sure it cancels a job after next_run passes the deadline + // FIXME - this test fails? call count never increments scheduler.clear(None); - let deadline = civil::date(2021, 1, 1) - .at(12, 0, 0, 0) + let deadline = civil::date(2024, 1, 1) + .at(7, 0, 10, 0) .intz("America/New_York") .unwrap(); every(5) @@ -409,12 +415,13 @@ mod tests { assert_eq!(scheduler.most_recent_job().unwrap().call_count, 1); assert_eq!(scheduler.jobs.len(), 1); scheduler.add_duration(5.seconds()); + scheduler.run_pending()?; assert_eq!(scheduler.jobs.len(), 1); assert_eq!(scheduler.most_recent_job().unwrap().call_count, 2); - scheduler.run_pending()?; scheduler.add_duration(5.seconds()); scheduler.run_pending()?; // TODO - how to test to ensure the job did not run? + // FIXME - job doesnt disappear? assert_eq!(scheduler.jobs.len(), 0); // Make sure it cancels a job if current execution passes the deadline @@ -443,9 +450,9 @@ mod tests { .run(&mut scheduler, job)?; let j = scheduler.most_recent_job().unwrap(); - assert_eq!(j.next_run.as_ref().unwrap().year(), 2021); + assert_eq!(j.next_run.as_ref().unwrap().year(), 2024); assert_eq!(j.next_run.as_ref().unwrap().month(), 1); - assert_eq!(j.next_run.as_ref().unwrap().day(), 6); + assert_eq!(j.next_run.as_ref().unwrap().day(), 3); assert_eq!(j.next_run.as_ref().unwrap().hour(), 22); assert_eq!(j.next_run.as_ref().unwrap().minute(), 38); assert_eq!(j.next_run.as_ref().unwrap().second(), 10); @@ -458,9 +465,9 @@ mod tests { .run(&mut scheduler, job)?; let j = scheduler.most_recent_job().unwrap(); - assert_eq!(j.next_run.as_ref().unwrap().year(), 2021); + assert_eq!(j.next_run.as_ref().unwrap().year(), 2024); assert_eq!(j.next_run.as_ref().unwrap().month(), 1); - assert_eq!(j.next_run.as_ref().unwrap().day(), 6); + assert_eq!(j.next_run.as_ref().unwrap().day(), 3); assert_eq!(j.next_run.as_ref().unwrap().hour(), 22); assert_eq!(j.next_run.as_ref().unwrap().minute(), 39); assert_eq!(j.next_run.as_ref().unwrap().second(), 0); diff --git a/src/time.rs b/src/time.rs index a82f6ec..26a6542 100644 --- a/src/time.rs +++ b/src/time.rs @@ -89,7 +89,7 @@ pub mod mock { use std::sync::LazyLock; pub(crate) static START: LazyLock = - LazyLock::new(|| "2024-01-01:22:00:00[America/New_York]".parse().unwrap()); + LazyLock::new(|| "2024-01-01T07:00:00[America/New_York]".parse().unwrap()); /// Mock the datetime for predictable results. #[derive(Debug)] @@ -115,7 +115,7 @@ pub mod mock { } fn add_duration(&mut self, duration: impl Into) { - let _ = self.instant.checked_add(duration); + self.instant = self.instant.checked_add(duration).unwrap(); } } }