Skip to content

Commit

Permalink
[sync] Add a test for test sync by notification message.
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Dec 1, 2020
1 parent 35ae7da commit 2a78f19
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion network/api/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl NotificationMessage {
pub fn encode_notification(&self) -> Result<(Cow<'static, str>, Vec<u8>)> {
Ok(match self {
NotificationMessage::Transactions(msg) => (TXN_PROTOCOL_NAME.into(), msg.encode()?),
NotificationMessage::CompactBlock(msg) => (TXN_PROTOCOL_NAME.into(), msg.encode()?),
NotificationMessage::CompactBlock(msg) => (BLOCK_PROTOCOL_NAME.into(), msg.encode()?),
})
}

Expand Down
2 changes: 1 addition & 1 deletion network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl Inner {
network_msg.peer_id,
);
if let Err(e) = inner.handle_network_message(network_msg).await {
warn!("Handle_network_message error: {:?}", e);
error!("Handle_network_message error: {:?}", e);
}
}

Expand Down
56 changes: 56 additions & 0 deletions sync/tests/full_sync_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod test_sync;
use config::{NodeConfig, SyncMode};
use futures::executor::block_on;
use logger::prelude::*;
use starcoin_service_registry::ActorService;
use starcoin_sync::sync2::SyncService2;
use std::sync::Arc;
use std::thread::sleep;
use std::time::Duration;
Expand All @@ -14,6 +16,60 @@ fn test_full_sync() {
test_sync::test_sync(SyncMode::FULL)
}

#[stest::test(timeout = 120)]
fn test_sync_by_notification() {
let first_config = Arc::new(NodeConfig::random_for_test());
info!(
"first peer : {:?}",
first_config.network.self_peer_id().unwrap()
);
let first_node = run_node_by_config(first_config.clone()).unwrap();
let first_chain = first_node.chain_service().unwrap();

let mut second_config = NodeConfig::random_for_test();
info!(
"second peer : {:?}",
second_config.network.self_peer_id().unwrap()
);
second_config.network.seeds = vec![first_config.network.self_address().unwrap()];
second_config.miner.enable_miner_client = false;

let second_node = run_node_by_config(Arc::new(second_config)).unwrap();
// stop sync service and just use notification message to sync.
second_node
.stop_service(SyncService2::service_name().to_string())
.unwrap();

let second_chain = second_node.chain_service().unwrap();

//wait second node sync service stop.
sleep(Duration::from_millis(500));

let count = 5;
for _i in 0..count {
first_node.generate_block().unwrap();
}

//wait block generate.
sleep(Duration::from_millis(500));
let block_1 = block_on(async { first_chain.main_head_block().await.unwrap() });
let number_1 = block_1.header().number();

let mut number_2 = 0;
for i in 0..10 as usize {
std::thread::sleep(Duration::from_secs(2));
let block_2 = block_on(async { second_chain.main_head_block().await.unwrap() });
number_2 = block_2.header().number();
debug!("index : {}, second chain number is {}", i, number_2);
if number_2 == number_1 {
break;
}
}
assert_eq!(number_1, number_2, "two node is not sync.");
second_node.stop().unwrap();
first_node.stop().unwrap();
}

//TODO fixme
#[ignore]
#[stest::test(timeout = 120)]
Expand Down

0 comments on commit 2a78f19

Please sign in to comment.