Skip to content

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

test(modules): cover storage host watching and cdylib paths

Steven Enamakel7ca4314a1b46
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index 702d412e4e..1ad36c9642 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -107,6 +107,8 @@ pub mod storage_grants;
pub(crate) mod storage_host;
#[cfg(feature = "modules")]
mod storage_host_linked;
+#[cfg(test)]
+mod storage_host_watching;
#[cfg(feature = "modules")]
mod storage_import_support;
#[cfg(feature = "modules")]
diff --git a/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs b/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs
index 785dc019b8..d2d7c19a3d 100644
--- a/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs
@@ -192,7 +192,8 @@ async fn rig_with_peers(module_id: &'static str, others: &[&'static str]) -> Rig
let stranger = Connection::connect(transport.connect().await.unwrap())
.await
.unwrap();
- let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let backend: Arc<dyn StorageBackend> =
+ Arc::new(crate::modules::storage_host_watching::WatchingStorage::new());
let trail = Arc::new(Trail::default());
let mut peers = Vec::new();
let mut named = vec![(PROBE, module_id)];
diff --git a/crates/openhuman-core/src/modules/storage_host_tests.rs b/crates/openhuman-core/src/modules/storage_host_tests.rs
index a5f63bfb43..ecd794bd10 100644
--- a/crates/openhuman-core/src/modules/storage_host_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_host_tests.rs
@@ -100,7 +100,7 @@ struct Rig {
}
fn rig() -> Rig {
- let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let backend: Arc<dyn StorageBackend> = Arc::new(super::super::storage_host_watching::WatchingStorage::new());
let env = Arc::new(Env {
backend: Mutex::new(Some(Arc::clone(&backend))),
profile: Mutex::new(None),
diff --git a/crates/openhuman-core/src/modules/storage_host_watching.rs b/crates/openhuman-core/src/modules/storage_host_watching.rs
new file mode 100644
index 0000000000..e89518528a
--- /dev/null
+++ b/crates/openhuman-core/src/modules/storage_host_watching.rs
@@ -0,0 +1,152 @@
+//! A test backend whose documents report a native watch.
+//!
+//! The in-memory driver has no watch, and the host serves `WatchOpen` from the
+//! driver's native watch (it never polls on a module's behalf). This wraps a
+//! `MemoryStorage` and fires an `Invalidated` notice at every watcher of a
+//! scope when one of its documents is written or deleted, which is all the
+//! watch tests need to prove how the signal travels.
+
+use std::collections::HashMap;
+use std::sync::{Arc, Mutex};
+
+use serde_json::Value;
+use tinystoragedrivers::{
+ async_trait, Capabilities, Capability, CollectionSpec, DocumentStore, DocumentWatch, Filter,
+ MemoryStorage, Page, Precondition, Query, Result, Scope, ScopedStorage, Sort, StorageBackend,
+ Version, Versioned, WatchNotice, WatchOptions,
+};
+use tokio::sync::broadcast;
+
+type Hub = Arc<Mutex<HashMap<String, broadcast::Sender<()>>>>;
+
+/// Memory storage with a native document watch.
+#[derive(Debug, Default)]
+pub(super) struct WatchingStorage {
+ inner: MemoryStorage,
+ hub: Hub,
+}
+
+impl WatchingStorage {
+ pub(super) fn new() -> Self {
+ Self::default()
+ }
+}
+
+impl StorageBackend for WatchingStorage {
+ fn driver(&self) -> &'static str {
+ "memory"
+ }
+
+ fn capabilities(&self) -> Capabilities {
+ self.inner.capabilities().with(Capability::Watch)
+ }
+
+ fn for_scope(&self, scope: &Scope) -> Result<ScopedStorage> {
+ let scoped = self.inner.for_scope(scope)?;
+ let sender = {
+ let mut hub = self.hub.lock().unwrap();
+ hub.entry(scope.as_str().to_string())
+ .or_insert_with(|| broadcast::channel(64).0)
+ .clone()
+ };
+ Ok(ScopedStorage::new(
+ scope.clone(),
+ "memory",
+ Arc::new(Documents {
+ inner: Arc::clone(scoped.documents()),
+ changed: sender,
+ }),
+ Arc::clone(scoped.streams()),
+ Arc::clone(scoped.blobs()),
+ ))
+ }
+}
+
+#[derive(Debug)]
+struct Documents {
+ inner: Arc<dyn DocumentStore>,
+ changed: broadcast::Sender<()>,
+}
+
+#[derive(Debug)]
+struct Feed(broadcast::Receiver<()>);
+
+#[async_trait]
+impl DocumentWatch for Feed {
+ async fn next(&mut self) -> Result<Option<WatchNotice>> {
+ loop {
+ match self.0.recv().await {
+ Ok(()) | Err(broadcast::error::RecvError::Lagged(_)) => {
+ return Ok(Some(WatchNotice::Invalidated { cursor: None }));
+ }
+ Err(broadcast::error::RecvError::Closed) => return Ok(None),
+ }
+ }
+ }
+}
+
+#[async_trait]
+impl DocumentStore for Documents {
+ fn capabilities(&self) -> Capabilities {
+ self.inner.capabilities().with(Capability::Watch)
+ }
+
+ async fn watch(
+ &self,
+ _collection: &str,
+ _filter: &Filter,
+ _options: &WatchOptions,
+ ) -> Result<Box<dyn DocumentWatch>> {
+ Ok(Box::new(Feed(self.changed.subscribe())))
+ }
+
+ async fn ensure_collection(&self, spec: &CollectionSpec) -> Result<()> {
+ self.inner.ensure_collection(spec).await
+ }
+
+ async fn get(&self, c: &str, id: &str) -> Result<Option<Versioned<Value>>> {
+ self.inner.get(c, id).await
+ }
+
+ async fn put(&self, c: &str, id: &str, doc: Value, p: Precondition) -> Result<Version> {
+ let version = self.inner.put(c, id, doc, p).await?;
+ let _ = self.changed.send(());
+ Ok(version)
+ }
+
+ async fn delete(&self, c: &str, id: &str, p: Precondition) -> Result<bool> {
+ let deleted = self.inner.delete(c, id, p).await?;
+ let _ = self.changed.send(());
+ Ok(deleted)
+ }
+
+ async fn query(&self, c: &str, q: &Query) -> Result<Page<Versioned<Value>>> {
+ self.inner.query(c, q).await
+ }
+
+ async fn count(&self, c: &str, f: &Filter) -> Result<u64> {
+ self.inner.count(c, f).await
+ }
+
+ async fn delete_where(&self, c: &str, f: &Filter) -> Result<u64> {
+ let deleted = self.inner.delete_where(c, f).await?;
+ let _ = self.changed.send(());
+ Ok(deleted)
+ }
+
+ async fn claim(
+ &self,
+ c: &str,
+ f: &Filter,
+ s: &[Sort],
+ p: &Value,
+ ) -> Result<Option<Versioned<Value>>> {
+ let claimed = self.inner.claim(c, f, s, p).await?;
+ let _ = self.changed.send(());
+ Ok(claimed)
+ }
+
+ async fn drop_collection(&self, c: &str) -> Result<()> {
+ self.inner.drop_collection(c).await
+ }
+}