diff --git a/ubisync-lib/src/lib.rs b/ubisync-lib/src/lib.rs index 0875afa..b06f1fb 100644 --- a/ubisync-lib/src/lib.rs +++ b/ubisync-lib/src/lib.rs @@ -2,16 +2,3 @@ pub mod api; pub mod messages; pub mod peer; pub mod types; - - -#[macro_export] -macro_rules! tracing_setup { - ($level:expr) => { - let _tracing_default_guard = tracing::subscriber::set_default( - tracing_subscriber::fmt() - .pretty() - .with_max_level($level) - .finish(), - ); - }; -} diff --git a/ubisync/src/comm/mod.rs b/ubisync/src/comm/mod.rs index 27c18c8..5b668f4 100644 --- a/ubisync/src/comm/mod.rs +++ b/ubisync/src/comm/mod.rs @@ -196,10 +196,57 @@ impl CommHandle { } #[cfg(test)] -mod tests {use i2p::net::I2pListener; +mod tests { + use std::time::Duration; + + use i2p::net::I2pListener; use i2p::sam::StreamForward; use i2p::sam_options::SAMOptions; use i2p::Session; + use ubisync_lib::messages::{Message, self}; + use ubisync_lib::types::ElementId; + + use crate::state::{CommState, State}; + use crate::Config; + + use super::CommHandle; + #[tokio::test(flavor = "multi_thread")] + pub async fn msg() { + let ch = CommHandle::new( + CommState::new(State::new().await.unwrap()), + &Config::default(), + ) + .unwrap(); + ch.run().await; + println!("My address: {:?}", ch.i2p_b32_address()); + + ch.send( + &ch.i2p_address().unwrap(), + Message::new(messages::MessageContent::Hello { + peer_name: "a".to_string(), + }), + ) + .await + .expect("Could not send hello"); + for i in 0..10 { + let result = ch + .send( + &ch.i2p_address().unwrap(), + Message::new(messages::MessageContent::CreateElement { + id: ElementId::new(), + content: ubisync_lib::types::ElementContent::Text(format!( + "hello world no. {}", + i + )), + }), + ) + .await; + tokio::time::sleep(Duration::from_millis(300)).await; + println!("Result of sending: {:?}", result); + } + + ch.stop().await; + } #[test] pub fn from_privkey() { diff --git a/ubisync/src/state/api_state.rs b/ubisync/src/state/api_state.rs index 8e1486e..4b33748 100644 --- a/ubisync/src/state/api_state.rs +++ b/ubisync/src/state/api_state.rs @@ -102,8 +102,7 @@ impl ApiState { #[cfg(test)] mod tests { - use tracing::Level; - use ubisync_lib::{types::ElementContent, tracing_setup}; + use ubisync_lib::types::ElementContent; use crate::state::State; @@ -112,8 +111,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn test_element_create() { - tracing_setup!(Level::DEBUG); - + tracing_subscriber::fmt().pretty().init(); let state = ApiState::new( State::new().await.unwrap(), "abcdabcdabcdabcdabcdabcdabcdabcd", @@ -131,8 +129,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn test_element_write() { - tracing_setup!(Level::DEBUG); - + tracing_subscriber::fmt().pretty().init(); let state = ApiState::new( State::new().await.unwrap(), "abcdabcdabcdabcdabcdabcdabcdabcd", diff --git a/ubisync/src/state/comm_state.rs b/ubisync/src/state/comm_state.rs index 3a2e9c2..f91c436 100644 --- a/ubisync/src/state/comm_state.rs +++ b/ubisync/src/state/comm_state.rs @@ -64,7 +64,7 @@ impl CommState { pub fn set_peer(&self, id: &PeerId, name: &str) -> anyhow::Result<()> { queries::peers::put(self.db(), id, name)?; - debug!("Set peer {} with address {}.", &name, id.to_string()); + debug!("Set peer {{{}}}", id.to_string()); Ok(()) } @@ -84,14 +84,12 @@ mod tests { use super::CommState; - use tracing::Level; - use ubisync_lib::{types::{ElementContent, ElementId, MessageId}, tracing_setup}; + use ubisync_lib::types::{ElementContent, ElementId, MessageId}; #[tokio::test] #[serial_test::serial] async fn test_element_add() { - tracing_setup!(Level::DEBUG); - + tracing_subscriber::fmt().pretty().init(); let state = CommState::new(State::new().await.unwrap()); let id = ElementId::new(); state @@ -111,8 +109,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn test_element_update() { - tracing_setup!(Level::DEBUG); - + tracing_subscriber::fmt().pretty().init(); let state = CommState::new(State::new().await.unwrap()); let id = ElementId::new(); state diff --git a/ubisync/tests/api.rs b/ubisync/tests/api.rs index 17accc0..367d389 100644 --- a/ubisync/tests/api.rs +++ b/ubisync/tests/api.rs @@ -4,14 +4,16 @@ use tracing::{debug, warn, Level}; use ubisync::{config::Config, Ubisync}; use ubisync_lib::{ api::element::{ElementCreateRequest, ElementGetRequest}, - types::{Element, ElementContent}, tracing_setup, + types::{Element, ElementContent}, }; use ubisync_sdk::UbisyncClient; - #[tokio::test(flavor = "multi_thread")] async fn two_nodes_element_creation() { - tracing_setup!(Level::DEBUG); + tracing_subscriber::fmt() + .pretty() + .with_max_level(Level::DEBUG) + .init(); // Two nodes need to bind to different ports let mut c2 = Config::default();