Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(cron): cover scheduler dispatch and policy edge cases

Steven Enamakel2512ecdc0293
diff --git a/crates/openhuman-core/src/cron/policy_tests.rs b/crates/openhuman-core/src/cron/policy_tests.rs
index 5146a841d6..0c993751eb 100644
--- a/crates/openhuman-core/src/cron/policy_tests.rs
+++ b/crates/openhuman-core/src/cron/policy_tests.rs
@@ -1,5 +1,4 @@
use super::*;
-use crate::storage::rusqlite;
use tempfile::TempDir;
use tinyflows_schedule::types::Schedule;
@@ -49,7 +48,7 @@ fn a_policy_round_trips_including_zero_retries() {
}
#[test]
-fn setting_the_default_policy_removes_the_row() {
+fn setting_the_default_policy_reverts_the_job_to_the_default() {
let tmp = TempDir::new().unwrap();
let config = config(&tmp);
let id = shell_job(&config, "back-to-default");
@@ -64,11 +63,14 @@ fn setting_the_default_policy_removes_the_row() {
.unwrap();
set_policy(&config, &id, JobPolicy::default()).unwrap();
assert_eq!(get_policy(&config, &id).unwrap(), JobPolicy::default());
- assert_eq!(count_rows(&config), 0);
+ assert_eq!(
+ crate::cron::get_job(&config, &id).unwrap().policy,
+ JobPolicy::default()
+ );
}
#[test]
-fn removing_a_job_removes_its_policy() {
+fn removing_a_job_takes_its_policy_with_it() {
let tmp = TempDir::new().unwrap();
let config = config(&tmp);
let id = shell_job(&config, "removed");
@@ -82,7 +84,8 @@ fn removing_a_job_removes_its_policy() {
)
.unwrap();
crate::cron::remove_job(&config, &id).unwrap();
- assert_eq!(count_rows(&config), 0);
+ assert_eq!(get_policy(&config, &id).unwrap(), JobPolicy::default());
+ assert!(crate::cron::get_job(&config, &id).is_err());
}
#[test]
@@ -102,7 +105,7 @@ fn clearing_every_job_clears_every_policy() {
.unwrap();
}
crate::cron::clear_all_jobs(&config).unwrap();
- assert_eq!(count_rows(&config), 0);
+ assert!(crate::cron::list_jobs(&config).unwrap().is_empty());
}
#[test]
@@ -117,10 +120,31 @@ fn effective_retries_prefers_the_job_override() {
assert_eq!(effective_retries(&config, &none), 0);
}
-fn count_rows(config: &Config) -> i64 {
- let conn = rusqlite::Connection::open(db_path(config)).unwrap();
- conn.query_row("SELECT COUNT(*) FROM cron_job_policies", [], |row| {
- row.get(0)
- })
- .unwrap()
+#[test]
+fn a_policy_stays_with_its_job_across_other_updates() {
+ let tmp = TempDir::new().unwrap();
+ let config = config(&tmp);
+ let id = shell_job(&config, "renamed");
+ let policy = JobPolicy {
+ retries: Some(3),
+ single_flight: true,
+ };
+ set_policy(&config, &id, policy).unwrap();
+ crate::cron::update_job(
+ &config,
+ &id,
+ crate::cron::CronJobPatch {
+ name: Some("renamed again".into()),
+ ..Default::default()
+ },
+ )
+ .unwrap();
+ assert_eq!(get_policy(&config, &id).unwrap(), policy);
+}
+
+#[test]
+fn clearing_the_policy_of_a_missing_job_is_not_an_error() {
+ let tmp = TempDir::new().unwrap();
+ let config = config(&tmp);
+ clear_policy(&config, "no-such-job").unwrap();
}
diff --git a/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs b/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
index fba4910ad0..ac7e1c7c33 100644
--- a/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
@@ -6,7 +6,6 @@ use super::*;
use crate::cron::policy::{set_policy, JobPolicy};
use crate::cron::system_job_handlers::{register, SystemJobContext};
use crate::cron::system_jobs::ensure_system_job;
-use crate::storage::rusqlite;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use tempfile::TempDir;
@@ -48,13 +47,10 @@ fn due_system_job(config: &Config, name: &str) -> CronJob {
}
fn force_due(config: &Config, job_id: &str) {
- let conn = rusqlite::Connection::open(crate::cron::policy::db_path(config)).unwrap();
- conn.execute(
- "UPDATE cron_jobs SET next_run = ?1 WHERE id = ?2",
- rusqlite::params![
- (Utc::now() - ChronoDuration::seconds(1)).to_rfc3339(),
- job_id
- ],
+ crate::cron::store::force_next_run_for_test(
+ config,
+ job_id,
+ Utc::now() - ChronoDuration::seconds(1),
)
.unwrap();
}
diff --git a/crates/openhuman-core/src/cron/store.rs b/crates/openhuman-core/src/cron/store.rs
index 61e293ab08..a0cef622d4 100644
--- a/crates/openhuman-core/src/cron/store.rs
+++ b/crates/openhuman-core/src/cron/store.rs
@@ -344,6 +344,39 @@ pub fn list_runs(config: &Config, job_id: &str, limit: usize) -> Result<Vec<Cron
run(async move { docs.list_runs(&job_id, limit).await })
}
+/// Moves a job's next run to `at`, which no production path can do directly
+/// (a patch reschedules from the schedule, not to a given instant). Writes the
+/// three forms the store keeps of the instant, so the due-job query sees it.
+#[cfg(test)]
+pub(crate) fn force_next_run_for_test(
+ config: &Config,
+ job_id: &str,
+ at: DateTime<Utc>,
+) -> Result<()> {
+ use tinystoragedrivers::Precondition;
+ let opened = crate::storage::local::open_standing(
+ &db_path(config),
+ "cron::store",
+ config,
+ &|backend, scope, config| legacy_import(backend, scope, config),
+ )?;
+ let docs = std::sync::Arc::clone(opened.scoped()?.documents());
+ let job_id = job_id.to_string();
+ run(async move {
+ let stored = docs
+ .get("cron_jobs", &job_id)
+ .await?
+ .ok_or_else(|| anyhow::anyhow!("no job {job_id}"))?;
+ let mut doc = stored.doc;
+ doc["next_run"] = serde_json::json!(at.to_rfc3339());
+ doc["next_run_ms"] = serde_json::json!(at.timestamp_millis());
+ doc["next_run_ns"] = serde_json::json!(at.timestamp_nanos_opt().unwrap_or(i64::MAX));
+ docs.put("cron_jobs", &job_id, doc, Precondition::None)
+ .await?;
+ Ok(())
+ })
+}
+
#[cfg(test)]
#[path = "store_tests.rs"]
mod tests;