Skip to content

Commit

Permalink
add non-doc tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
deciduously committed Sep 19, 2024
1 parent 07f3597 commit 9031713
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
23 changes: 15 additions & 8 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Scheduler {
/// ```
#[must_use]
pub fn idle_seconds(&self) -> Option<i64> {
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
Expand Down Expand Up @@ -240,19 +240,23 @@ 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())
);

// 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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub mod mock {
use std::sync::LazyLock;

pub(crate) static START: LazyLock<Zoned> =
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)]
Expand All @@ -115,7 +115,7 @@ pub mod mock {
}

fn add_duration(&mut self, duration: impl Into<ZonedArithmetic>) {
let _ = self.instant.checked_add(duration);
self.instant = self.instant.checked_add(duration).unwrap();
}
}
}

0 comments on commit 9031713

Please # to comment.