Skip to content

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

test(storage): use shared memory backend in storage tests

Steven Enamakelccdc55f02410
diff --git a/crates/openhuman-core/src/security/devices/store_documents_tests.rs b/crates/openhuman-core/src/security/devices/store_documents_tests.rs
index 4fdcd4d2e8..3500f3266a 100644
--- a/crates/openhuman-core/src/security/devices/store_documents_tests.rs
+++ b/crates/openhuman-core/src/security/devices/store_documents_tests.rs
@@ -85,11 +85,11 @@ fn devices_list_oldest_first() {
#[test]
fn scopes_keep_devices_apart() {
- let storage = MemoryStorage::new();
- docs_in(&storage, "alice")
+ let storage = memory_backend();
+ docs_in(storage.as_ref(), "alice")
.insert_device("ch-1", "a", "pk", "h")
.unwrap();
- let bob = docs_in(&storage, "bob");
+ let bob = docs_in(storage.as_ref(), "bob");
assert!(bob.list_devices().unwrap().is_empty());
assert!(!bob.revoke_device("ch-1").unwrap());
}
diff --git a/crates/openhuman-core/src/storage/blob_sweep_tests.rs b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
index 4aedc904b5..2eb8a5a7b6 100644
--- a/crates/openhuman-core/src/storage/blob_sweep_tests.rs
+++ b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
@@ -1,16 +1,12 @@
use super::*;
-use std::sync::atomic::{AtomicU64, Ordering};
-use tinystoragedrivers::MemoryStorage;
-
-/// A memory backend on a clock the test advances by hand.
-fn backend_with_clock() -> (Arc<AtomicU64>, Arc<dyn StorageBackend>) {
- let now = Arc::new(AtomicU64::new(1_000));
- let clock = Arc::clone(&now);
- let backend: Arc<dyn StorageBackend> =
- Arc::new(MemoryStorage::with_clock(Arc::new(move || {
- clock.load(Ordering::SeqCst)
- })));
- (now, backend)
+use std::{ops::Range, sync::Arc, time::Duration};
+use tinystoragedrivers_api::{BlobStore, Storage, StorageBackend};
+use tinystoragedrivers_types::{Blob, BlobMeta, Capabilities, Capability, Result, Scope, StorageError};
+
+/// The TSD testkit memory engine uses its real clock; tests wait on the
+/// backend's observable expiry instead of mutating a process-global clock.
+fn backend_with_clock() -> Arc<dyn StorageBackend> {
+ crate::storage::test_support::memory_backend()
}
async fn put_expiring(backend: &Arc<dyn StorageBackend>, scope: &Scope, key: &str, ms: u64) {
@@ -23,16 +19,37 @@ async fn put_expiring(backend: &Arc<dyn StorageBackend>, scope: &Scope, key: &st
.unwrap();
}
+async fn wait_until_expired(backend: &Arc<dyn StorageBackend>, scope: &Scope, key: &str) {
+ tokio::time::timeout(Duration::from_secs(2), async {
+ loop {
+ if backend
+ .for_scope(scope)
+ .unwrap()
+ .blobs()
+ .get(key)
+ .await
+ .unwrap()
+ .is_none()
+ {
+ return;
+ }
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("the TSD memory blob's recorded TTL eventually lapses");
+}
+
#[tokio::test]
async fn sweep_scope_reclaims_only_lapsed_blobs_of_that_scope() {
- let (now, backend) = backend_with_clock();
+ let backend = backend_with_clock();
let local = Scope::local();
let other = crate::storage::scope_for_agent("blob-sweep-other");
put_expiring(&backend, &local, "short", 10).await;
put_expiring(&backend, &local, "long", 10_000).await;
put_expiring(&backend, &other, "short", 10).await;
- now.store(2_000, Ordering::SeqCst);
+ wait_until_expired(&backend, &local, "short").await;
assert_eq!(sweep_scope(backend.as_ref(), &local).await, 1);
assert_eq!(sweep_scope(backend.as_ref(), &local).await, 0, "idempotent");
assert_eq!(
@@ -46,9 +63,9 @@ async fn sweep_scope_reclaims_only_lapsed_blobs_of_that_scope() {
#[tokio::test]
async fn sweep_all_visits_the_local_scope() {
- let (now, backend) = backend_with_clock();
+ let backend = backend_with_clock();
put_expiring(&backend, &Scope::local(), "gone", 10).await;
- now.store(2_000, Ordering::SeqCst);
+ wait_until_expired(&backend, &Scope::local(), "gone").await;
assert_eq!(sweep_all(Arc::clone(&backend)).await, 1);
assert_eq!(sweep_all(backend).await, 0);
}
@@ -67,7 +84,7 @@ async fn spawned_sweeper_waits_for_its_first_delay_then_keeps_ticking() {
#[tokio::test]
async fn sweeping_through_the_fenced_backend_reaches_the_driver() {
- let (now, inner) = backend_with_clock();
+ let inner = backend_with_clock();
let scope = Scope::local();
put_expiring(&inner, &scope, "gone", 10).await;
now.store(2_000, Ordering::SeqCst);
@@ -82,13 +99,13 @@ async fn sweeping_through_the_fenced_backend_reaches_the_driver() {
#[cfg(feature = "modules")]
#[tokio::test]
async fn module_blob_scopes_are_swept_alongside_the_acting_scope() {
- let (now, backend) = backend_with_clock();
+ let backend = backend_with_clock();
let module_scopes = crate::modules::expiring_blob_module_scopes(
&crate::core::runtime::DispatchCtx::host_default(),
);
assert!(!module_scopes.is_empty(), "TinyJuice owns expiring blobs");
put_expiring(&backend, &module_scopes[0], "tinyjuice_ccr/t", 10).await;
- now.store(2_000, Ordering::SeqCst);
+ wait_until_expired(&backend, &module_scopes[0], "tinyjuice_ccr/t").await;
assert_eq!(sweep_all(backend).await, 1);
}
@@ -105,13 +122,14 @@ fn no_module_blob_scopes_without_the_modules_feature() {
#[tokio::test]
async fn sweep_profiles_reclaims_each_profiles_own_scope() {
- let (now, backend) = backend_with_clock();
+ let backend = backend_with_clock();
let acme = crate::storage::scope_for_profile("acme");
let globex = crate::storage::scope_for_profile("globex");
put_expiring(&backend, &acme, "gone", 10).await;
put_expiring(&backend, &acme, "kept", 10_000).await;
put_expiring(&backend, &globex, "gone", 10).await;
- now.store(2_000, Ordering::SeqCst);
+ wait_until_expired(&backend, &acme, "gone").await;
+ wait_until_expired(&backend, &globex, "gone").await;
let ids = vec!["acme".to_string(), "globex".to_string()];
assert_eq!(sweep_profiles(backend.as_ref(), &ids).await, 2);
@@ -126,10 +144,10 @@ async fn sweep_profiles_reclaims_each_profiles_own_scope() {
#[tokio::test]
async fn sweep_profiles_skips_profiles_not_listed() {
- let (now, backend) = backend_with_clock();
+ let backend = backend_with_clock();
let other = crate::storage::scope_for_profile("not-listed");
put_expiring(&backend, &other, "gone", 10).await;
- now.store(2_000, Ordering::SeqCst);
+ wait_until_expired(&backend, &other, "gone").await;
assert_eq!(sweep_profiles(backend.as_ref(), &[]).await, 0);
assert_eq!(sweep_scope(backend.as_ref(), &other).await, 1);
}
@@ -137,11 +155,11 @@ async fn sweep_profiles_skips_profiles_not_listed() {
#[cfg(feature = "modules")]
#[tokio::test]
async fn sweep_profiles_reaches_the_modules_scopes_for_that_profile() {
- let (now, backend) = backend_with_clock();
+ let backend = backend_with_clock();
let module_scopes = crate::modules::expiring_blob_module_scopes_for("acme");
assert!(!module_scopes.is_empty(), "TinyJuice owns expiring blobs");
put_expiring(&backend, &module_scopes[0], "tinyjuice_ccr/t", 10).await;
- now.store(2_000, Ordering::SeqCst);
+ wait_until_expired(&backend, &module_scopes[0], "tinyjuice_ccr/t").await;
assert_eq!(
sweep_profiles(backend.as_ref(), &["acme".to_string()]).await,
1
diff --git a/crates/openhuman-core/src/storage/secrets_tests.rs b/crates/openhuman-core/src/storage/secrets_tests.rs
index f2a72eb886..e3ffcc21b0 100644
--- a/crates/openhuman-core/src/storage/secrets_tests.rs
+++ b/crates/openhuman-core/src/storage/secrets_tests.rs
@@ -1,19 +1,19 @@
use super::*;
-use crate::storage::{MemoryStorage, Scope, StorageBackend};
+use crate::storage::{Scope, StorageBackend, test_support::memory_backend};
fn keys(byte: u8) -> Arc<dyn KeyProvider> {
Arc::new(DerivedKeys::new(Zeroizing::new([byte; 32])))
}
-fn secrets_in(storage: &MemoryStorage, scope: &str, key: u8) -> DocumentSecrets {
+fn secrets_in(storage: &dyn StorageBackend, scope: &str, key: u8) -> DocumentSecrets {
let scope = Scope::new(scope).unwrap();
over(&storage.for_scope(&scope).unwrap(), scope, keys(key))
}
#[test]
fn a_secret_round_trips_from_sync_code() {
- let storage = MemoryStorage::new();
- let secrets = secrets_in(&storage, "local", 7);
+ let storage = memory_backend();
+ let secrets = secrets_in(storage.as_ref(), "local", 7);
assert!(get_blocking(&secrets, "user:token").unwrap().is_none());
set_blocking(&secrets, "user:token", b"s3cret").unwrap();
assert_eq!(
@@ -27,25 +27,25 @@ fn a_secret_round_trips_from_sync_code() {
#[test]
fn scopes_and_keys_keep_secrets_apart() {
- let storage = MemoryStorage::new();
- set_blocking(&secrets_in(&storage, "alice", 7), "user:token", b"a").unwrap();
+ let storage = memory_backend();
+ set_blocking(&secrets_in(storage.as_ref(), "alice", 7), "user:token", b"a").unwrap();
assert!(
- get_blocking(&secrets_in(&storage, "bob", 7), "user:token")
+ get_blocking(&secrets_in(storage.as_ref(), "bob", 7), "user:token")
.unwrap()
.is_none(),
"another scope sees nothing"
);
assert!(
- get_blocking(&secrets_in(&storage, "alice", 8), "user:token").is_err(),
+ get_blocking(&secrets_in(storage.as_ref(), "alice", 8), "user:token").is_err(),
"a different master key cannot decrypt"
);
}
#[test]
fn the_stored_document_holds_no_plaintext() {
- let storage = MemoryStorage::new();
+ let storage = memory_backend();
set_blocking(
- &secrets_in(&storage, "local", 7),
+ &secrets_in(storage.as_ref(), "local", 7),
"user:token",
b"plain-value",
)