Skip to content

Commit

Permalink
Actually guarantee random temp dirs (avoid fixed seed rng)
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez committed Nov 9, 2023
1 parent dd8b7be commit 7ea1067
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
4 changes: 1 addition & 3 deletions crates/core/src/runtime/contract_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,11 @@ impl ContractStore {

#[cfg(test)]
mod test {
use tempfile::TempDir;

use super::*;

#[test]
fn store_and_load() -> Result<(), Box<dyn std::error::Error>> {
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(
Expand Down
10 changes: 6 additions & 4 deletions crates/core/src/runtime/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -416,9 +417,9 @@ mod test {

fn setup_runtime(
name: &str,
) -> Result<(DelegateContainer, Runtime, TempDir), Box<dyn std::error::Error>> {
) -> Result<(DelegateContainer, Runtime, tempfile::TempDir), Box<dyn std::error::Error>> {
// 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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -489,6 +490,7 @@ mod test {
outbound.get(0),
Some(OutboundDelegateMsg::ApplicationMessage(msg)) if *msg.payload == expected_payload
));
std::mem::drop(temp_dir);
Ok(())
}
}
9 changes: 5 additions & 4 deletions crates/core/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down
7 changes: 4 additions & 3 deletions crates/core/src/runtime/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -49,7 +50,7 @@ pub(crate) fn get_test_module(name: &str) -> Result<Vec<u8>, Box<dyn std::error:

pub(crate) struct TestSetup {
#[allow(unused)]
temp_dir: TempDir,
temp_dir: tempfile::TempDir,
contract_store: ContractStore,
delegate_store: DelegateStore,
secrets_store: SecretsStore,
Expand All @@ -58,7 +59,7 @@ pub(crate) struct TestSetup {

pub(crate) fn setup_test_contract(name: &str) -> Result<TestSetup, Box<dyn std::error::Error>> {
// 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)?;
Expand Down
18 changes: 18 additions & 0 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,21 @@ impl<'x> Contains<PeerKey> 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::<char>())
.collect::<String>(),
)
.tempdir()
.expect("Failed to create a temporary directory")
}
}

0 comments on commit 7ea1067

Please sign in to comment.