use std::time::Duration; use tracing::{debug, Level}; use ubisync::{ api::v0::app::AppDescription, config::Config, state::types::{Element, ElementContent, ElementId}, Ubisync, }; #[tokio::test(flavor = "multi_thread")] async fn two_nodes_element_creation() { tracing_subscriber::fmt() .pretty() .with_max_level(Level::DEBUG) .init(); // Two nodes need to bind to different ports let mut c2 = Config::default(); c2.api_config.port = Some(9982); let ubi1 = Ubisync::new(&Config::default()).await.unwrap(); let ubi2 = Ubisync::new(&c2).await.unwrap(); ubi1.add_peer_from_id(ubi2.get_destination().unwrap().into()) .unwrap(); let http_client = reqwest::Client::new(); let register_response = http_client .put("http://localhost:9981/v0/app/register") .json(&AppDescription { name: "Test".to_string(), desc_text: "desc".to_string(), }) .send() .await .unwrap(); let jwt1 = register_response .text() .await .expect("Couldn't fetch token from response"); let register_response = http_client .put("http://localhost:9982/v0/app/register") .json(&AppDescription { name: "Test".to_string(), desc_text: "desc".to_string(), }) .send() .await .unwrap(); let jwt2 = register_response .text() .await .expect("Couldn't fetch token from response"); let test_element_content = ElementContent::Text("Text".to_string()); let put_resp = http_client .put(&format!("http://localhost:9981/v0/element")) .json(&test_element_content) .header("Authorization", &format!("Bearer {}", &jwt1)) .send() .await .unwrap(); debug!("{:?}", &put_resp); let put_resp_text = put_resp.text().await.expect("No put response body"); debug!("{}", put_resp_text); let id = serde_json::from_str::(&put_resp_text).expect("Could not deserialize ElementId"); tokio::time::sleep(Duration::from_millis(3000)).await; let get_resp = http_client .get(&format!( "http://localhost:9982/v0/element/{}", Into::::into(&id) )) .header("Authorization", &format!("Bearer {}", &jwt2)) .send() .await .expect("Get request failed"); let get_resp_text = get_resp.text().await.expect("No get request body"); debug!("{}", get_resp_text); let received_element = serde_json::from_str::(&get_resp_text).expect("Could not deserialize Element"); debug!("Other node received this element: {:?}", received_element); assert_eq!(&test_element_content, received_element.content()); std::process::exit(0); }