| 1 |
//! Allows testing code to enable and disable failspots |
| 2 |
//! |
| 3 |
//! When testing a crate, failspots can be enabled through a [Client] object. This can be retrieved |
| 4 |
//! by using the `fn testing_client() -> Client<'static, Self>` method that exists as part of |
| 5 |
//! every enum that was declared using the [`failspot_name!()`][crate::failspot_name] macro. |
| 6 |
//! |
| 7 |
//! The [`Client::set_enabled()`][Client::set_enabled] method can be used to set or unset a |
| 8 |
//! failspot. [`Client::reset()`][Client::reset] will unset all failspots. |
| 9 |
//! |
| 10 |
//! Example usage: |
| 11 |
//! |
| 12 |
//! ``` |
| 13 |
//! # failspot::failspot_name! { pub enum FailSpotName { Name1 } } |
| 14 |
//! # fn run_tests() {} |
| 15 |
//! let mut client = FailSpotName::testing_client(); |
| 16 |
//! client.set_enabled(FailSpotName::Name1, true); |
| 17 |
//! // When the `Client` object drops, all the failspots will be reset to disabled |
| 18 |
//! // Must ensure it stays alive while tests are running. |
| 19 |
//! run_tests(); |
| 20 |
//! ``` |
| 21 |
//! |
| 22 |
//! # Concurrency -- Important!!! |
| 23 |
//! |
| 24 |
//! **TL;DR -- Put all your integration tests that use failspot in a separate source file!** |
| 25 |
//! |
| 26 |
//! ## The problem |
| 27 |
//! |
| 28 |
//! In Rust, **tests are run concurrently by default**. Since the configuration for the failspots |
| 29 |
//! is a global variable that will be shared by all threads, that would create a problem -- Tests |
| 30 |
//! that don't use failspots will suddenly start failing because another concurrent test enabled |
| 31 |
//! them, and tests that do use failspots would clobber each other's configuration. |
| 32 |
//! |
| 33 |
//! To prevent this, the [Client] returned by `testing_client()` is **protected by a mutex** -- |
| 34 |
//! Only one test at a time can configure the failspots through the `Client` methods. When the |
| 35 |
//! client is dropped, all the failspots are reset to disabled state and the mutex is released so |
| 36 |
//! the next test can start with a fresh state. |
| 37 |
//! |
| 38 |
//! This means **every test that may run concurrently with a failspot test must hold the [Client] |
| 39 |
//! object the entire time the test is running**, even if that test doesn't actually use failspots. |
| 40 |
//! If there are multiple enums declared with [`failspot_name!()`][crate::failspot_name] then a |
| 41 |
//! [Client] object for each enum must be held by every test that may run concurrently. |
| 42 |
//! |
| 43 |
//! For tests that use failspots, this is intuitive -- Most tests that use failspots will create a |
| 44 |
//! [Client] as part of their setup. |
| 45 |
//! |
| 46 |
//! ## Stopping regular tests from breaking |
| 47 |
//! |
| 48 |
//! For tests that don't use failspots, there are 2 choices: |
| 49 |
//! |
| 50 |
//! 1. **Put failspot tests in their own source file (recommended)** Integration tests in |
| 51 |
//! different source files are run in different processes, so separating failspot and non |
| 52 |
//! failspot tests eliminates the concurrency issue. |
| 53 |
//! |
| 54 |
//! 2. **Force tests to run serially** By setting `RUST_TEST_THREADS=1` in the enviroment, the |
| 55 |
//! tests will run one-at-a-time and there will be no interference. |
| 56 |
//! |
| 57 |
//! Obviously, the first one should be preferred unless there is a good reason not to. |
| 58 |
|
| 59 |
use { |
| 60 |
flagset::FlagSet, |
| 61 |
std::{ |
| 62 |
ops::{Deref, DerefMut}, |
| 63 |
sync::{Mutex, MutexGuard, RwLock}, |
| 64 |
}, |
| 65 |
}; |
| 66 |
|
| 67 |
/// Config object for an enum declared with [`failspot_name!()`][crate::failspot_name] |
| 68 |
/// |
| 69 |
/// Every failspot enum has one of these attached. It tracks which failspots are currently |
| 70 |
/// enabled for that enum, and contains the mutex that ensures that only one [Client] at a time |
| 71 |
/// is running. It is not normally used directly by user code, but is instead used by the |
| 72 |
/// [`failspot!()`][crate::failspot] macro for testing failpoints, and by the `testing_client()` |
| 73 |
/// method to obtain a [Client] for testing code. |
| 74 |
#[derive(Debug)] |
| 75 |
pub struct Config<T: flagset::Flags> { |
| 76 |
inner: RwLock<ConfigInner<T>>, |
| 77 |
client_mutex: Mutex<()>, |
| 78 |
} |
| 79 |
|
| 80 |
#[derive(Debug, Eq, PartialEq)] |
| 81 |
struct ConfigInner<T: flagset::Flags> { |
| 82 |
enabled_spots: FlagSet<T>, |
| 83 |
} |
| 84 |
|
| 85 |
impl<T: flagset::Flags> Default for Config<T> { |
| 86 |
fn default() -> Self { |
| 87 |
Self { |
| 88 |
inner: Default::default(), |
| 89 |
client_mutex: Default::default(), |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl<T: flagset::Flags> Default for ConfigInner<T> { |
| 95 |
fn default() -> Self { |
| 96 |
Self { |
| 97 |
enabled_spots: Default::default(), |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl<T: flagset::Flags> Config<T> { |
| 103 |
/// Returns whether or not the given failspot is enabled |
| 104 |
pub fn enabled(&self, spot: T) -> bool { |
| 105 |
self.inner().enabled_spots.contains(spot) |
| 106 |
} |
| 107 |
/// Returns a client for this failspot config |
| 108 |
pub fn client(&self) -> Client<'_, T> { |
| 109 |
Client::new(self) |
| 110 |
} |
| 111 |
fn inner(&self) -> impl Deref<Target = ConfigInner<T>> + '_ { |
| 112 |
self.inner.read().unwrap() |
| 113 |
} |
| 114 |
fn inner_mut(&self) -> impl DerefMut<Target = ConfigInner<T>> + '_ { |
| 115 |
self.inner.write().unwrap() |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/// Client for testing code |
| 120 |
/// |
| 121 |
/// See [module-level docs][self], especially the part about concurrency. |
| 122 |
#[derive(Debug)] |
| 123 |
pub struct Client<'a, T: flagset::Flags> { |
| 124 |
config: &'a Config<T>, |
| 125 |
_guard: MutexGuard<'a, ()>, |
| 126 |
} |
| 127 |
|
| 128 |
impl<'a, T: flagset::Flags> Client<'a, T> { |
| 129 |
/// Create a new [Client]. |
| 130 |
/// |
| 131 |
/// Normally not used directly -- Use `EnumName::testing_client()` instead |
| 132 |
pub fn new(config: &'a Config<T>) -> Self { |
| 133 |
let _guard = config |
| 134 |
.client_mutex |
| 135 |
.lock() |
| 136 |
.unwrap_or_else(|e| e.into_inner()); |
| 137 |
|
| 138 |
assert_eq!( |
| 139 |
*config.inner(), |
| 140 |
ConfigInner::default(), |
| 141 |
"somehow failed to reset config to default after last client" |
| 142 |
); |
| 143 |
|
| 144 |
Client { config, _guard } |
| 145 |
} |
| 146 |
/// Set whether the given failspot is enabled or disabled |
| 147 |
pub fn set_enabled(&mut self, spot: T, enabled: bool) -> &mut Self { |
| 148 |
if enabled { |
| 149 |
self.config.inner_mut().enabled_spots |= spot; |
| 150 |
} else { |
| 151 |
self.config.inner_mut().enabled_spots -= spot; |
| 152 |
} |
| 153 |
self |
| 154 |
} |
| 155 |
/// Reset all failspots to disabled |
| 156 |
pub fn reset(&mut self) -> &mut Self { |
| 157 |
*self.config.inner_mut() = ConfigInner::default(); |
| 158 |
self |
| 159 |
} |
| 160 |
/// Finish with a [Client], resetting all failspots to disabled and releasing the mutex |
| 161 |
/// |
| 162 |
/// Identical to dropping the [Client], but a bit more explicit about intent. |
| 163 |
pub fn finish(self) { |
| 164 |
drop(self) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl<'a, T: flagset::Flags> Drop for Client<'a, T> { |
| 169 |
fn drop(&mut self) { |
| 170 |
self.reset(); |
| 171 |
} |
| 172 |
} |