Skip to content

Commit

Permalink
Make the mock transaction less expensive (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored Aug 19, 2022
1 parent c6eea92 commit cf01950
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions eng/test/mock_transport/src/mock_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use azure_core::error::{Error, ErrorKind, ResultExt};
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::{Arc, Mutex};

#[derive(Debug, Clone)]
pub(crate) struct MockTransaction {
pub(crate) name: String,
pub(crate) number: Arc<AtomicUsize>,
workspace_root: Arc<Mutex<Option<String>>>,
}

impl MockTransaction {
pub(crate) fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
number: Arc::new(AtomicUsize::new(0)),
workspace_root: Arc::new(Mutex::new(None)),
}
}

Expand All @@ -30,10 +32,21 @@ impl MockTransaction {
}

pub(crate) fn file_path(&self, create_when_not_exist: bool) -> azure_core::Result<PathBuf> {
let mut path = PathBuf::from(workspace_root().context(
ErrorKind::MockFramework,
"could not read the workspace_root from the cargo metadata",
)?);
let root_path = {
let mut cache = self.workspace_root.lock().unwrap();
match &*cache {
Some(root) => root.clone(),
None => {
let root = workspace_root().context(
ErrorKind::MockFramework,
"could not read the workspace_root from the cargo metadata",
)?;
*cache = Some(root.clone());
root
}
}
};
let mut path = PathBuf::from(root_path);
path.push("test");
path.push("transactions");
let name = self.name();
Expand Down

0 comments on commit cf01950

Please sign in to comment.