Skip to content

Commit

Permalink
Add initial loom tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harryscholes committed Feb 29, 2024
1 parent 84320ae commit cad239f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[target.'cfg(loom)'.dependencies]
loom = "0.7.1"

[dev-dependencies]
proptest = "1.3.1"
50 changes: 49 additions & 1 deletion src/queue.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use std::ptr;

#[cfg(loom)]
use loom::{
hint,
sync::atomic::{AtomicPtr, Ordering},
};
#[cfg(not(loom))]
use std::{
hint, ptr,
hint,
sync::atomic::{AtomicPtr, Ordering},
};

Expand Down Expand Up @@ -137,6 +145,7 @@ impl<T> Node<T> {
}

#[cfg(test)]
#[cfg(not(loom))]
mod tests {
use std::{
collections::HashMap,
Expand Down Expand Up @@ -452,3 +461,42 @@ mod tests {
}
}
}

#[cfg(test)]
#[cfg(loom)]
mod loom_tests {
use loom::{sync::Arc, thread};

use super::*;

#[test]
fn test_multiple_concurrent_enqueuers_single_dequeuer() {
loom::model(|| {
let num_threads = 2;

let q = Arc::new(Queue::new());

let handles = (0..num_threads)
.map(|i| {
let q = q.clone();

thread::spawn(move || {
q.enqueue(i);
})
})
.collect::<Vec<_>>();

for handle in handles {
handle.join().unwrap();
}

let mut res = vec![];
while let Some(item) = q.dequeue() {
res.push(item);
}
res.sort();

assert_eq!(res, (0..num_threads).collect::<Vec<_>>());
});
}
}

0 comments on commit cad239f

Please sign in to comment.