Skip to content

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

chore: files changed tests/support/cron_policy_failure.rs

Steven Enamakel8713433b1a7e
diff --git a/tests/support/cron_policy_failure.rs b/tests/support/cron_policy_failure.rs
index 216138147d..9f952367e8 100644
--- a/tests/support/cron_policy_failure.rs
+++ b/tests/support/cron_policy_failure.rs
@@ -30,17 +30,31 @@ struct Backend {
}
impl StorageBackend for Backend {
- fn driver(&self) -> &'static str { self.inner.driver() }
- fn capabilities(&self) -> Capabilities { self.inner.capabilities() }
+ fn driver(&self) -> &'static str {
+ self.inner.driver()
+ }
+ fn capabilities(&self) -> Capabilities {
+ self.inner.capabilities()
+ }
fn for_scope(&self, scope: &Scope) -> StoreResult<ScopedStorage> {
let inner = self.inner.for_scope(scope)?;
- Ok(ScopedStorage::new(scope.clone(), inner.driver(), Arc::new(Documents {
- inner: Arc::clone(inner.documents()), scope: scope.to_string(),
- faults: Arc::clone(&self.faults),
- }), Arc::clone(inner.streams()), Arc::clone(inner.blobs())))
+ Ok(ScopedStorage::new(
+ scope.clone(),
+ inner.driver(),
+ Arc::new(Documents {
+ inner: Arc::clone(inner.documents()),
+ scope: scope.to_string(),
+ faults: Arc::clone(&self.faults),
+ }),
+ Arc::clone(inner.streams()),
+ Arc::clone(inner.blobs()),
+ ))
}
fn database(&self, name: &str) -> StoreResult<Arc<dyn StorageBackend>> {
- Ok(Arc::new(Self { inner: self.inner.database(name)?, faults: Arc::clone(&self.faults) }))
+ Ok(Arc::new(Self {
+ inner: self.inner.database(name)?,
+ faults: Arc::clone(&self.faults),
+ }))
}
}
@@ -53,49 +67,105 @@ struct Documents {
#[async_trait]
impl DocumentStore for Documents {
- fn capabilities(&self) -> Capabilities { self.inner.capabilities() }
+ fn capabilities(&self) -> Capabilities {
+ self.inner.capabilities()
+ }
async fn ensure_collection(&self, spec: &CollectionSpec) -> StoreResult<()> {
self.inner.ensure_collection(spec).await
}
- async fn get(&self, collection: &str, id: &str) -> StoreResult<Option<Versioned<serde_json::Value>>> {
+ async fn get(
+ &self,
+ collection: &str,
+ id: &str,
+ ) -> StoreResult<Option<Versioned<serde_json::Value>>> {
if collection == POLICIES {
- self.faults.calls.lock().unwrap().push((self.scope.clone(), id.to_string()));
- if self.faults.read.load(Ordering::SeqCst) { return Err(StorageError::backend(BACKEND_DETAIL)); }
+ self.faults
+ .calls
+ .lock()
+ .unwrap()
+ .push((self.scope.clone(), id.to_string()));
+ if self.faults.read.load(Ordering::SeqCst) {
+ return Err(StorageError::backend(BACKEND_DETAIL));
+ }
}
self.inner.get(collection, id).await
}
- async fn put(&self, collection: &str, id: &str, doc: serde_json::Value, precondition: Precondition) -> StoreResult<Version> {
- if collection == POLICIES && self.faults.write.load(Ordering::SeqCst) { return Err(StorageError::backend(BACKEND_DETAIL)); }
+ async fn put(
+ &self,
+ collection: &str,
+ id: &str,
+ doc: serde_json::Value,
+ precondition: Precondition,
+ ) -> StoreResult<Version> {
+ if collection == POLICIES && self.faults.write.load(Ordering::SeqCst) {
+ return Err(StorageError::backend(BACKEND_DETAIL));
+ }
self.inner.put(collection, id, doc, precondition).await
}
- async fn delete(&self, collection: &str, id: &str, precondition: Precondition) -> StoreResult<bool> {
- if collection == POLICIES && self.faults.delete.load(Ordering::SeqCst) { return Err(StorageError::backend(BACKEND_DETAIL)); }
+ async fn delete(
+ &self,
+ collection: &str,
+ id: &str,
+ precondition: Precondition,
+ ) -> StoreResult<bool> {
+ if collection == POLICIES && self.faults.delete.load(Ordering::SeqCst) {
+ return Err(StorageError::backend(BACKEND_DETAIL));
+ }
self.inner.delete(collection, id, precondition).await
}
- async fn query(&self, collection: &str, query: &Query) -> StoreResult<Page<Versioned<serde_json::Value>>> {
+ async fn query(
+ &self,
+ collection: &str,
+ query: &Query,
+ ) -> StoreResult<Page<Versioned<serde_json::Value>>> {
self.inner.query(collection, query).await
}
- async fn count(&self, collection: &str, filter: &Filter) -> StoreResult<u64> { self.inner.count(collection, filter).await }
- async fn delete_where(&self, collection: &str, filter: &Filter) -> StoreResult<u64> { self.inner.delete_where(collection, filter).await }
- async fn claim(&self, collection: &str, filter: &Filter, sort: &[Sort], patch: &serde_json::Value) -> StoreResult<Option<Versioned<serde_json::Value>>> {
+ async fn count(&self, collection: &str, filter: &Filter) -> StoreResult<u64> {
+ self.inner.count(collection, filter).await
+ }
+ async fn delete_where(&self, collection: &str, filter: &Filter) -> StoreResult<u64> {
+ self.inner.delete_where(collection, filter).await
+ }
+ async fn claim(
+ &self,
+ collection: &str,
+ filter: &Filter,
+ sort: &[Sort],
+ patch: &serde_json::Value,
+ ) -> StoreResult<Option<Versioned<serde_json::Value>>> {
self.inner.claim(collection, filter, sort, patch).await
}
- async fn drop_collection(&self, collection: &str) -> StoreResult<()> { self.inner.drop_collection(collection).await }
+ async fn drop_collection(&self, collection: &str) -> StoreResult<()> {
+ self.inner.drop_collection(collection).await
+ }
}
pub(super) async fn policy_reads_refuse_execution_without_consuming_a_slot(case: Case) {
let workspace = tempfile::tempdir().unwrap();
- let config = Config { workspace_dir: workspace.path().join("workspace"),
- config_path: workspace.path().join("config.toml"), ..Config::default() };
+ let config = Config {
+ workspace_dir: workspace.path().join("workspace"),
+ config_path: workspace.path().join("config.toml"),
+ ..Config::default()
+ };
tinyhumans_boot::boot();
- let _runtime = CoreBuilder::new(HostKind::Library).config(config.clone())
- .services(ServiceSet::none()).domains(DomainSet::none()).build().await.unwrap();
+ let _runtime = CoreBuilder::new(HostKind::Library)
+ .config(config.clone())
+ .services(ServiceSet::none())
+ .domains(DomainSet::none())
+ .build()
+ .await
+ .unwrap();
let faults = Arc::new(Faults::default());
- storage::install(Arc::new(Backend { inner: case.backend(), faults: Arc::clone(&faults) }));
+ storage::install(Arc::new(Backend {
+ inner: case.backend(),
+ faults: Arc::clone(&faults),
+ }));
let profile = |name: &str| {
let ctx = CoreContext::current().unwrap().derive_with(
ContextOverlay::new(config.clone(), DomainSet::none(), Default::default())
- .session_agent(name).profile(name));
+ .session_agent(name)
+ .profile(name),
+ );
AgentContextRegistry::register(name, &ctx);
ctx
};
@@ -103,74 +173,156 @@ pub(super) async fn policy_reads_refuse_execution_without_consuming_a_slot(case:
let beta = profile("policy-beta");
let calls = Arc::new(AtomicUsize::new(0));
let recorded = Arc::clone(&calls);
- let _handler = cron::system_job_handlers::register("policy-fixture", Arc::new(move |_| {
- recorded.fetch_add(1, Ordering::SeqCst);
- Box::pin(async { Ok(()) })
- }));
+ let _handler = cron::system_job_handlers::register(
+ "policy-fixture",
+ Arc::new(move |_| {
+ recorded.fetch_add(1, Ordering::SeqCst);
+ Box::pin(async { Ok(()) })
+ }),
+ );
let create = || async {
- cron::system_jobs::ensure_system_job(&config, "policy-fixture", Schedule::Every { every_ms: 3_600_000 }).unwrap()
+ cron::system_jobs::ensure_system_job(
+ &config,
+ "policy-fixture",
+ Schedule::Every {
+ every_ms: 3_600_000,
+ },
+ )
+ .unwrap()
};
let job = CoreContext::scope(Arc::clone(&alpha), create()).await;
let other = CoreContext::scope(Arc::clone(&beta), create()).await;
- assert_eq!(job.id, other.id, "same job id must remain isolated by profile");
- let restricted = JobPolicy { retries: Some(0), single_flight: true };
+ assert_eq!(
+ job.id, other.id,
+ "same job id must remain isolated by profile"
+ );
+ let restricted = JobPolicy {
+ retries: Some(0),
+ single_flight: true,
+ };
CoreContext::scope(Arc::clone(&alpha), async {
policy::set_policy(&config, &job.id, restricted).unwrap();
assert_eq!(policy::get_policy(&config, &job.id).unwrap(), restricted);
faults.write.store(true, Ordering::SeqCst);
- assert!(policy::set_policy(&config, &job.id, JobPolicy { retries: Some(4), single_flight: false }).is_err());
+ assert!(policy::set_policy(
+ &config,
+ &job.id,
+ JobPolicy {
+ retries: Some(4),
+ single_flight: false
+ }
+ )
+ .is_err());
faults.write.store(false, Ordering::SeqCst);
faults.delete.store(true, Ordering::SeqCst);
assert!(policy::clear_policy(&config, &job.id).is_err());
faults.delete.store(false, Ordering::SeqCst);
assert_eq!(policy::get_policy(&config, &job.id).unwrap(), restricted);
- }).await;
+ })
+ .await;
CoreContext::scope(Arc::clone(&beta), async {
- assert_eq!(policy::get_policy(&config, &job.id).unwrap(), JobPolicy::default());
- }).await;
+ assert_eq!(
+ policy::get_policy(&config, &job.id).unwrap(),
+ JobPolicy::default()
+ );
+ })
+ .await;
// Force the persisted occurrence into the past through its actual document port.
CoreContext::scope(Arc::clone(&alpha), async {
let docs = storage::current_scoped().unwrap().unwrap();
- let mut stored = docs.documents().get("cron_jobs", &job.id).await.unwrap().unwrap();
+ let mut stored = docs
+ .documents()
+ .get("cron_jobs", &job.id)
+ .await
+ .unwrap()
+ .unwrap();
let due = chrono::Utc::now() - chrono::Duration::seconds(1);
stored.doc["next_run"] = json!(due.to_rfc3339());
stored.doc["next_run_ms"] = json!(due.timestamp_millis());
stored.doc["next_run_ns"] = json!(due.timestamp_nanos_opt().unwrap());
- docs.documents().put("cron_jobs", &job.id, stored.doc, Precondition::None).await.unwrap();
- }).await;
- let before = CoreContext::scope(Arc::clone(&alpha), async { cron::get_job(&config, &job.id).unwrap().next_run }).await;
+ docs.documents()
+ .put("cron_jobs", &job.id, stored.doc, Precondition::None)
+ .await
+ .unwrap();
+ })
+ .await;
+ let before = CoreContext::scope(Arc::clone(&alpha), async {
+ cron::get_job(&config, &job.id).unwrap().next_run
+ })
+ .await;
faults.read.store(true, Ordering::SeqCst);
cron::scheduler::run_live_agent_pass(1).await;
- let after = CoreContext::scope(Arc::clone(&alpha), async { cron::get_job(&config, &job.id).unwrap().next_run }).await;
+ let after = CoreContext::scope(Arc::clone(&alpha), async {
+ cron::get_job(&config, &job.id).unwrap().next_run
+ })
+ .await;
let scheduled_calls = calls.load(Ordering::SeqCst);
let (direct, manual, queued, tool) = CoreContext::scope(Arc::clone(&alpha), async {
assert!(policy::get_policy(&config, &job.id).is_err());
let direct = cron::scheduler::execute_job_now(&config, &job, "policy-direct").await;
let manual = cron::ops::run_job_now(&config, &job.id).await;
let queued = cron::ops::cron_run(&config, &job.id).await;
- let tool = cron::tools::CronRunTool::new(Arc::new(config.clone())).execute(json!({"job_id": job.id})).await.unwrap();
+ let tool = cron::tools::CronRunTool::new(Arc::new(config.clone()))
+ .execute(json!({"job_id": job.id}))
+ .await
+ .unwrap();
(direct, manual, queued, tool)
- }).await;
- assert_eq!(scheduled_calls, 0, "policy read failure admitted a scheduled job");
- assert_eq!(after, before, "policy read failure consumed the due occurrence");
- assert!(!direct.0, "direct execution must refuse an unreadable policy");
- assert!(manual.is_err(), "Run now must refuse before delivery/persistence");
- assert!(queued.is_err(), "queued Run now must refuse before enqueueing");
+ })
+ .await;
+ assert_eq!(
+ scheduled_calls, 0,
+ "policy read failure admitted a scheduled job"
+ );
+ assert_eq!(
+ after, before,
+ "policy read failure consumed the due occurrence"
+ );
+ assert!(
+ !direct.0,
+ "direct execution must refuse an unreadable policy"
+ );
+ assert!(
+ manual.is_err(),
+ "Run now must refuse before delivery/persistence"
+ );
+ assert!(
+ queued.is_err(),
+ "queued Run now must refuse before enqueueing"
+ );
assert!(!tool.success, "run tool must refuse an unreadable policy");
- assert_eq!(calls.load(Ordering::SeqCst), 0, "manual paths executed the job");
+ assert_eq!(
+ calls.load(Ordering::SeqCst),
+ 0,
+ "manual paths executed the job"
+ );
assert!(!format!("{direct:?}{manual:?}{queued:?}{tool:?}").contains("secret"));
- assert!(!config.workspace_dir.join("cron/jobs.db").exists(), "configured storage fell back to SQLite");
+ assert!(
+ !config.workspace_dir.join("cron/jobs.db").exists(),
+ "configured storage fell back to SQLite"
+ );
let reads = faults.calls.lock().unwrap();
- assert!(reads.iter().any(|(scope, id)| scope == "profile:policy-alpha" && id == &job.id));
- assert!(reads.iter().any(|(scope, id)| scope == "profile:policy-beta" && id == &job.id));
+ assert!(reads
+ .iter()
+ .any(|(scope, id)| scope == "profile:policy-alpha" && id == &job.id));
+ assert!(reads
+ .iter()
+ .any(|(scope, id)| scope == "profile:policy-beta" && id == &job.id));
drop(reads);
faults.read.store(false, Ordering::SeqCst);
CoreContext::scope(Arc::clone(&alpha), async {
policy::clear_policy(&config, &job.id).unwrap();
- assert_eq!(policy::get_policy(&config, &job.id).unwrap(), JobPolicy::default());
+ assert_eq!(
+ policy::get_policy(&config, &job.id).unwrap(),
+ JobPolicy::default()
+ );
assert!(cron::ops::run_job_now(&config, &job.id).await.unwrap().0);
- }).await;
- assert_eq!(calls.load(Ordering::SeqCst), 1, "missing policy row must still run with defaults");
+ })
+ .await;
+ assert_eq!(
+ calls.load(Ordering::SeqCst),
+ 1,
+ "missing policy row must still run with defaults"
+ );
assert!(AgentContextRegistry::deregister("policy-alpha", &alpha));
assert!(AgentContextRegistry::deregister("policy-beta", &beta));
}