From dddbf589451a61daeecd122d4e83e6ae3b9e3165 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 14:50:48 +0400 Subject: [PATCH] test helpers initial --- src/tests/client.rs | 23 +++++-------------- src/tests/helpers.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/tests/mod.rs | 1 + 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index eb58699b4..57ab6e577 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -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) -> Arc { - 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) -> Arc { #[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()); diff --git a/src/tests/helpers.rs b/src/tests/helpers.rs index e69de29bb..4cbd36692 100644 --- a/src/tests/helpers.rs +++ b/src/tests/helpers.rs @@ -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; + } + +} \ No newline at end of file diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 290c2e293..17da72d77 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,3 +6,4 @@ mod executive; mod state; mod client; mod chain; +mod helpers; \ No newline at end of file