-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* revert e2e lockfile to old version * remove global state from tests * thread local * generalize global values * sort imports, cleanup
- Loading branch information
1 parent
89c795f
commit 86894d2
Showing
10 changed files
with
1,187 additions
and
1,433 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#[macro_export] | ||
/// Specify a global value that can be accessed from anywhere in the application. | ||
/// Positional arguments: | ||
/// - `$name`: The name of the global value. This will be the name of the variable that holds the value. | ||
/// - `$type`: The type of the global value. | ||
/// - `$init`: The initial value of the global value. | ||
/// - `$set_fn`: The name of the function that will be used to set the global value. | ||
/// - `$get_fn`: The name of the function that will be used to get the global value. | ||
/// | ||
/// The macro will also automatically generate boilerplate code for unit tests to work correctly. | ||
macro_rules! global_value { | ||
($name:ident, $type:ty, $init:expr, $set_fn:ident, $get_fn:ident) => { | ||
use std::sync::RwLock; | ||
#[cfg(not(test))] | ||
use std::sync::RwLockReadGuard; | ||
|
||
#[cfg(test)] | ||
thread_local! { | ||
static $name: RwLock<$type> = const { RwLock::new($init) }; | ||
} | ||
|
||
#[cfg(not(test))] | ||
static $name: RwLock<$type> = RwLock::new($init); | ||
|
||
#[cfg(not(test))] | ||
pub fn $set_fn(value: $type) { | ||
*$name.write().expect("Failed to acquire lock on the mutex.") = value; | ||
} | ||
|
||
#[cfg(not(test))] | ||
pub fn $get_fn() -> RwLockReadGuard<'static, $type> { | ||
$name.read().expect("Failed to acquire lock on the mutex.") | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn $set_fn(new_value: $type) { | ||
$name.with(|value| { | ||
*value.write().expect("Failed to acquire lock on the mutex.") = new_value; | ||
}); | ||
} | ||
|
||
// This is not really a 1:1 replacement for the non-test RwLockReadGuard<'static, $type> as the RwLock may be tried to be | ||
// dereferenced | ||
#[cfg(test)] | ||
pub fn $get_fn() -> $type { | ||
$name.with(|value| { | ||
value | ||
.read() | ||
.expect("Failed to acquire lock on the mutex.") | ||
.clone() | ||
}) | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters