added temp directory struct with drop

This commit is contained in:
Nikolay Volf 2016-01-27 17:21:54 +04:00
parent a8c25744e3
commit d2cd6f69cb
2 changed files with 36 additions and 21 deletions

View File

@ -32,7 +32,8 @@ fn get_bad_state_dummy_block() -> Bytes {
fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> { fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> {
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
for block in &blocks { for block in &blocks {
if let Err(_) = client.import_block(block.clone()) { if let Err(_) = client.import_block(block.clone()) {
panic!("panic importing block which is well-formed"); panic!("panic importing block which is well-formed");
@ -46,20 +47,23 @@ fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> {
#[test] #[test]
fn created() { fn created() {
let client_result = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()); let dir = RandomTempPath::new();
let client_result = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected());
assert!(client_result.is_ok()); assert!(client_result.is_ok());
} }
#[test] #[test]
fn imports_from_empty() { fn imports_from_empty() {
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
client.import_verified_blocks(&IoChannel::disconnected()); client.import_verified_blocks(&IoChannel::disconnected());
client.flush_queue(); client.flush_queue();
} }
#[test] #[test]
fn imports_good_block() { fn imports_good_block() {
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
let good_block = get_good_dummy_block(); let good_block = get_good_dummy_block();
if let Err(_) = client.import_block(good_block) { if let Err(_) = client.import_block(good_block) {
panic!("error importing block being good by definition"); panic!("error importing block being good by definition");
@ -73,7 +77,8 @@ fn imports_good_block() {
#[test] #[test]
fn query_none_block() { fn query_none_block() {
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
let non_existant = client.block_header_at(188); let non_existant = client.block_header_at(188);
assert!(non_existant.is_none()); assert!(non_existant.is_none());

View File

@ -3,25 +3,33 @@ use std::env;
use super::test_common::*; use super::test_common::*;
use std::path::PathBuf; use std::path::PathBuf;
use spec::*; use spec::*;
use std::fs::{create_dir_all}; use std::fs::{remove_dir_all};
const FIXED_TEMP_DIR_NAME: &'static str = "parity-temp"; pub struct RandomTempPath {
path: PathBuf
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
} }
pub fn get_random_path() -> PathBuf { impl RandomTempPath {
let mut dir = get_tests_temp_dir(); pub fn new() -> RandomTempPath {
let mut dir = env::temp_dir();
dir.push(H32::random().hex()); dir.push(H32::random().hex());
dir RandomTempPath {
path: dir.clone()
}
}
pub fn as_path(&self) -> &PathBuf {
&self.path
}
}
impl Drop for RandomTempPath {
fn drop(&mut self) {
if let Err(e) = remove_dir_all(self.as_path()) {
panic!("failed to remove temp directory, probably something failed to destroyed ({})", e);
}
}
} }
@ -39,7 +47,9 @@ pub fn create_test_block(header: &Header) -> Bytes {
} }
pub fn generate_dummy_client(block_number: usize) -> Arc<Client> { pub fn generate_dummy_client(block_number: usize) -> Arc<Client> {
let client = Client::new(get_test_spec(), &get_random_path(), IoChannel::disconnected()).unwrap(); let dir = RandomTempPath::new();
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
let test_spec = get_test_spec(); let test_spec = get_test_spec();
let test_engine = test_spec.to_engine().unwrap(); let test_engine = test_spec.to_engine().unwrap();