From fcf9366eb89bf9ef10acd50d37f8f1060029099a Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Thu, 4 Feb 2016 21:07:52 +0300 Subject: [PATCH 1/3] local coverage for sync --- sync/cov.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 sync/cov.sh diff --git a/sync/cov.sh b/sync/cov.sh new file mode 100755 index 000000000..2f8c594ff --- /dev/null +++ b/sync/cov.sh @@ -0,0 +1,9 @@ +if ! type kcov > /dev/null; then + echo "Install kcov first (details inside this file). Aborting." + exit 1 +fi + +cargo test --no-run || exit $? +mkdir -p target/coverage +kcov --exclude-pattern ~/.multirust,rocksdb,secp256k1 --include-pattern src --verify target/coverage target/debug/ethsync* +xdg-open target/coverage/index.html From e27081fe668b3ad5f2c8274789d91cd08bf7ba2f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Thu, 4 Feb 2016 21:09:32 +0300 Subject: [PATCH 2/3] filter out util/core --- sync/cov.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync/cov.sh b/sync/cov.sh index 2f8c594ff..c19dd5689 100755 --- a/sync/cov.sh +++ b/sync/cov.sh @@ -5,5 +5,5 @@ fi cargo test --no-run || exit $? mkdir -p target/coverage -kcov --exclude-pattern ~/.multirust,rocksdb,secp256k1 --include-pattern src --verify target/coverage target/debug/ethsync* +kcov --exclude-pattern ~/.multirust,rocksdb,secp256k1 --include-pattern sync/src --verify target/coverage target/debug/ethsync* xdg-open target/coverage/index.html From c0776b75b68500427d310d8407e09b61f34bfefe Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Thu, 4 Feb 2016 21:30:31 +0300 Subject: [PATCH 3/3] moving sources for proper test setup --- sync/src/chain.rs | 4 + sync/src/lib.rs | 4 +- sync/src/tests/chain.rs | 91 +++++++++++++++++ sync/src/{tests.rs => tests/helpers.rs} | 130 ++++-------------------- sync/src/tests/mod.rs | 2 + 5 files changed, 120 insertions(+), 111 deletions(-) create mode 100644 sync/src/tests/chain.rs rename sync/src/{tests.rs => tests/helpers.rs} (71%) create mode 100644 sync/src/tests/mod.rs diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 665508d92..40cbd496b 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -1013,3 +1013,7 @@ impl ChainSync { } } } + +#[cfg(test)] +mod tests { +} \ No newline at end of file diff --git a/sync/src/lib.rs b/sync/src/lib.rs index f3b43396c..1523a8a9f 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -109,6 +109,4 @@ impl NetworkProtocolHandler for EthSync { self.sync.write().unwrap().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref())); self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref())); } -} - - +} \ No newline at end of file diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs new file mode 100644 index 000000000..083a9bd33 --- /dev/null +++ b/sync/src/tests/chain.rs @@ -0,0 +1,91 @@ +use util::*; +use ethcore::client::{BlockChainClient}; +use io::SyncIo; +use chain::{SyncState}; +use super::helpers::*; + +#[test] +fn chain_two_peers() { + ::env_logger::init().ok(); + let mut net = TestNet::new(3); + net.peer_mut(1).chain.add_blocks(1000, false); + net.peer_mut(2).chain.add_blocks(1000, false); + net.sync(); + assert!(net.peer(0).chain.block_at(1000).is_some()); + assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); +} + +#[test] +fn chain_status_after_sync() { + ::env_logger::init().ok(); + let mut net = TestNet::new(3); + net.peer_mut(1).chain.add_blocks(1000, false); + net.peer_mut(2).chain.add_blocks(1000, false); + net.sync(); + let status = net.peer(0).sync.status(); + assert_eq!(status.state, SyncState::Idle); +} + +#[test] +fn chain_takes_few_steps() { + let mut net = TestNet::new(3); + net.peer_mut(1).chain.add_blocks(100, false); + net.peer_mut(2).chain.add_blocks(100, false); + let total_steps = net.sync(); + assert!(total_steps < 7); +} + +#[test] +fn chain_empty_blocks() { + ::env_logger::init().ok(); + let mut net = TestNet::new(3); + for n in 0..200 { + net.peer_mut(1).chain.add_blocks(5, n % 2 == 0); + net.peer_mut(2).chain.add_blocks(5, n % 2 == 0); + } + net.sync(); + assert!(net.peer(0).chain.block_at(1000).is_some()); + assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); +} + +#[test] +fn chain_forked() { + ::env_logger::init().ok(); + let mut net = TestNet::new(3); + net.peer_mut(0).chain.add_blocks(300, false); + net.peer_mut(1).chain.add_blocks(300, false); + net.peer_mut(2).chain.add_blocks(300, false); + net.peer_mut(0).chain.add_blocks(100, true); //fork + net.peer_mut(1).chain.add_blocks(200, false); + net.peer_mut(2).chain.add_blocks(200, false); + net.peer_mut(1).chain.add_blocks(100, false); //fork between 1 and 2 + net.peer_mut(2).chain.add_blocks(10, true); + // peer 1 has the best chain of 601 blocks + let peer1_chain = net.peer(1).chain.numbers.read().unwrap().clone(); + net.sync(); + assert_eq!(net.peer(0).chain.numbers.read().unwrap().deref(), &peer1_chain); + assert_eq!(net.peer(1).chain.numbers.read().unwrap().deref(), &peer1_chain); + assert_eq!(net.peer(2).chain.numbers.read().unwrap().deref(), &peer1_chain); +} + +#[test] +fn chain_restart() { + let mut net = TestNet::new(3); + net.peer_mut(1).chain.add_blocks(1000, false); + net.peer_mut(2).chain.add_blocks(1000, false); + + net.sync_steps(8); + + // make sure that sync has actually happened + assert!(net.peer(0).chain.chain_info().best_block_number > 100); + net.restart_peer(0); + + let status = net.peer(0).sync.status(); + assert_eq!(status.state, SyncState::NotSynced); +} + +#[test] +fn chain_status_empty() { + let net = TestNet::new(2); + assert_eq!(net.peer(0).sync.status().state, SyncState::NotSynced); +} \ No newline at end of file diff --git a/sync/src/tests.rs b/sync/src/tests/helpers.rs similarity index 71% rename from sync/src/tests.rs rename to sync/src/tests/helpers.rs index 88f19a8b6..7fdd695fb 100644 --- a/sync/src/tests.rs +++ b/sync/src/tests/helpers.rs @@ -4,14 +4,14 @@ use ethcore::block_queue::BlockQueueInfo; use ethcore::header::{Header as BlockHeader, BlockNumber}; use ethcore::error::*; use io::SyncIo; -use chain::{ChainSync, SyncState}; +use chain::{ChainSync}; -struct TestBlockChainClient { - blocks: RwLock>, - numbers: RwLock>, - genesis_hash: H256, - last_hash: RwLock, - difficulty: RwLock, +pub struct TestBlockChainClient { + pub blocks: RwLock>, + pub numbers: RwLock>, + pub genesis_hash: H256, + pub last_hash: RwLock, + pub difficulty: RwLock, } impl TestBlockChainClient { @@ -189,10 +189,10 @@ impl BlockChainClient for TestBlockChainClient { } } -struct TestIo<'p> { - chain: &'p mut TestBlockChainClient, - queue: &'p mut VecDeque, - sender: Option, +pub struct TestIo<'p> { + pub chain: &'p mut TestBlockChainClient, + pub queue: &'p mut VecDeque, + pub sender: Option, } impl<'p> TestIo<'p> { @@ -235,21 +235,21 @@ impl<'p> SyncIo for TestIo<'p> { } } -struct TestPacket { - data: Bytes, - packet_id: PacketId, - recipient: PeerId, +pub struct TestPacket { + pub data: Bytes, + pub packet_id: PacketId, + pub recipient: PeerId, } -struct TestPeer { - chain: TestBlockChainClient, - sync: ChainSync, - queue: VecDeque, +pub struct TestPeer { + pub chain: TestBlockChainClient, + pub sync: ChainSync, + pub queue: VecDeque, } -struct TestNet { - peers: Vec, - started: bool, +pub struct TestNet { + pub peers: Vec, + pub started: bool, } impl TestNet { @@ -329,89 +329,3 @@ impl TestNet { self.peers.iter().all(|p| p.queue.is_empty()) } } - -#[test] -fn chain_two_peers() { - ::env_logger::init().ok(); - let mut net = TestNet::new(3); - net.peer_mut(1).chain.add_blocks(1000, false); - net.peer_mut(2).chain.add_blocks(1000, false); - net.sync(); - assert!(net.peer(0).chain.block_at(1000).is_some()); - assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); -} - -#[test] -fn chain_status_after_sync() { - ::env_logger::init().ok(); - let mut net = TestNet::new(3); - net.peer_mut(1).chain.add_blocks(1000, false); - net.peer_mut(2).chain.add_blocks(1000, false); - net.sync(); - let status = net.peer(0).sync.status(); - assert_eq!(status.state, SyncState::Idle); -} - -#[test] -fn chain_takes_few_steps() { - let mut net = TestNet::new(3); - net.peer_mut(1).chain.add_blocks(100, false); - net.peer_mut(2).chain.add_blocks(100, false); - let total_steps = net.sync(); - assert!(total_steps < 7); -} - -#[test] -fn chain_empty_blocks() { - ::env_logger::init().ok(); - let mut net = TestNet::new(3); - for n in 0..200 { - net.peer_mut(1).chain.add_blocks(5, n % 2 == 0); - net.peer_mut(2).chain.add_blocks(5, n % 2 == 0); - } - net.sync(); - assert!(net.peer(0).chain.block_at(1000).is_some()); - assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); -} - -#[test] -fn chain_forked() { - ::env_logger::init().ok(); - let mut net = TestNet::new(3); - net.peer_mut(0).chain.add_blocks(300, false); - net.peer_mut(1).chain.add_blocks(300, false); - net.peer_mut(2).chain.add_blocks(300, false); - net.peer_mut(0).chain.add_blocks(100, true); //fork - net.peer_mut(1).chain.add_blocks(200, false); - net.peer_mut(2).chain.add_blocks(200, false); - net.peer_mut(1).chain.add_blocks(100, false); //fork between 1 and 2 - net.peer_mut(2).chain.add_blocks(10, true); - // peer 1 has the best chain of 601 blocks - let peer1_chain = net.peer(1).chain.numbers.read().unwrap().clone(); - net.sync(); - assert_eq!(net.peer(0).chain.numbers.read().unwrap().deref(), &peer1_chain); - assert_eq!(net.peer(1).chain.numbers.read().unwrap().deref(), &peer1_chain); - assert_eq!(net.peer(2).chain.numbers.read().unwrap().deref(), &peer1_chain); -} - -#[test] -fn chain_restart() { - let mut net = TestNet::new(3); - net.peer_mut(1).chain.add_blocks(1000, false); - net.peer_mut(2).chain.add_blocks(1000, false); - - net.sync_steps(8); - - // make sure that sync has actually happened - assert!(net.peer(0).chain.chain_info().best_block_number > 100); - net.restart_peer(0); - - let status = net.peer(0).sync.status(); - assert_eq!(status.state, SyncState::NotSynced); -} - -#[test] -fn chain_status_empty() { - let net = TestNet::new(2); - assert_eq!(net.peer(0).sync.status().state, SyncState::NotSynced); -} \ No newline at end of file diff --git a/sync/src/tests/mod.rs b/sync/src/tests/mod.rs new file mode 100644 index 000000000..a5fa44b04 --- /dev/null +++ b/sync/src/tests/mod.rs @@ -0,0 +1,2 @@ +pub mod helpers; +mod chain; \ No newline at end of file