mirror of
https://github.com/bertptrs/tracing-mutex.git
synced 2025-12-25 20:50:32 +01:00
Add a fuzz-test for the mutex ID's graph
This commit is contained in:
@@ -222,7 +222,7 @@ where
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use rand::prelude::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
34
src/lib.rs
34
src/lib.rs
@@ -89,9 +89,6 @@ lazy_static! {
|
|||||||
///
|
///
|
||||||
/// This type is currently private to prevent usage while the exact implementation is figured out,
|
/// This type is currently private to prevent usage while the exact implementation is figured out,
|
||||||
/// but it will likely be public in the future.
|
/// but it will likely be public in the future.
|
||||||
///
|
|
||||||
/// One possible alteration is to make this type not `Copy` but `Drop`, and handle deregistering
|
|
||||||
/// the lock from there.
|
|
||||||
struct MutexId(usize);
|
struct MutexId(usize);
|
||||||
|
|
||||||
impl MutexId {
|
impl MutexId {
|
||||||
@@ -271,6 +268,9 @@ fn get_dependency_graph() -> impl DerefMut<Target = DiGraph<usize>> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use rand::thread_rng;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -303,4 +303,32 @@ mod tests {
|
|||||||
// If b's destructor correctly ran correctly we can now add an edge from c to a.
|
// If b's destructor correctly ran correctly we can now add an edge from c to a.
|
||||||
assert!(get_dependency_graph().add_edge(c.value(), a.value()));
|
assert!(get_dependency_graph().add_edge(c.value(), a.value()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fuzz the global dependency graph by fake-acquiring lots of mutexes in a valid order.
|
||||||
|
///
|
||||||
|
/// This test generates all possible forward edges in a 100-node graph consisting of natural
|
||||||
|
/// numbers, shuffles them, then adds them to the graph. This will always be a valid directed,
|
||||||
|
/// acyclic graph because there is a trivial order (the natural numbers) but because the edges
|
||||||
|
/// are added in a random order the DiGraph will still occassionally need to reorder nodes.
|
||||||
|
#[test]
|
||||||
|
fn fuzz_mutex_id() {
|
||||||
|
const NUM_NODES: usize = 100;
|
||||||
|
|
||||||
|
let ids: Vec<MutexId> = (0..NUM_NODES).map(|_| Default::default()).collect();
|
||||||
|
|
||||||
|
let mut edges = Vec::with_capacity(NUM_NODES * NUM_NODES);
|
||||||
|
for i in 0..NUM_NODES {
|
||||||
|
for j in i..NUM_NODES {
|
||||||
|
edges.push((i, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
edges.shuffle(&mut thread_rng());
|
||||||
|
|
||||||
|
for (x, y) in edges {
|
||||||
|
// Acquire the mutexes, smallest first to ensure a cycle-free graph
|
||||||
|
let _ignored = ids[x].get_borrowed();
|
||||||
|
let _ = ids[y].get_borrowed();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user