From 7ea1067decd2f6b164e92d80474c38fa7e47d122 Mon Sep 17 00:00:00 2001 From: Ignacio Duart Date: Thu, 9 Nov 2023 10:44:02 +0100 Subject: [PATCH] Actually guarantee random temp dirs (avoid fixed seed rng) --- crates/core/src/runtime/contract_store.rs | 4 +--- crates/core/src/runtime/delegate.rs | 10 ++++++---- crates/core/src/runtime/store.rs | 9 +++++---- crates/core/src/runtime/tests/mod.rs | 7 ++++--- crates/core/src/util.rs | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/crates/core/src/runtime/contract_store.rs b/crates/core/src/runtime/contract_store.rs index c79b1254a..4478e14db 100644 --- a/crates/core/src/runtime/contract_store.rs +++ b/crates/core/src/runtime/contract_store.rs @@ -215,13 +215,11 @@ impl ContractStore { #[cfg(test)] mod test { - use tempfile::TempDir; - use super::*; #[test] fn store_and_load() -> Result<(), Box> { - let contract_dir = TempDir::new()?; + let contract_dir = crate::util::tests_util::get_temp_dir(); std::fs::create_dir_all(contract_dir.path())?; let mut store = ContractStore::new(contract_dir.path().into(), 10_000)?; let contract = WrappedContract::new( diff --git a/crates/core/src/runtime/delegate.rs b/crates/core/src/runtime/delegate.rs index 06b3bc6b3..a61d37691 100644 --- a/crates/core/src/runtime/delegate.rs +++ b/crates/core/src/runtime/delegate.rs @@ -390,7 +390,8 @@ mod test { use freenet_stdlib::prelude::*; use serde::{Deserialize, Serialize}; use std::sync::Arc; - use tempfile::TempDir; + + use crate::util::tests_util::get_temp_dir; use super::super::{delegate_store::DelegateStore, ContractStore, SecretsStore}; use super::*; @@ -416,9 +417,9 @@ mod test { fn setup_runtime( name: &str, - ) -> Result<(DelegateContainer, Runtime, TempDir), Box> { + ) -> Result<(DelegateContainer, Runtime, tempfile::TempDir), Box> { // let _ = tracing_subscriber::fmt().with_env_filter("info").try_init(); - let temp_dir = TempDir::new().expect("Failed to create a temporary directory"); + let temp_dir = get_temp_dir(); let contracts_dir = temp_dir.path().join("contracts"); let delegates_dir = temp_dir.path().join("delegates"); let secrets_dir = temp_dir.path().join("secrets"); @@ -455,7 +456,7 @@ mod test { Arc::new(ContractCode::from(vec![1])), Parameters::from(vec![]), ); - let (delegate, mut runtime, _temp_dir) = setup_runtime(TEST_DELEGATE_1)?; + let (delegate, mut runtime, temp_dir) = setup_runtime(TEST_DELEGATE_1)?; let app = ContractInstanceId::try_from(contract.key.to_string()).unwrap(); // CreateInboxRequest message parts @@ -489,6 +490,7 @@ mod test { outbound.get(0), Some(OutboundDelegateMsg::ApplicationMessage(msg)) if *msg.payload == expected_payload )); + std::mem::drop(temp_dir); Ok(()) } } diff --git a/crates/core/src/runtime/store.rs b/crates/core/src/runtime/store.rs index ca0a7c967..195cf3aaa 100644 --- a/crates/core/src/runtime/store.rs +++ b/crates/core/src/runtime/store.rs @@ -337,9 +337,10 @@ mod tests { sync::{Arc, Barrier}, }; + use crate::util::tests_util::get_temp_dir; + use super::*; use dashmap::DashMap; - use tempfile::TempDir; struct TestStore1; @@ -375,7 +376,7 @@ mod tests { #[test] fn test_store() { - let temp_dir = TempDir::new().expect("Failed to create a temporary directory"); + let temp_dir = crate::util::tests_util::get_temp_dir(); let contract_keys_file_path = temp_dir.path().join("contract_keys"); let delegate_keys_file_path = temp_dir.path().join("delegate_keys"); @@ -453,7 +454,7 @@ mod tests { fn test_concurrent_updates() { const NUM_THREADS: usize = 4; - let temp_dir = TempDir::new().expect("Failed to create a temporary directory"); + let temp_dir = get_temp_dir(); let contract_keys_file_path = temp_dir.path().join("contract_keys"); std::fs::File::create(&contract_keys_file_path).expect("Failed to create file"); @@ -513,7 +514,7 @@ mod tests { #[test] fn test_concurrent_compaction() { - let temp_dir = TempDir::new().expect("Failed to create temp dir"); + let temp_dir = get_temp_dir(); let key_file_path = temp_dir.path().join("data.dat"); std::fs::File::create(&key_file_path).expect("Failed to create file"); diff --git a/crates/core/src/runtime/tests/mod.rs b/crates/core/src/runtime/tests/mod.rs index 274f97b90..3cebbd8cc 100644 --- a/crates/core/src/runtime/tests/mod.rs +++ b/crates/core/src/runtime/tests/mod.rs @@ -7,7 +7,8 @@ use std::{ use freenet_stdlib::prelude::{ ContractCode, ContractContainer, ContractKey, ContractWasmAPIVersion, WrappedContract, }; -use tempfile::TempDir; + +use crate::util::tests_util::get_temp_dir; use super::{ContractStore, DelegateStore, SecretsStore}; @@ -49,7 +50,7 @@ pub(crate) fn get_test_module(name: &str) -> Result, Box Result> { // let _ = tracing_subscriber::fmt().with_env_filter("info").try_init(); - let temp_dir = TempDir::new().expect("Failed to create a temporary directory"); + let temp_dir = get_temp_dir(); let mut contract_store = ContractStore::new(temp_dir.path().join("contract"), 10_000)?; let delegate_store = DelegateStore::new(temp_dir.path().join("delegate"), 10_000)?; diff --git a/crates/core/src/util.rs b/crates/core/src/util.rs index 410d2ea4f..5fe45fd2c 100644 --- a/crates/core/src/util.rs +++ b/crates/core/src/util.rs @@ -236,3 +236,21 @@ impl<'x> Contains for &'x [&PeerKey] { self.contains(&target) } } + +#[cfg(test)] +pub mod tests_util { + use rand::Rng; + use tempfile::TempDir; + + /// Use this to guarantee unique directory names in case you are running multiple tests in parallel. + pub fn get_temp_dir() -> TempDir { + tempfile::Builder::new() + .suffix( + &(0..8) + .map(|_| rand::thread_rng().gen::()) + .collect::(), + ) + .tempdir() + .expect("Failed to create a temporary directory") + } +}