From d0a728a9b54e5a8adeb54c3a8b28672a0188f465 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 10 Feb 2021 00:57:49 -0600 Subject: [PATCH] Log & test connections --- src/data/actor.rs | 2 ++ src/db.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/data/actor.rs b/src/data/actor.rs index 31cb927..6d2d65a 100644 --- a/src/data/actor.rs +++ b/src/data/actor.rs @@ -57,11 +57,13 @@ impl ActorCache { } pub(crate) async fn add_connection(&self, actor: Actor) -> Result<(), MyError> { + log::debug!("Adding connection: {}", actor.id); self.db.add_connection(actor.id.clone()).await?; self.db.save_actor(actor).await } pub(crate) async fn remove_connection(&self, actor: &Actor) -> Result<(), MyError> { + log::debug!("Removing connection: {}", actor.id); self.db.remove_connection(actor.id.clone()).await } diff --git a/src/db.rs b/src/db.rs index 8955405..d35a992 100644 --- a/src/db.rs +++ b/src/db.rs @@ -194,8 +194,10 @@ impl Inner { impl Db { pub(crate) fn build(config: &Config) -> Result { let db = sled::open(config.sled_path())?; - let restricted_mode = config.restricted_mode(); + Self::build_inner(config.restricted_mode(), db) + } + fn build_inner(restricted_mode: bool, db: sled::Db) -> Result { Ok(Db { inner: Arc::new(Inner { actor_id_actor: db.open_tree("actor-id-actor")?, @@ -621,3 +623,29 @@ fn url_from_ivec(ivec: sled::IVec) -> Option { fn uuid_from_ivec(ivec: sled::IVec) -> Option { Uuid::from_slice(&ivec).ok() } + +#[cfg(test)] +mod tests { + use super::Db; + use activitystreams::url::Url; + use std::future::Future; + + #[test] + fn connect_and_verify() { + run(|db| async move { + let example_actor: Url = "http://example.com/actor".parse().unwrap(); + db.add_connection(example_actor.clone()).await.unwrap(); + assert!(db.is_connected(example_actor).await.unwrap()); + }) + } + + fn run(f: F) + where + F: Fn(Db) -> Fut, + Fut: Future + 'static, + { + let db = + Db::build_inner(true, sled::Config::new().temporary(true).open().unwrap()).unwrap(); + actix_rt::System::new("test").block_on((f)(db)); + } +}