Skip to content

Commit 4eaa148

Browse files
authored
Merge pull request #122 from palfrey/tokio-multi-test
Explicit testing for tokio multi-thread
2 parents 3ac9744 + f2694a6 commit 4eaa148

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

Cargo.lock

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

serial_test_test/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ lock_api = { version="^0.4.7", default-features = false }
1616
wasm-bindgen-test = {version="0.3.20", optional=true, default-features = false }
1717
scoped-tls = { version="1", optional=true, default-features = false }
1818
log = { version = ">=0.4.4" , default-features = false }
19+
scc = { version = "2", default-features = false}
1920

2021
[dev-dependencies]
21-
tokio = { version = "^1.27", features = ["macros", "rt"], default-features = false }
22+
tokio = { version = "^1.27", features = ["macros", "rt", "rt-multi-thread"], default-features = false }
2223
actix-rt = { version = "^2.8", features = ["macros"], default-features = false }
2324
futures-util = {version = "^0.3", default-features = false }
2425

serial_test_test/src/lib.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
//! ```
3838
3939
use log::info;
40+
use once_cell::sync::OnceCell;
41+
use scc::HashMap;
4042
#[cfg(test)]
4143
use serial_test::{parallel, serial};
4244
use std::{
@@ -47,19 +49,24 @@ use std::{
4749
time::Duration,
4850
};
4951

50-
static LOCK: AtomicUsize = AtomicUsize::new(0);
52+
static LOCKS: OnceCell<HashMap<String, AtomicUsize>> = OnceCell::new();
5153

5254
fn init() {
5355
let _ = env_logger::builder().is_test(false).try_init();
5456
}
5557

56-
pub fn test_fn(count: usize) {
58+
pub fn test_fn(key: &str, count: usize) {
5759
init();
60+
let local_locks = LOCKS.get_or_init(HashMap::new);
61+
let entry = local_locks
62+
.entry(key.to_string())
63+
.or_insert(AtomicUsize::new(0));
64+
let local_lock = entry.get();
5865
info!("(non-fs) Start {}", count);
59-
LOCK.store(count, Ordering::Relaxed);
66+
local_lock.store(count, Ordering::Relaxed);
6067
thread::sleep(Duration::from_millis(1000 * (count as u64)));
6168
info!("(non-fs) End {}", count);
62-
assert_eq!(LOCK.load(Ordering::Relaxed), count);
69+
assert_eq!(local_lock.load(Ordering::Relaxed), count);
6370
}
6471

6572
pub fn fs_test_fn(count: usize) {
@@ -134,19 +141,19 @@ mod tests {
134141
#[test]
135142
#[serial(alpha)]
136143
fn test_serial_1() {
137-
test_fn(1)
144+
test_fn("alpha", 1)
138145
}
139146

140147
#[test]
141148
#[serial(alpha)]
142149
fn test_serial_2() {
143-
test_fn(2)
150+
test_fn("alpha", 2)
144151
}
145152

146153
#[test]
147154
#[serial(alpha)]
148155
fn test_serial_3() {
149-
test_fn(3)
156+
test_fn("alpha", 3)
150157
}
151158

152159
#[test]
@@ -394,4 +401,22 @@ mod tests {
394401
#[serial]
395402
#[wasm_bindgen_test]
396403
async fn wasm_works_second() {}
404+
405+
#[tokio::test(flavor = "multi_thread")]
406+
#[serial(slt)]
407+
async fn tokio_multi_1() {
408+
test_fn("tokio", 1);
409+
}
410+
411+
#[tokio::test(flavor = "multi_thread")]
412+
#[serial(slt)]
413+
async fn tokio_multi_2() {
414+
test_fn("tokio", 2);
415+
}
416+
417+
#[tokio::test(flavor = "multi_thread")]
418+
#[serial(slt)]
419+
async fn tokio_multi_3() {
420+
test_fn("tokio", 3);
421+
}
397422
}

0 commit comments

Comments
 (0)