From 32cc6282d30cc6fbc73eb22f32c27f177450f457 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 28 May 2019 15:06:13 +0200 Subject: [PATCH] Fixed tests concurrency --- src/config/cli.rs | 4 ++-- src/config/mod.rs | 39 ++++++++++++++++++++++++------------ src/services/audio_folder.rs | 4 ++-- src/services/search.rs | 2 +- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/config/cli.rs b/src/config/cli.rs index 23ced62c..190aa9cd 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -265,7 +265,7 @@ where { let d = base_data_dir(); if !d.is_dir() { - std::fs::create_dir(d).or_else(|e| { + std::fs::create_dir(&d).or_else(|e| { arg_error!( "data-dir", "Audioserve data directory {:?} cannot be created due to error {}", @@ -456,7 +456,7 @@ where #[cfg(test)] mod test { use super::*; - + use crate::config::init::init_default_config; #[test] fn test_basic_args() { init_default_config(); diff --git a/src/config/mod.rs b/src/config/mod.rs index 63b4cc77..b5c3a0b2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -413,26 +413,39 @@ pub fn init_config() -> Result<()> { Ok(()) } -//this default config is used only for testing -#[allow(dead_code)] -pub fn init_default_config() { - unsafe { - if CONFIG.is_some() { - return; - } - - BASE_DATA_DIR = Some(dirs::home_dir().unwrap_or_else(|| PathBuf::from("."))); +#[cfg(test)] +pub mod init { + /// Static config initialization for tests + /// as tests are run concurrently it requires also some synchronication + use super::{BASE_DATA_DIR, CONFIG, Config}; + use std::path::PathBuf; + use std::sync::Mutex; + lazy_static!{ + static ref GUARD:Mutex = Mutex::new(false); } - let config = Config::default(); - unsafe { - CONFIG = Some(config); + /// this default config is used only for testing + pub fn init_default_config() { + let mut l = GUARD.lock().unwrap(); + unsafe { + if BASE_DATA_DIR.is_some() { return } + let base_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); + BASE_DATA_DIR = Some(base_dir); + } + + let config = Config::default(); + unsafe { + CONFIG = Some(config); + } + *l = true } + } + #[cfg(test)] mod tests { use super::*; - + use crate::config::init::init_default_config; #[test] fn test_default_serialize() { init_default_config(); diff --git a/src/services/audio_folder.rs b/src/services/audio_folder.rs index 0852629d..d9c74ab8 100644 --- a/src/services/audio_folder.rs +++ b/src/services/audio_folder.rs @@ -451,7 +451,7 @@ pub fn get_real_file_type>( #[cfg(test)] mod tests { use super::*; - use crate::config::init_default_config; + use crate::config::init::init_default_config; use serde_json; #[test] @@ -518,7 +518,7 @@ mod tests { #[test] fn test_chapters_file() { - pretty_env_logger::init(); + //pretty_env_logger::init(); let path = Path::new("./test_data/01-file.mp3"); let chapters = chapters_from_csv(path).unwrap().unwrap(); assert_eq!(3, chapters.len()); diff --git a/src/services/search.rs b/src/services/search.rs index b1b02a83..56d42082 100644 --- a/src/services/search.rs +++ b/src/services/search.rs @@ -298,7 +298,7 @@ mod cache { #[cfg(test)] mod tests { use super::*; - use crate::config::init_default_config; + use crate::config::init::init_default_config; const TEST_DATA_DIR: &str = "./test_data";