test helpers initial

This commit is contained in:
Nikolay Volf 2016-01-27 14:50:48 +04:00
parent d2229f519d
commit dddbf58945
3 changed files with 61 additions and 17 deletions

View File

@ -3,18 +3,7 @@ use std::env;
use super::test_common::*;
use std::path::PathBuf;
use spec::*;
#[cfg(test)]
fn get_random_temp_dir() -> PathBuf {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
dir
}
#[cfg(test)]
fn get_test_spec() -> Spec {
Spec::new_test()
}
use super::helpers::*;
#[cfg(test)]
fn get_good_dummy_block() -> Bytes {
@ -57,7 +46,7 @@ fn create_test_block(header: &Header) -> Bytes {
#[cfg(test)]
fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> {
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap();
for block in &blocks {
if let Err(_) = client.import_block(block.clone()) {
panic!("panic importing block which is well-formed");
@ -71,20 +60,20 @@ fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> {
#[test]
fn created() {
let client_result = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected());
let client_result = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected());
assert!(client_result.is_ok());
}
#[test]
fn imports_from_empty() {
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap();
client.import_verified_blocks(&IoChannel::disconnected());
client.flush_queue();
}
#[test]
fn imports_good_block() {
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap();
let good_block = get_good_dummy_block();
if let Err(_) = client.import_block(good_block) {
panic!("error importing block being good by definition");
@ -98,7 +87,7 @@ fn imports_good_block() {
#[test]
fn query_none_block() {
let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap();
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap();
let non_existant = client.block_header_at(188);
assert!(non_existant.is_none());

View File

@ -0,0 +1,54 @@
use client::{BlockChainClient,Client};
use std::env;
use super::test_common::*;
use std::path::PathBuf;
use spec::*;
use std::fs::{create_dir_all};
#[cfg(test)]
const FIXED_TEMP_DIR_NAME: &'static str = "parity-temp";
#[cfg(test)]
pub fn get_tests_temp_dir() -> PathBuf {
let mut dir = env::temp_dir();
dir.push(FIXED_TEMP_DIR_NAME);
if let Err(_) = create_dir_all(&dir) {
panic!("failed to create test dir!");
}
dir
}
#[cfg(test)]
pub fn get_random_path() -> PathBuf {
let mut dir = get_tests_temp_dir();
dir.push(H32::random().hex());
dir
}
#[cfg(test)]
pub fn get_test_spec() -> Spec {
Spec::new_test()
}
#[cfg(test)]
pub fn generate_dummy_client(block_number: usize) {
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap();
let mut rolling_hash = test_engine.spec().genesis_header().hash();
let mut rolling_state = test_engine.spec().genesis_header().state_root;
let mut rolling_block_number = 1;
for _ in 0..block_number {
let mut header = Header::new();
header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap());
header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap());
header.timestamp = 40;
header.number = rolling_block_number;
header.parent_hash = test_engine.spec().genesis_header().hash();
header.state_root = test_engine.spec().genesis_header().state_root;
}
}

View File

@ -6,3 +6,4 @@ mod executive;
mod state;
mod client;
mod chain;
mod helpers;