From f2694a642ed6d105e74518cf1a9439f3abf06a32 Mon Sep 17 00:00:00 2001 From: Tom Parker-Shemilt Date: Wed, 27 Nov 2024 21:18:39 +0000 Subject: [PATCH] Explicit testing for tokio multi-thread Because https://github.com/palfrey/serial_test/issues/121 flagged a possible issue --- Cargo.lock | 18 +++++++++++++++++ serial_test_test/Cargo.toml | 3 ++- serial_test_test/src/lib.rs | 39 ++++++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b10e48..ec8fb00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,6 +168,12 @@ dependencies = [ "slab", ] +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "itertools" version = "0.10.5" @@ -232,6 +238,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -366,6 +382,7 @@ dependencies = [ "log", "once_cell", "parking_lot", + "scc", "scoped-tls", "serial_test", "tokio", @@ -437,6 +454,7 @@ dependencies = [ "autocfg", "libc", "mio", + "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", diff --git a/serial_test_test/Cargo.toml b/serial_test_test/Cargo.toml index 8f64aee..bb3787f 100644 --- a/serial_test_test/Cargo.toml +++ b/serial_test_test/Cargo.toml @@ -16,9 +16,10 @@ lock_api = { version="^0.4.7", default-features = false } wasm-bindgen-test = {version="0.3.20", optional=true, default-features = false } scoped-tls = { version="1", optional=true, default-features = false } log = { version = ">=0.4.4" , default-features = false } +scc = { version = "2", default-features = false} [dev-dependencies] -tokio = { version = "^1.27", features = ["macros", "rt"], default-features = false } +tokio = { version = "^1.27", features = ["macros", "rt", "rt-multi-thread"], default-features = false } actix-rt = { version = "^2.8", features = ["macros"], default-features = false } futures-util = {version = "^0.3", default-features = false } diff --git a/serial_test_test/src/lib.rs b/serial_test_test/src/lib.rs index 5fa70d1..009c253 100644 --- a/serial_test_test/src/lib.rs +++ b/serial_test_test/src/lib.rs @@ -37,6 +37,8 @@ //! ``` use log::info; +use once_cell::sync::OnceCell; +use scc::HashMap; #[cfg(test)] use serial_test::{parallel, serial}; use std::{ @@ -47,19 +49,24 @@ use std::{ time::Duration, }; -static LOCK: AtomicUsize = AtomicUsize::new(0); +static LOCKS: OnceCell> = OnceCell::new(); fn init() { let _ = env_logger::builder().is_test(false).try_init(); } -pub fn test_fn(count: usize) { +pub fn test_fn(key: &str, count: usize) { init(); + let local_locks = LOCKS.get_or_init(HashMap::new); + let entry = local_locks + .entry(key.to_string()) + .or_insert(AtomicUsize::new(0)); + let local_lock = entry.get(); info!("(non-fs) Start {}", count); - LOCK.store(count, Ordering::Relaxed); + local_lock.store(count, Ordering::Relaxed); thread::sleep(Duration::from_millis(1000 * (count as u64))); info!("(non-fs) End {}", count); - assert_eq!(LOCK.load(Ordering::Relaxed), count); + assert_eq!(local_lock.load(Ordering::Relaxed), count); } pub fn fs_test_fn(count: usize) { @@ -134,19 +141,19 @@ mod tests { #[test] #[serial(alpha)] fn test_serial_1() { - test_fn(1) + test_fn("alpha", 1) } #[test] #[serial(alpha)] fn test_serial_2() { - test_fn(2) + test_fn("alpha", 2) } #[test] #[serial(alpha)] fn test_serial_3() { - test_fn(3) + test_fn("alpha", 3) } #[test] @@ -394,4 +401,22 @@ mod tests { #[serial] #[wasm_bindgen_test] async fn wasm_works_second() {} + + #[tokio::test(flavor = "multi_thread")] + #[serial(slt)] + async fn tokio_multi_1() { + test_fn("tokio", 1); + } + + #[tokio::test(flavor = "multi_thread")] + #[serial(slt)] + async fn tokio_multi_2() { + test_fn("tokio", 2); + } + + #[tokio::test(flavor = "multi_thread")] + #[serial(slt)] + async fn tokio_multi_3() { + test_fn("tokio", 3); + } }