Skip to content

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

refactor(modules): move the test module storage into its own file

M3gA-Mind5ce47adfb427
diff --git a/crates/openhuman-core/src/integrations/composio/module_client.rs b/crates/openhuman-core/src/integrations/composio/module_client.rs
index 4b31870213..66350236d7 100644
--- a/crates/openhuman-core/src/integrations/composio/module_client.rs
+++ b/crates/openhuman-core/src/integrations/composio/module_client.rs
@@ -318,7 +318,7 @@ pub(crate) struct ModuleGuard {
// next test may take the module. Without the module loader there are no
// module calls to serve.
#[cfg(feature = "modules")]
- _storage: crate::modules::storage_host::TestModuleStorage,
+ _storage: crate::modules::storage_host::test_module::TestModuleStorage,
_lock: tokio::sync::MutexGuard<'static, ()>,
}
@@ -334,7 +334,7 @@ pub(crate) async fn module_guard() -> ModuleGuard {
let lock = module_lock_guard().await;
ModuleGuard {
#[cfg(feature = "modules")]
- _storage: crate::modules::storage_host::serve_test_module_storage(
+ _storage: crate::modules::storage_host::test_module::serve_test_module_storage(
crate::storage::test_support::memory_backend(),
),
_lock: lock,
diff --git a/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs
index 17eb25d409..10e4573188 100644
--- a/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs
@@ -247,7 +247,7 @@ async fn an_unreadable_preference_denies_at_every_gate() {
.await
.expect("the module stores the pref while storage is served");
#[cfg(feature = "modules")]
- crate::modules::storage_host::withdraw_test_module_storage();
+ crate::modules::storage_host::test_module::withdraw_test_module_storage();
#[cfg(not(feature = "modules"))]
crate::storage::clear();
diff --git a/crates/openhuman-core/src/modules/storage_host.rs b/crates/openhuman-core/src/modules/storage_host.rs
index 778aa6abf7..04718f6a49 100644
--- a/crates/openhuman-core/src/modules/storage_host.rs
+++ b/crates/openhuman-core/src/modules/storage_host.rs
@@ -576,56 +576,9 @@ pub(crate) fn current_module_scope(
module_scope(tenant.profile.as_deref(), module_id)
}
-/// Storage for module calls in unit tests that hold a module lock (the
-/// connector module's `module_guard`). A module call carries no workspace, so
-/// such a test needs a backend; this one is read only here, so it is never
-/// another test's storage the way an installed process slot is (MAIN-57).
#[cfg(test)]
-static TEST_MODULE_STORAGE: std::sync::Mutex<Option<Arc<dyn StorageBackend>>> =
- std::sync::Mutex::new(None);
-
-#[cfg(test)]
-fn test_module_storage() -> Option<Arc<dyn StorageBackend>> {
- TEST_MODULE_STORAGE
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .clone()
-}
-
-/// Serve `backend` to module calls until the returned guard drops. Hold the
-/// module's test lock for as long as it lives.
-#[cfg(test)]
-pub(crate) fn serve_test_module_storage(backend: Arc<dyn StorageBackend>) -> TestModuleStorage {
- let previous = TEST_MODULE_STORAGE
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .replace(backend);
- TestModuleStorage { previous }
-}
-
-/// Withdraw the backend module calls are served, as a storage outage would,
-/// while the guard from [`serve_test_module_storage`] is still held.
-#[cfg(test)]
-pub(crate) fn withdraw_test_module_storage() {
- *TEST_MODULE_STORAGE
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner) = None;
-}
-
-/// Restores the previous test module storage on drop.
-#[cfg(test)]
-pub(crate) struct TestModuleStorage {
- previous: Option<Arc<dyn StorageBackend>>,
-}
-
-#[cfg(test)]
-impl Drop for TestModuleStorage {
- fn drop(&mut self) {
- *TEST_MODULE_STORAGE
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner) = self.previous.take();
- }
-}
+#[path = "storage_host_test_module.rs"]
+pub(crate) mod test_module;
/// Production wiring: the broker's name owners, the installed backend, the
/// host's scope rule and the compiled grants.
@@ -656,7 +609,7 @@ impl Environment for Live {
// so a module is not the one consumer left with no storage.
let installed = crate::storage::installed();
#[cfg(test)]
- let installed = installed.or_else(test_module_storage);
+ let installed = installed.or_else(test_module::test_module_storage);
installed.or_else(|| {
let workspace = crate::core::runtime::DispatchCtx::ambient()
.resolved()?
diff --git a/crates/openhuman-core/src/modules/storage_host_test_module.rs b/crates/openhuman-core/src/modules/storage_host_test_module.rs
new file mode 100644
index 0000000000..d158e7a22e
--- /dev/null
+++ b/crates/openhuman-core/src/modules/storage_host_test_module.rs
@@ -0,0 +1,52 @@
+//! Test-only storage for module calls while a unit test holds a module lock
+//! (the connector module's `module_guard`). Kept out of the process storage
+//! slot so it is never another concurrent test's storage (MAIN-57).
+
+use std::sync::Arc;
+
+use crate::storage::StorageBackend;
+
+/// Storage for module calls in unit tests that hold a module lock (the
+/// connector module's `module_guard`). A module call carries no workspace, so
+/// such a test needs a backend; this one is read only here, so it is never
+/// another test's storage the way an installed process slot is (MAIN-57).
+static TEST_MODULE_STORAGE: std::sync::Mutex<Option<Arc<dyn StorageBackend>>> =
+ std::sync::Mutex::new(None);
+
+pub(super) fn test_module_storage() -> Option<Arc<dyn StorageBackend>> {
+ TEST_MODULE_STORAGE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .clone()
+}
+
+/// Serve `backend` to module calls until the returned guard drops. Hold the
+/// module's test lock for as long as it lives.
+pub(crate) fn serve_test_module_storage(backend: Arc<dyn StorageBackend>) -> TestModuleStorage {
+ let previous = TEST_MODULE_STORAGE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .replace(backend);
+ TestModuleStorage { previous }
+}
+
+/// Withdraw the backend module calls are served, as a storage outage would,
+/// while the guard from [`serve_test_module_storage`] is still held.
+pub(crate) fn withdraw_test_module_storage() {
+ *TEST_MODULE_STORAGE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = None;
+}
+
+/// Restores the previous test module storage on drop.
+pub(crate) struct TestModuleStorage {
+ previous: Option<Arc<dyn StorageBackend>>,
+}
+
+impl Drop for TestModuleStorage {
+ fn drop(&mut self) {
+ *TEST_MODULE_STORAGE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = self.previous.take();
+ }
+}
diff --git a/scripts/ci/process-statics.json b/scripts/ci/process-statics.json
index 78b00ba65f..18d4fac65f 100644
--- a/scripts/ci/process-statics.json
+++ b/scripts/ci/process-statics.json
@@ -400,7 +400,7 @@
"verification": "modules/runtime_tests.rs: the_module_bus_is_a_singleton_and_serves_proxies"
},
{
- "file": "crates/openhuman-core/src/modules/storage_host.rs",
+ "file": "crates/openhuman-core/src/modules/storage_host_test_module.rs",
"scope": "",
"name": "TEST_MODULE_STORAGE",
"type": "std::sync::Mutex<Option<Arc<dyn StorageBackend>>>",