@@ -6,6 +6,15 @@ pub const ROUND: usize = 10000;
66#[ cfg( miri) ]
77pub const ROUND : usize = 20 ;
88
9+ #[ cfg( feature = "compio_dispatcher" ) ]
10+ use std:: sync:: OnceLock ;
11+
12+ #[ cfg( feature = "compio_dispatcher" ) ]
13+ use compio:: dispatcher:: Dispatcher ;
14+
15+ #[ cfg( feature = "compio_dispatcher" ) ]
16+ pub static COMPIO_DISPATCHER : OnceLock < Dispatcher > = OnceLock :: new ( ) ;
17+
918pub fn _setup_log ( ) {
1019 #[ cfg( feature = "trace_log" ) ]
1120 {
@@ -57,25 +66,29 @@ macro_rules! runtime_block_on {
5766 log:: info!( "run with smol" ) ;
5867 smol:: block_on( $f)
5968 }
60- #[ cfg( not ( feature = "smol" ) ) ]
69+ #[ cfg( feature = "async_std" ) ]
6170 {
62- #[ cfg( feature = "async_std" ) ]
63- {
64- log:: info!( "run with async_std" ) ;
65- async_std:: task:: block_on( $f)
66- }
67- #[ cfg( any( feature = "tokio" , not( feature = "async_std" ) ) ) ]
68- {
69- let runtime_flag = std:: env:: var( "SINGLE_THREAD_RUNTIME" ) . unwrap_or( "" . to_string( ) ) ;
70- let mut rt = if runtime_flag. len( ) > 0 {
71- log:: info!( "run with tokio current thread" ) ;
72- tokio:: runtime:: Builder :: new_current_thread( )
73- } else {
74- log:: info!( "run with tokio multi thread" ) ;
75- tokio:: runtime:: Builder :: new_multi_thread( )
76- } ;
77- rt. enable_all( ) . build( ) . unwrap( ) . block_on( $f)
78- }
71+ log:: info!( "run with async_std" ) ;
72+ async_std:: task:: block_on( $f)
73+ }
74+ #[ cfg( feature = "tokio" ) ]
75+ {
76+ let runtime_flag = std:: env:: var( "SINGLE_THREAD_RUNTIME" ) . unwrap_or( "" . to_string( ) ) ;
77+ let mut rt = if runtime_flag. len( ) > 0 {
78+ log:: info!( "run with tokio current thread" ) ;
79+ tokio:: runtime:: Builder :: new_current_thread( )
80+ } else {
81+ log:: info!( "run with tokio multi thread" ) ;
82+ tokio:: runtime:: Builder :: new_multi_thread( )
83+ } ;
84+ rt. enable_all( ) . build( ) . unwrap( ) . block_on( $f)
85+ }
86+ #[ cfg( any( feature = "compio" , feature = "compio_dispatcher" ) ) ]
87+ {
88+ log:: info!( "run with compio" ) ;
89+
90+ let rt = compio:: runtime:: Runtime :: new( ) . unwrap( ) ;
91+ rt. block_on( $f)
7992 }
8093 } } ;
8194}
@@ -88,16 +101,27 @@ macro_rules! async_spawn {
88101 {
89102 smol:: spawn( $f)
90103 }
91- #[ cfg( not( feature = "smol" ) ) ]
104+ #[ cfg( feature = "async_std" ) ]
105+ {
106+ async_std:: task:: spawn( $f)
107+ }
108+ #[ cfg( feature = "tokio" ) ]
109+ {
110+ tokio:: spawn( $f)
111+ }
112+ #[ cfg( feature = "compio" ) ]
113+ {
114+ compio:: runtime:: spawn( $f)
115+ }
116+ #[ cfg( feature = "compio_dispatcher" ) ]
92117 {
93- #[ cfg( feature = "async_std" ) ]
94- {
95- async_std:: task:: spawn( $f)
96- }
97- #[ cfg( any( feature = "tokio" , not( feature = "async_std" ) ) ) ]
98- {
99- tokio:: spawn( $f)
100- }
118+ let disp = crate :: tests:: common:: COMPIO_DISPATCHER . get_or_init( || {
119+ compio:: dispatcher:: DispatcherBuilder :: new( )
120+ . worker_threads( std:: num:: NonZero :: new( 8 ) . unwrap( ) )
121+ . build( )
122+ . expect( "create dispatcher" )
123+ } ) ;
124+ disp. dispatch( move || $f) . expect( "dispatch" )
101125 }
102126 } } ;
103127}
@@ -106,20 +130,17 @@ pub(super) use async_spawn;
106130#[ allow( dead_code) ]
107131macro_rules! async_join_result {
108132 ( $th: expr) => { {
109- #[ cfg( feature = "smol" ) ]
133+ #[ cfg( any ( feature = "async_std" , feature = " smol") ) ]
110134 {
111135 $th. await
112136 }
113- #[ cfg( not ( feature = "smol " ) ) ]
137+ #[ cfg( any ( feature = "compio" , feature = "tokio ") ) ]
114138 {
115- #[ cfg( feature = "async_std" ) ]
116- {
117- $th. await
118- }
119- #[ cfg( not( feature = "async_std" ) ) ]
120- {
121- $th. await . expect( "join" )
122- }
139+ $th. await . expect( "join" )
140+ }
141+ #[ cfg( feature = "compio_dispatcher" ) ]
142+ {
143+ $th. await . expect( "join" )
123144 }
124145 } } ;
125146}
@@ -182,16 +203,17 @@ pub async fn sleep(duration: std::time::Duration) {
182203 {
183204 smol:: Timer :: after ( duration) . await ;
184205 }
185- #[ cfg( not ( feature = "smol" ) ) ]
206+ #[ cfg( feature = "async_std" ) ]
186207 {
187- #[ cfg( feature = "async_std" ) ]
188- {
189- async_std:: task:: sleep ( duration) . await ;
190- }
191- #[ cfg( not( feature = "async_std" ) ) ]
192- {
193- tokio:: time:: sleep ( duration) . await ;
194- }
208+ async_std:: task:: sleep ( duration) . await ;
209+ }
210+ #[ cfg( feature = "tokio" ) ]
211+ {
212+ tokio:: time:: sleep ( duration) . await ;
213+ }
214+ #[ cfg( any( feature = "compio" , feature = "compio_dispatcher" ) ) ]
215+ {
216+ compio:: time:: sleep ( duration) . await ;
195217 }
196218}
197219
@@ -206,10 +228,16 @@ where
206228 . await
207229 . map_err ( |_| format ! ( "Test timed out after {:?}" , duration) )
208230 }
209- #[ cfg( not ( feature = "async_std" ) ) ]
231+ #[ cfg( feature = "tokio" ) ]
210232 {
211233 tokio:: time:: timeout ( duration, future)
212234 . await
213235 . map_err ( |_| format ! ( "Test timed out after {:?}" , duration) )
214236 }
237+ #[ cfg( any( feature = "compio" , feature = "compio_dispatcher" ) ) ]
238+ {
239+ compio:: time:: timeout ( duration, future)
240+ . await
241+ . map_err ( |_| format ! ( "Test timed out after {:?}" , duration) )
242+ }
215243}
0 commit comments