Skip to content

Commit

Permalink
Cache parent process when testing unless FakeParentArgs are used
Browse files Browse the repository at this point in the history
  • Loading branch information
th1000s committed Jan 3, 2022
1 parent 225af06 commit 91da24d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/utils/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ pub fn calling_process() -> MutexGuard<'static, CallingProcess> {
#[cfg(test)]
pub fn calling_process() -> Box<CallingProcess> {
type _UnusedImport = MutexGuard<'static, i8>;
Box::new(determine_calling_process())

if crate::utils::process::tests::FakeParentArgs::are_set() {
// If the (thread-local) FakeParentArgs are set, then the following command returns
// these, so the cached global real ones can not be used.
Box::new(determine_calling_process())
} else {
let (caller_mutex, _) = &**CALLER;

let mut caller = caller_mutex.lock().unwrap();
if *caller == CallingProcess::Pending {
*caller = determine_calling_process();
}

Box::new(caller.clone())
}
}

fn determine_calling_process() -> CallingProcess {
Expand Down Expand Up @@ -589,6 +603,9 @@ pub mod tests {
}
})
}
pub fn are_set() -> bool {
FAKE_ARGS.with(|a| *a.borrow() != TlsState::None)
}
fn error(where_: &str) -> ! {
panic!(
"test logic error (in {}): wrong FakeParentArgs scope?",
Expand Down

0 comments on commit 91da24d

Please sign in to comment.