Skip to content

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

test(storage): add lease document and local storage tests

Steven Enamakel86df9e6aba97
diff --git a/crates/openhuman-core/src/storage/lease_documents_tests.rs b/crates/openhuman-core/src/storage/lease_documents_tests.rs
index b9dc82e1fb..4e51be31ce 100644
--- a/crates/openhuman-core/src/storage/lease_documents_tests.rs
+++ b/crates/openhuman-core/src/storage/lease_documents_tests.rs
@@ -123,7 +123,8 @@ async fn keys_are_independent() {
/// Real threads, each its own runtime and node, race for one key on one
/// backend opened by URL: exactly one wins and the rest see it.
-async fn race_on(url: String, contenders: usize) {
+#[cfg(feature = "storage-sqlite")]
+fn race_on(url: String, contenders: usize) {
let barrier = std::sync::Arc::new(std::sync::Barrier::new(contenders));
let handles: Vec<_> = (0..contenders)
.map(|i| {
@@ -159,11 +160,11 @@ async fn race_on(url: String, contenders: usize) {
}
#[cfg(feature = "storage-sqlite")]
-#[tokio::test]
-async fn sqlite_threads_racing_for_a_key_have_one_winner() {
+#[test]
+fn sqlite_threads_racing_for_a_key_have_one_winner() {
for round in 0..5 {
let dir = tempfile::tempdir().unwrap();
let url = format!("sqlite:{}", dir.path().join(format!("leases-{round}.db")).display());
- race_on(url, 8).await;
+ race_on(url, 8);
}
}
diff --git a/crates/openhuman-core/src/storage/lease_local_tests.rs b/crates/openhuman-core/src/storage/lease_local_tests.rs
new file mode 100644
index 0000000000..061e0b7a3d
--- /dev/null
+++ b/crates/openhuman-core/src/storage/lease_local_tests.rs
@@ -0,0 +1,69 @@
+use super::*;
+
+#[tokio::test]
+async fn a_second_instance_on_one_root_is_held_out() {
+ let dir = tempfile::tempdir().unwrap();
+ let (a, b) = (
+ LocalLeases::new(dir.path(), "a"),
+ LocalLeases::new(dir.path(), "b"),
+ );
+ let grant = a.acquire("user-1", 0).await.unwrap();
+ assert_eq!((grant.epoch, grant.previous_unclean), (1, false));
+ match b.acquire("user-1", 0).await {
+ Err(LeaseError::Held(record)) => {
+ assert_eq!(record.owner, "a");
+ assert!(record.is_live(u64::MAX - 1));
+ }
+ other => panic!("expected Held, got {other:?}"),
+ }
+ assert_eq!(b.holder("user-1").await.unwrap().unwrap().owner, "a");
+ assert!(b.holder("user-1").await.unwrap().unwrap().is_live(0));
+}
+
+#[tokio::test]
+async fn release_hands_over_cleanly_and_stale_grants_are_lost() {
+ let dir = tempfile::tempdir().unwrap();
+ let (a, b) = (
+ LocalLeases::new(dir.path(), "a"),
+ LocalLeases::new(dir.path(), "b"),
+ );
+ let grant = a.acquire("k", 0).await.unwrap();
+ let renewed = a.renew(&grant, 5).await.unwrap();
+ assert!(matches!(a.renew(&grant, 6).await, Err(LeaseError::Lost)));
+ let again = a.acquire("k", 7).await.unwrap();
+ assert_eq!((again.epoch, again.previous_unclean), (1, false));
+ a.release(renewed.clone()).await.unwrap();
+ assert!(matches!(a.release(renewed).await, Err(LeaseError::Lost)));
+ assert!(b.holder("k").await.unwrap().unwrap().released);
+ let next = b.acquire("k", 8).await.unwrap();
+ assert_eq!((next.epoch, next.previous_unclean), (2, false));
+}
+
+#[tokio::test]
+async fn a_dead_holder_leaves_an_unclean_record() {
+ let dir = tempfile::tempdir().unwrap();
+ {
+ let crashed = LocalLeases::new(dir.path(), "a");
+ crashed.acquire("k", 0).await.unwrap();
+ // Dropped without releasing: the lock goes with the file handle, as
+ // it would with the process.
+ }
+ let b = LocalLeases::new(dir.path(), "b");
+ let stale = b.holder("k").await.unwrap().unwrap();
+ assert_eq!(stale.owner, "a");
+ assert!(!stale.is_live(0), "a free lock means the holder is gone");
+ let taken = b.acquire("k", 0).await.unwrap();
+ assert_eq!((taken.epoch, taken.previous_unclean), (2, true));
+}
+
+#[tokio::test]
+async fn keys_are_validated_and_nothing_is_held_by_default() {
+ let dir = tempfile::tempdir().unwrap();
+ let a = LocalLeases::new(dir.path(), "a");
+ assert!(matches!(
+ a.acquire("../x", 0).await,
+ Err(LeaseError::Storage(_))
+ ));
+ assert!(a.holder("free").await.unwrap().is_none());
+ assert!(!dir.path().join("..").join("x").join(".lease").exists());
+}