Refactoring, mainly formatting

This commit is contained in:
Philip (a-0) 2024-01-05 20:48:23 +01:00
parent 3825263fa3
commit d8f1733eb3
25 changed files with 667 additions and 348 deletions

View file

@ -1,40 +1,86 @@
use std::time::Duration;
use tracing::{Level, debug};
use ubisync::{Ubisync, config::Config, state::types::{ElementContent, Element, ElementId}, api::AppDescription};
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();
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();
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 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();
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::<ElementId>(&put_resp_text).expect("Could not deserialize ElementId");
let id =
serde_json::from_str::<ElementId>(&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::<String>::into(&id))).header("Authorization", &format!("Bearer {}", &jwt2)).send().await.expect("Get request failed");
let get_resp = http_client
.get(&format!(
"http://localhost:9982/v0/element/{}",
Into::<String>::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::<Element>(&get_resp_text).expect("Could not deserialize Element");
let received_element =
serde_json::from_str::<Element>(&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);
}