Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libdd-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ multer = "3.1"
bytes = "1.11.1"
rand = "0.8"
tempfile = "3.8"
rusty-fork = "0.3"
tokio = { version = "1.23", features = ["rt", "macros", "time"] }

[features]
Expand Down
100 changes: 51 additions & 49 deletions libdd-common/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ async fn parse_multipart(boundary: String, body: Vec<u8>) -> anyhow::Result<Vec<
#[cfg(test)]
mod tests {
use super::*;
use rusty_fork::rusty_fork_test;

#[test]
fn test_temp_file_guard_and_path_generation() {
Expand Down Expand Up @@ -514,58 +515,59 @@ mod tests {
assert_eq!(parsed.multipart_parts[0].name, "field");
assert_eq!(parsed.multipart_parts[0].content, b"value");
}
}

#[cfg(test)]
mod single_threaded_tests {
use crate::test_utils::count_active_threads;

#[test]
#[cfg_attr(miri, ignore)]
fn test_count_active_threads() {
let initial_count = count_active_threads().expect("Failed to count threads");
assert!(
initial_count >= 1,
"Expected at least 1 thread, got {}",
initial_count
);

// Spawn some threads and verify the count increases
use std::sync::{Arc, Barrier};
let barrier = Arc::new(Barrier::new(6)); // 5 spawned threads + main thread

let handles: Vec<_> = (0..5)
.map(|_| {
let barrier = Arc::clone(&barrier);
std::thread::spawn(move || {
barrier.wait();
std::thread::sleep(std::time::Duration::from_millis(50));
// This test must run in its own process to get accurate thread counts,
// since the test runner and other parallel tests spawn threads.
rusty_fork_test! {
#[test]
#[cfg_attr(miri, ignore)]
fn test_count_active_threads() {
use crate::test_utils::count_active_threads;

let initial_count = count_active_threads().expect("Failed to count threads");
assert!(
initial_count >= 1,
"Expected at least 1 thread, got {}",
initial_count
);

// Spawn some threads and verify the count increases
use std::sync::{Arc, Barrier};
let barrier = Arc::new(Barrier::new(6)); // 5 spawned threads + main thread

let handles: Vec<_> = (0..5)
.map(|_| {
let barrier = Arc::clone(&barrier);
std::thread::spawn(move || {
barrier.wait();
std::thread::sleep(std::time::Duration::from_millis(50));
})
})
})
.collect();

barrier.wait();
let count_with_threads = count_active_threads().expect("Failed to count threads");
assert!(
count_with_threads >= initial_count + 5,
"Expected at least {} threads (initial: {}, with 5 spawned: {})",
initial_count + 5,
initial_count,
count_with_threads
);
.collect();

barrier.wait();
let count_with_threads = count_active_threads().expect("Failed to count threads");
assert!(
count_with_threads >= initial_count + 5,
"Expected at least {} threads (initial: {}, with 5 spawned: {})",
initial_count + 5,
initial_count,
count_with_threads
);

for handle in handles {
handle.join().expect("Thread should join successfully");
}

for handle in handles {
handle.join().expect("Thread should join successfully");
let count_after_join = count_active_threads().expect("Failed to count threads");
// Allow up to 1 extra: some platforms (e.g. CentOS 7) lazily spawn a helper thread
assert!(
count_after_join <= initial_count + 1,
"Expected thread count to return to {} or {} after join, got {}",
initial_count,
initial_count + 1,
count_after_join
);
}

let count_after_join = count_active_threads().expect("Failed to count threads");
// Allow up to 1 extra: some platforms (e.g. CentOS 7) lazily spawn a helper thread
assert!(
count_after_join <= initial_count + 1,
"Expected thread count to return to {} or {} after join, got {}",
initial_count,
initial_count + 1,
count_after_join
);
}
}
Loading