From 9d23915caf2d9c125e0148ad282b3205e6975647 Mon Sep 17 00:00:00 2001 From: keorn Date: Tue, 20 Sep 2016 15:48:17 +0200 Subject: [PATCH] more simulation methods --- sync/src/tests/consensus.rs | 34 ++++++++++++++++------------------ sync/src/tests/mocknet.rs | 20 +++++++++++++++++++- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/sync/src/tests/consensus.rs b/sync/src/tests/consensus.rs index cafade39c..55b0bb0f7 100644 --- a/sync/src/tests/consensus.rs +++ b/sync/src/tests/consensus.rs @@ -22,15 +22,12 @@ use std::time::Duration; use ethcore::client::BlockChainClient; #[test] -fn 2_peer_1_tx_seal() { +fn two_peer_tx_seal() { ::env_logger::init().ok(); let mut net = MockNet::new_with_spec(2, vec!["1".sha3()], &Spec::new_test_round); net.peer(1).issue_rand_tx(); sleep(Duration::from_secs(1)); net.sync(); - net.sync(); - net.sync(); - net.sync(); println!("{:?}", net.peer(0).client.chain_info()); println!("{:?}", net.peer(1).client.chain_info()); } @@ -39,21 +36,22 @@ fn 2_peer_1_tx_seal() { fn issue_many() { ::env_logger::init().ok(); let mut net = MockNet::new_with_spec(2, vec!["1".sha3()], &Spec::new_test_round); - net.peer(1).issue_rand_tx(); - net.peer(1).issue_rand_tx(); - net.peer(1).issue_rand_tx(); - net.peer(1).issue_rand_tx(); - net.peer(1).issue_rand_tx(); + net.peer(1).issue_rand_txs(5); sleep(Duration::from_secs(1)); net.sync(); + net.peer(0).issue_rand_txs(5); net.sync(); - net.peer(0).issue_rand_tx(); - net.peer(0).issue_rand_tx(); - net.peer(0).issue_rand_tx(); - net.peer(0).issue_rand_tx(); - net.peer(0).issue_rand_tx(); - net.sync(); - net.sync(); - //println!("{:?}", net.peer(0).client.chain_info()); - //println!("{:?}", net.peer(1).client.chain_info()); + println!("{:?}", net.peer(0).client.chain_info()); + println!("{:?}", net.peer(1).client.chain_info()); +} + +#[test] +fn rand_simulation() { + ::env_logger::init().ok(); + let mut net = MockNet::new_with_spec(2, vec!["1".sha3()], &Spec::new_test_round); + + net.rand_simulation(10); + + println!("{:?}", net.peer(0).client.chain_info()); + println!("{:?}", net.peer(1).client.chain_info()); } diff --git a/sync/src/tests/mocknet.rs b/sync/src/tests/mocknet.rs index 1091e868f..088d55fbe 100644 --- a/sync/src/tests/mocknet.rs +++ b/sync/src/tests/mocknet.rs @@ -31,6 +31,7 @@ use ethcore::miner::Miner; use ethcore::service::ClientService; use std::time::Duration; use std::thread::sleep; +use rand::{thread_rng, Rng}; pub struct TestIo<'p> { pub client: Arc, @@ -196,6 +197,12 @@ impl MockPeer { pub fn issue_rand_tx(&self) { self.issue_tx(random_transaction()) } + + pub fn issue_rand_txs(&self, n: usize) { + for _ in 0..n { + self.issue_rand_tx(); + } + } } pub struct MockNet { @@ -264,7 +271,6 @@ impl MockNet { let mut io = TestIo::new(peer0.client.clone(), &peer0.snapshot_service, &mut *q0, None); p.sync.write().maintain_sync(&mut io); p.sync.write().propagate_new_transactions(&mut io); - sleep(Duration::from_secs(2)); } } @@ -303,4 +309,16 @@ impl MockNet { pub fn done(&self) -> bool { self.peers.iter().all(|p| p.queue.try_read().unwrap().is_empty()) } + + pub fn rand_peer(&self) -> Arc { + thread_rng().choose(&self.peers).unwrap().clone() + } + + pub fn rand_simulation(&mut self, steps: usize) { + for _ in 0..steps { + self.rand_peer().issue_rand_tx(); + sleep(Duration::from_millis(100)); + self.sync(); + } + } }