Skip to content

Commit

Permalink
Explicit testing for tokio multi-thread
Browse files Browse the repository at this point in the history
Because #121 flagged a possible issue
  • Loading branch information
palfrey committed Nov 27, 2024
1 parent 3ac9744 commit f2694a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion serial_test_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
39 changes: 32 additions & 7 deletions serial_test_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
//! ```
use log::info;
use once_cell::sync::OnceCell;
use scc::HashMap;
#[cfg(test)]
use serial_test::{parallel, serial};
use std::{
Expand All @@ -47,19 +49,24 @@ use std::{
time::Duration,
};

static LOCK: AtomicUsize = AtomicUsize::new(0);
static LOCKS: OnceCell<HashMap<String, AtomicUsize>> = 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) {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f2694a6

Please sign in to comment.