Merge pull request #348 from ethcore/sync-tests-i3
sync tests setup & local module coverage
This commit is contained in:
commit
4af8adc89a
9
sync/cov.sh
Executable file
9
sync/cov.sh
Executable file
@ -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 sync/src --verify target/coverage target/debug/ethsync*
|
||||||
|
xdg-open target/coverage/index.html
|
@ -1013,3 +1013,7 @@ impl ChainSync {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
}
|
@ -109,6 +109,4 @@ impl NetworkProtocolHandler<SyncMessage> for EthSync {
|
|||||||
self.sync.write().unwrap().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref()));
|
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()));
|
self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
91
sync/src/tests/chain.rs
Normal file
91
sync/src/tests/chain.rs
Normal file
@ -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);
|
||||||
|
}
|
@ -4,14 +4,14 @@ use ethcore::block_queue::BlockQueueInfo;
|
|||||||
use ethcore::header::{Header as BlockHeader, BlockNumber};
|
use ethcore::header::{Header as BlockHeader, BlockNumber};
|
||||||
use ethcore::error::*;
|
use ethcore::error::*;
|
||||||
use io::SyncIo;
|
use io::SyncIo;
|
||||||
use chain::{ChainSync, SyncState};
|
use chain::{ChainSync};
|
||||||
|
|
||||||
struct TestBlockChainClient {
|
pub struct TestBlockChainClient {
|
||||||
blocks: RwLock<HashMap<H256, Bytes>>,
|
pub blocks: RwLock<HashMap<H256, Bytes>>,
|
||||||
numbers: RwLock<HashMap<usize, H256>>,
|
pub numbers: RwLock<HashMap<usize, H256>>,
|
||||||
genesis_hash: H256,
|
pub genesis_hash: H256,
|
||||||
last_hash: RwLock<H256>,
|
pub last_hash: RwLock<H256>,
|
||||||
difficulty: RwLock<U256>,
|
pub difficulty: RwLock<U256>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestBlockChainClient {
|
impl TestBlockChainClient {
|
||||||
@ -189,10 +189,10 @@ impl BlockChainClient for TestBlockChainClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TestIo<'p> {
|
pub struct TestIo<'p> {
|
||||||
chain: &'p mut TestBlockChainClient,
|
pub chain: &'p mut TestBlockChainClient,
|
||||||
queue: &'p mut VecDeque<TestPacket>,
|
pub queue: &'p mut VecDeque<TestPacket>,
|
||||||
sender: Option<PeerId>,
|
pub sender: Option<PeerId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'p> TestIo<'p> {
|
impl<'p> TestIo<'p> {
|
||||||
@ -235,21 +235,21 @@ impl<'p> SyncIo for TestIo<'p> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TestPacket {
|
pub struct TestPacket {
|
||||||
data: Bytes,
|
pub data: Bytes,
|
||||||
packet_id: PacketId,
|
pub packet_id: PacketId,
|
||||||
recipient: PeerId,
|
pub recipient: PeerId,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TestPeer {
|
pub struct TestPeer {
|
||||||
chain: TestBlockChainClient,
|
pub chain: TestBlockChainClient,
|
||||||
sync: ChainSync,
|
pub sync: ChainSync,
|
||||||
queue: VecDeque<TestPacket>,
|
pub queue: VecDeque<TestPacket>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TestNet {
|
pub struct TestNet {
|
||||||
peers: Vec<TestPeer>,
|
pub peers: Vec<TestPeer>,
|
||||||
started: bool,
|
pub started: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestNet {
|
impl TestNet {
|
||||||
@ -329,89 +329,3 @@ impl TestNet {
|
|||||||
self.peers.iter().all(|p| p.queue.is_empty())
|
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);
|
|
||||||
}
|
|
2
sync/src/tests/mod.rs
Normal file
2
sync/src/tests/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod helpers;
|
||||||
|
mod chain;
|
Loading…
Reference in New Issue
Block a user