Skip to content

Commit

Permalink
Fix node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Jul 13, 2023
1 parent 878a37d commit f6f2ecf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
17 changes: 2 additions & 15 deletions node/src/jsonrpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,24 +525,11 @@ mod tests {
use jsonrpc_core::types::params::Params;

use super::*;
use crate::prelude::rings_core::dht::Stabilization;
use crate::prelude::*;
use crate::processor::*;
use crate::tests::prepare_processor;

async fn new_rnd_meta() -> RpcMeta {
let key = SecretKey::random();
let session_manager = SessionManager::new_with_seckey(&key).unwrap();
let path = uuid::Uuid::new_v4().to_simple().to_string();
let storage = PersistenceStorage::new_with_cap_and_path(1000, path.as_str())
.await
.unwrap();
let swarm = Arc::new(
SwarmBuilder::new("stun://stun.l.google.com:19302", storage, session_manager)
.message_callback(None)
.build(),
);
let stab = Arc::new(Stabilization::new(swarm.clone(), 20));
let processor: Processor = (swarm, stab).into();
let (processor, _) = prepare_processor().await;
Arc::new(processor).into()
}

Expand Down
49 changes: 7 additions & 42 deletions node/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,48 +515,13 @@ mod test {
use futures::lock::Mutex;

use super::*;
use crate::prelude::rings_core::ecc::SecretKey;
use crate::prelude::rings_core::storage::PersistenceStorage;
use crate::prelude::rings_core::swarm::SwarmBuilder;
use crate::prelude::*;

async fn new_processor() -> (Processor, String) {
let key = SecretKey::random();
let session_manager = SessionManager::new_with_seckey(&key).unwrap();

let stun = "stun://stun.l.google.com:19302";
let path = PersistenceStorage::random_path("./tmp");
let storage = PersistenceStorage::new_with_path(path.as_str())
.await
.unwrap();

let swarm = Arc::new(SwarmBuilder::new(stun, storage, session_manager).build());
let stabilization = Arc::new(Stabilization::new(swarm.clone(), 200));
((swarm, stabilization).into(), path)
}

async fn new_processor_with_callback(callback: CallbackFn) -> (Processor, String) {
let key = SecretKey::random();
let session_manager = SessionManager::new_with_seckey(&key).unwrap();

let stun = "stun://stun.l.google.com:19302";
let path = PersistenceStorage::random_path("./tmp");
let storage = PersistenceStorage::new_with_path(path.as_str())
.await
.unwrap();

let swarm = Arc::new(
SwarmBuilder::new(stun, storage, session_manager)
.message_callback(Some(callback))
.build(),
);
let stabilization = Arc::new(Stabilization::new(swarm.clone(), 200));
((swarm, stabilization).into(), path)
}
use crate::tests::prepare_processor;
use crate::tests::prepare_processor_with_callback;

#[tokio::test]
async fn test_processor_create_offer() {
let (processor, path) = new_processor().await;
let (processor, path) = prepare_processor().await;
let ti = processor.swarm.create_offer().await.unwrap();
let pendings = processor.swarm.pending_transports().await.unwrap();
assert_eq!(pendings.len(), 1);
Expand All @@ -566,7 +531,7 @@ mod test {

#[tokio::test]
async fn test_processor_list_pendings() {
let (processor, path) = new_processor().await;
let (processor, path) = prepare_processor().await;
let ti0 = processor.swarm.create_offer().await.unwrap();
let ti1 = processor.swarm.create_offer().await.unwrap();
let pendings = processor.swarm.pending_transports().await.unwrap();
Expand All @@ -586,7 +551,7 @@ mod test {

#[tokio::test]
async fn test_processor_close_pending_transport() {
let (processor, path) = new_processor().await;
let (processor, path) = prepare_processor().await;
let ti0 = processor.swarm.create_offer().await.unwrap();
let _ti1 = processor.swarm.create_offer().await.unwrap();
let ti2 = processor.swarm.create_offer().await.unwrap();
Expand Down Expand Up @@ -688,8 +653,8 @@ mod test {
msgs: msgs2.clone(),
});

let (p1, path1) = new_processor_with_callback(callback1).await;
let (p2, path2) = new_processor_with_callback(callback2).await;
let (p1, path1) = prepare_processor_with_callback(Some(callback1)).await;
let (p2, path2) = prepare_processor_with_callback(Some(callback2)).await;
let did1 = p1.did().to_string();
let did2 = p2.did().to_string();

Expand Down
37 changes: 36 additions & 1 deletion node/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
#[cfg(feature = "browser")]
pub mod wasm;

use crate::prelude::rings_core::ecc::SecretKey;
use crate::prelude::rings_core::storage::PersistenceStorage;
use crate::prelude::CallbackFn;
use crate::prelude::SessionManager;
use crate::processor::Processor;
use crate::processor::ProcessorBuilder;
use crate::processor::ProcessorConfig;

pub async fn prepare_processor(key: SecretKey) -> Processor {}
pub async fn prepare_processor_with_callback(
message_callback: Option<CallbackFn>,
) -> (Processor, String) {
let key = SecretKey::random();
let sm = SessionManager::new_with_seckey(&key).unwrap();

let config = serde_yaml::to_string(&ProcessorConfig {
ice_servers: "stun://stun.l.google.com:19302".to_string(),
external_address: None,
session_manager: sm.dump().unwrap(),
stabilize_timeout: 200,
})
.unwrap();

let storage_path = PersistenceStorage::random_path("./tmp");
let storage = PersistenceStorage::new_with_path(storage_path.as_str())
.await
.unwrap();

let procssor = ProcessorBuilder::from_config(config)
.unwrap()
.storage(storage)
.message_callback(message_callback)
.build()
.unwrap();

(procssor, storage_path)
}

pub async fn prepare_processor() -> (Processor, String) {
prepare_processor_with_callback(None).await
}

0 comments on commit f6f2ecf

Please sign in to comment.