Skip to content

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

test(storage): cover once-per-backend run semantics

Steven Enamakeld61cce62e492
diff --git a/crates/openhuman-core/src/storage/documents_tests.rs b/crates/openhuman-core/src/storage/documents_tests.rs
index 21ea192fca..8ef02d784d 100644
--- a/crates/openhuman-core/src/storage/documents_tests.rs
+++ b/crates/openhuman-core/src/storage/documents_tests.rs
@@ -166,3 +166,44 @@ fn a_repo_with_an_origin_declares_a_collection_only_on_its_first_run() {
repo.run(|docs| async move { docs.get(THINGS, "a").await })
.unwrap();
}
+
+fn repo_on(backend: &Arc<dyn StorageBackend>, scope: &str) -> Repo {
+ let scoped = backend.for_scope(&Scope::new(scope).unwrap()).unwrap();
+ let mut repo = Repo::over(&scoped, "test", specs);
+ repo.origin = Some(Origin {
+ backend: Arc::clone(backend),
+ scope: scope.to_string(),
+ });
+ repo
+}
+
+#[test]
+fn one_time_work_runs_once_per_backend_and_scope_and_retries_failures() {
+ let runs = std::cell::Cell::new(0);
+ let count = || {
+ runs.set(runs.get() + 1);
+ Ok(())
+ };
+ let first = backend();
+ let repo = repo_on(&first, "once");
+ assert!(repo
+ .once_per_backend("k", || Err(anyhow::anyhow!("boom")))
+ .is_err());
+ repo.once_per_backend("k", count).unwrap();
+ repo.once_per_backend("k", count).unwrap();
+ assert_eq!(runs.get(), 1, "a failure is retried, a success is recorded");
+ repo.once_per_backend("other", count).unwrap();
+ repo_on(&first, "once-b").once_per_backend("k", count).unwrap();
+ repo_on(&backend(), "once").once_per_backend("k", count).unwrap();
+ assert_eq!(runs.get(), 4, "another key, scope or backend runs afresh");
+
+ // Without an origin there is no identity to key on: it always runs.
+ let plain = Repo::over(
+ &first.for_scope(&Scope::new("once").unwrap()).unwrap(),
+ "test",
+ specs,
+ );
+ plain.once_per_backend("k", count).unwrap();
+ plain.once_per_backend("k", count).unwrap();
+ assert_eq!(runs.get(), 6);
+}