The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(storage): add import claim and module tests
Steven Enamakel6dcc20a4e0c8
diff --git a/crates/openhuman-core/src/storage/import/mod_tests.rs b/crates/openhuman-core/src/storage/import/mod_tests.rsindex ac61944149..085a86cb57 100644--- a/crates/openhuman-core/src/storage/import/mod_tests.rs+++ b/crates/openhuman-core/src/storage/import/mod_tests.rs@@ -153,34 +153,59 @@ fn a_finished_import_does_not_run_again_or_resurrect_deleted_records() {}#[test]-fn an_import_claimed_by_another_process_is_skipped_and_leaves_no_marker() {+fn an_import_claimed_by_another_process_waits_for_it_and_does_not_repeat_it() {let (_dir, config) = workspace();legacy_device(&config, "chan-a", "Phone", false);let backend = backend();let source = devices();- // Another process holds the claim for this domain and workspace.+ // Another process holds the claim for this domain and workspace, and+ // finishes (marker written, claim released) shortly after we start.let markers = Repo::on(&backend, &Scope::local(), "storage::import", marker_spec).unwrap();- let claim =- crate::storage::import_claim::claim_id(&marker_id(source.domain, &config.workspace_dir));- markers+ let marker = marker_id(source.domain, &config.workspace_dir);+ let claim = crate::storage::import_claim::claim_id(&marker);+ let held_claim = claim.clone();+ let version = markers.run(move |docs| async move {docs.put(MARKER_COLLECTION,- &claim,+ &held_claim,json!({ "claimed_at_ms": chrono::Utc::now().timestamp_millis() }),Precondition::Absent,).await- .map(|_| ())}).unwrap();+ let finisher = {+ let markers = markers.clone();+ std::thread::spawn(move || {+ std::thread::sleep(std::time::Duration::from_millis(400));+ markers+ .run(move |docs| async move {+ docs.put(+ MARKER_COLLECTION,+ &marker,+ json!({ "version": MARKER_VERSION, "found": 1, "written": 1 }),+ Precondition::Absent,+ )+ .await?;+ docs.delete(MARKER_COLLECTION, &claim, Precondition::Version(version))+ .await+ })+ .unwrap();+ })+ };+ let started = std::time::Instant::now();assert_eq!(outcome(run_with(&backend, &config, &[source])),- Outcome::InProgress+ Outcome::AlreadyDone);- assert!(!is_done(&backend, &config, &source).unwrap());+ assert!(+ started.elapsed() >= std::time::Duration::from_millis(300),+ "the loser must not proceed while the claim is held"+ );+ finisher.join().unwrap();assert!(dump(&backend, &source, "paired_devices").is_empty());}diff --git a/crates/openhuman-core/src/storage/import_claim_tests.rs b/crates/openhuman-core/src/storage/import_claim_tests.rsindex 9ec2f7ea35..219428581a 100644--- a/crates/openhuman-core/src/storage/import_claim_tests.rs+++ b/crates/openhuman-core/src/storage/import_claim_tests.rs@@ -60,7 +60,8 @@ fn a_second_process_skips_while_the_first_is_importing() {started_rx.recv().unwrap();let mut second_ran = false;- let second = with_claim(&repo, COLLECTION, "m", || {+ let now = chrono::Utc::now().timestamp_millis();+ let second = with_claim_at(&repo, COLLECTION, "m", now, CLAIM_TTL_MS, || {second_ran = true;Ok("second")})@@ -143,3 +144,45 @@ fn a_stale_claim_is_taken_over() {assert_eq!(taken, Some(()));assert!(!claim_exists(&repo, "m"));}++#[test]+fn a_waiting_process_runs_once_the_holder_releases() {+ let repo = repo();+ let (started_tx, started_rx) = mpsc::channel::<()>();+ let holder_repo = repo.clone();+ let holder = std::thread::spawn(move || {+ with_claim(&holder_repo, COLLECTION, "m", || {+ started_tx.send(()).unwrap();+ std::thread::sleep(std::time::Duration::from_millis(400));+ Ok("first")+ })+ .unwrap()+ });+ started_rx.recv().unwrap();+ let began = std::time::Instant::now();+ let second = with_claim(&repo, COLLECTION, "m", || Ok("second")).unwrap();+ assert_eq!(second, Some("second"));+ assert!(began.elapsed() >= std::time::Duration::from_millis(200));+ assert_eq!(holder.join().unwrap(), Some("first"));+}++#[test]+fn a_long_import_keeps_its_claim_alive() {+ let repo = repo();+ let now = chrono::Utc::now().timestamp_millis();+ // 90ms lease, body runs 400ms: without renewal a rival would take over.+ let rival_repo = repo.clone();+ let rival_ran = with_claim_at(&repo, COLLECTION, "m", now, 90, || {+ let mut took_over = false;+ for _ in 0..8 {+ std::thread::sleep(std::time::Duration::from_millis(50));+ let at = chrono::Utc::now().timestamp_millis();+ took_over |= with_claim_at(&rival_repo, COLLECTION, "m", at, 90, || Ok(()))+ .unwrap()+ .is_some();+ }+ Ok(took_over)+ })+ .unwrap();+ assert_eq!(rival_ran, Some(false));+}