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> {
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 {
if let Err(_) = client.import_block(block.clone()) {
panic!("panic importing block which is well-formed");
@ -46,20 +47,23 @@ fn get_test_client_with_blocks(blocks: Vec<Bytes>) -> Arc<Client> {
#[test]
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());
}
#[test]
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.flush_queue();
}
#[test]
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();
if let Err(_) = client.import_block(good_block) {
panic!("error importing block being good by definition");
@ -73,7 +77,8 @@ fn imports_good_block() {
#[test]
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);
assert!(non_existant.is_none());

View File

@ -3,25 +3,33 @@ use std::env;
use super::test_common::*;
use std::path::PathBuf;
use spec::*;
use std::fs::{create_dir_all};
use std::fs::{remove_dir_all};
const FIXED_TEMP_DIR_NAME: &'static str = "parity-temp";
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 struct RandomTempPath {
path: PathBuf
}
pub fn get_random_path() -> PathBuf {
let mut dir = get_tests_temp_dir();
dir.push(H32::random().hex());
dir
impl RandomTempPath {
pub fn new() -> RandomTempPath {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
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> {
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_engine = test_spec.to_engine().unwrap();