Skip to content

Commit

Permalink
more stress testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Jul 6, 2024
1 parent 62f46e7 commit d9f4fb8
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 116 deletions.
10 changes: 8 additions & 2 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,14 @@ where
return unsafe { Table::from_raw(next) };
}

// Double the table capacity.
let next_capacity = capacity.unwrap_or(self.table.len << 1);
let next_capacity = match option_env!("PAPAYA_RESIZE_STRESS") {
// Never grow the table to stress the incremental resizing algorithm.
Some(_) => self.table.len,
// Double the table capacity.
None => self.table.len << 1,
};

let next_capacity = capacity.unwrap_or(next_capacity);
assert!(
next_capacity <= isize::MAX as usize,
"`HashMap` exceeded maximum capacity"
Expand Down
16 changes: 16 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ fn concurrent_update() {
#[test]
#[cfg_attr(miri, ignore)]
fn concurrent_resize_and_get() {
if resize_stress!() {
return;
}

with_map::<usize, usize>(|map| {
let map = map();
let map = Arc::new(map);
Expand Down Expand Up @@ -470,6 +474,10 @@ fn debug() {

#[test]
fn extend() {
if resize_stress!() {
return;
}

with_map::<usize, usize>(|map| {
let map = map();
let guard = map.guard();
Expand All @@ -491,6 +499,10 @@ fn extend() {

#[test]
fn extend_ref() {
if resize_stress!() {
return;
}

with_map::<usize, usize>(|map| {
let map = map();
let mut entries: Vec<(&usize, &usize)> = vec![(&42, &0), (&16, &6), (&38, &42)];
Expand Down Expand Up @@ -530,6 +542,10 @@ fn len() {

#[test]
fn iter() {
if resize_stress!() {
return;
}

with_map::<usize, usize>(|map| {
let map = map();
let len = if cfg!(miri) { 100 } else { 10_000 };
Expand Down
38 changes: 36 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#![allow(dead_code)]

use papaya::{HashMap, ResizeMode};

// Run the test on different configurations of a `HashMap`.
pub fn with_map<K, V>(mut test: impl FnMut(&dyn Fn() -> HashMap<K, V>)) {
// Blocking resize mode.
test(&(|| HashMap::builder().resize_mode(ResizeMode::Blocking).build()));
if !crate::resize_stress!() {
test(&(|| HashMap::builder().resize_mode(ResizeMode::Blocking).build()));
}

// Incremental resize mode with a small chunk to stress operations on nested tables.
test(
Expand All @@ -19,8 +23,38 @@ pub fn with_map<K, V>(mut test: impl FnMut(&dyn Fn() -> HashMap<K, V>)) {
test(
&(|| {
HashMap::builder()
.resize_mode(ResizeMode::Incremental(256))
.resize_mode(ResizeMode::Incremental(128))
.build()
}),
);
}

// Prints a log message if `RUST_LOG=debug` is set.
#[macro_export]
macro_rules! debug {
($($x:tt)*) => {
if std::env::var("RUST_LOG").as_deref() == Ok("debug") {
println!($($x)*);
}
};
}

// Returns a `bool` indicating whether resize stress is enabled.
//
// If this is true, linearizable operations such as iteration cannot be
// performed and will block indefinitely.
#[macro_export]
macro_rules! resize_stress {
() => {
option_env!("PAPAYA_RESIZE_STRESS").is_some()
};
}

// Returns the number of threads to use for stress testing.
pub fn threads() -> usize {
if cfg!(miri) {
2
} else {
std::thread::available_parallelism().unwrap().get() / 2
}
}
Loading

0 comments on commit d9f4fb8

Please sign in to comment.