37
37
//! ```
38
38
39
39
use log:: info;
40
+ use once_cell:: sync:: OnceCell ;
41
+ use scc:: HashMap ;
40
42
#[ cfg( test) ]
41
43
use serial_test:: { parallel, serial} ;
42
44
use std:: {
@@ -47,19 +49,24 @@ use std::{
47
49
time:: Duration ,
48
50
} ;
49
51
50
- static LOCK : AtomicUsize = AtomicUsize :: new ( 0 ) ;
52
+ static LOCKS : OnceCell < HashMap < String , AtomicUsize > > = OnceCell :: new ( ) ;
51
53
52
54
fn init ( ) {
53
55
let _ = env_logger:: builder ( ) . is_test ( false ) . try_init ( ) ;
54
56
}
55
57
56
- pub fn test_fn ( count : usize ) {
58
+ pub fn test_fn ( key : & str , count : usize ) {
57
59
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 ( ) ;
58
65
info ! ( "(non-fs) Start {}" , count) ;
59
- LOCK . store ( count, Ordering :: Relaxed ) ;
66
+ local_lock . store ( count, Ordering :: Relaxed ) ;
60
67
thread:: sleep ( Duration :: from_millis ( 1000 * ( count as u64 ) ) ) ;
61
68
info ! ( "(non-fs) End {}" , count) ;
62
- assert_eq ! ( LOCK . load( Ordering :: Relaxed ) , count) ;
69
+ assert_eq ! ( local_lock . load( Ordering :: Relaxed ) , count) ;
63
70
}
64
71
65
72
pub fn fs_test_fn ( count : usize ) {
@@ -134,19 +141,19 @@ mod tests {
134
141
#[ test]
135
142
#[ serial( alpha) ]
136
143
fn test_serial_1 ( ) {
137
- test_fn ( 1 )
144
+ test_fn ( "alpha" , 1 )
138
145
}
139
146
140
147
#[ test]
141
148
#[ serial( alpha) ]
142
149
fn test_serial_2 ( ) {
143
- test_fn ( 2 )
150
+ test_fn ( "alpha" , 2 )
144
151
}
145
152
146
153
#[ test]
147
154
#[ serial( alpha) ]
148
155
fn test_serial_3 ( ) {
149
- test_fn ( 3 )
156
+ test_fn ( "alpha" , 3 )
150
157
}
151
158
152
159
#[ test]
@@ -394,4 +401,22 @@ mod tests {
394
401
#[ serial]
395
402
#[ wasm_bindgen_test]
396
403
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
+ }
397
422
}
0 commit comments