Skip to content

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

test(storage): cover lease expiry, ownership and epoch edge cases

Steven Enamakel13531c9a011e
diff --git a/crates/openhuman-core/proptest-regressions/storage/lease_model_tests.txt b/crates/openhuman-core/proptest-regressions/storage/lease_model_tests.txt
new file mode 100644
index 0000000000..f40e0e21ec
--- /dev/null
+++ b/crates/openhuman-core/proptest-regressions/storage/lease_model_tests.txt
@@ -0,0 +1,7 @@
+# Seeds for failure cases proptest has generated in the past. It is
+# automatically read and these particular cases re-run before any
+# novel cases are generated.
+#
+# It is recommended to check this file in to source control so that
+# everyone who runs the test benefits from these saved cases.
+cc 0a42ad3732baaf6285f7bd8e5793b4a4181e09b7b0684d572d5536026ae7aa43 # shrinks to (nodes, steps) = (2, [Acquire(0), Advance(1000), Acquire(0)])
diff --git a/crates/openhuman-core/src/storage/lease_documents_tests.rs b/crates/openhuman-core/src/storage/lease_documents_tests.rs
index 5dc38be358..c71b3224ab 100644
--- a/crates/openhuman-core/src/storage/lease_documents_tests.rs
+++ b/crates/openhuman-core/src/storage/lease_documents_tests.rs
@@ -1,6 +1,7 @@
use super::*;
use std::time::Duration;
+
use crate::storage::MemoryStorage;
const TTL: Duration = Duration::from_millis(1_000);
@@ -177,3 +178,39 @@ fn sqlite_threads_racing_for_a_key_have_one_winner() {
race_on(url, 8);
}
}
+
+#[tokio::test]
+async fn renew_after_expiry_is_lost_even_without_takeover() {
+ let storage = MemoryStorage::new();
+ let a = node(&storage, "a");
+ let grant = a.acquire("k", 0).await.unwrap();
+ assert!(matches!(
+ a.renew(&grant, 2_000).await,
+ Err(LeaseError::Lost)
+ ));
+ assert!(a.renew(&grant, 999).await.is_ok());
+}
+
+#[tokio::test]
+async fn another_instances_grant_cannot_renew_or_release() {
+ let storage = MemoryStorage::new();
+ let (a, b) = (node(&storage, "a"), node(&storage, "b"));
+ let grant = a.acquire("k", 0).await.unwrap();
+ assert!(matches!(b.renew(&grant, 10).await, Err(LeaseError::Lost)));
+ assert!(matches!(
+ b.release(grant.clone()).await,
+ Err(LeaseError::Lost)
+ ));
+ assert_eq!(a.holder("k").await.unwrap().unwrap().owner, "a");
+ assert!(a.renew(&grant, 10).await.is_ok());
+}
+
+#[tokio::test]
+async fn sub_millisecond_ttl_does_not_expire_at_acquisition() {
+ let storage = MemoryStorage::new();
+ let a = DocumentLeases::cluster(&storage, "a", None, Duration::ZERO).unwrap();
+ let b = node(&storage, "b");
+ let grant = a.acquire("k", 5).await.unwrap();
+ assert!(grant.expires_at_ms > 5);
+ assert!(matches!(b.acquire("k", 5).await, Err(LeaseError::Held(_))));
+}
diff --git a/crates/openhuman-core/src/storage/lease_tests.rs b/crates/openhuman-core/src/storage/lease_tests.rs
index a6d2e3650a..db735742d7 100644
--- a/crates/openhuman-core/src/storage/lease_tests.rs
+++ b/crates/openhuman-core/src/storage/lease_tests.rs
@@ -112,3 +112,35 @@ fn lease_errors_name_the_holder() {
assert_eq!(error.to_string(), "lease held by n2 (epoch 3)");
assert_eq!(LeaseError::Lost.to_string(), "lease lost");
}
+
+#[test]
+fn expired_reentrant_record_advances_the_epoch() {
+ let found = record("n1", 4, 100, false);
+ assert_eq!(
+ decide(Some(&found), "n1", Some(4), 99),
+ Takeover::Take {
+ epoch: 4,
+ unclean: false
+ }
+ );
+ assert_eq!(
+ decide(Some(&found), "n1", Some(4), 100),
+ Takeover::Take {
+ epoch: 5,
+ unclean: true
+ }
+ );
+}
+
+#[test]
+fn exhausted_epoch_is_refused_not_reused() {
+ let found = record("n2", u64::MAX, 100, false);
+ assert_eq!(decide(Some(&found), "n1", None, 100), Takeover::Exhausted);
+}
+
+#[test]
+fn local_dirs_do_not_alias_by_case() {
+ let root = std::path::Path::new("/r");
+ assert_ne!(local_dir(root, "alice"), local_dir(root, "ALICE"));
+ assert_eq!(local_dir(root, "alice"), local_dir(root, "alice"));
+}