@@ -2,14 +2,23 @@ use std::time::Duration;
22
33#[ derive( Debug , Copy , Clone ) ]
44pub ( crate ) enum SleepBackend {
5+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "reqwest" ) ) ]
6+ Tokio ,
57 #[ cfg( not( target_arch = "wasm32" ) ) ]
68 Thread ,
79 #[ cfg( target_arch = "wasm32" ) ]
810 Javascript ,
911}
1012
1113impl SleepBackend {
12- pub ( crate ) fn infer ( ) -> Self {
14+ pub ( crate ) fn infer ( is_tokio : bool ) -> Self {
15+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "reqwest" ) ) ]
16+ if is_tokio {
17+ return Self :: Tokio ;
18+ }
19+ #[ cfg( any( target_arch = "wasm32" , not( feature = "reqwest" ) ) ) ]
20+ let _ = is_tokio;
21+
1322 #[ cfg( not( target_arch = "wasm32" ) ) ]
1423 return Self :: Thread ;
1524
@@ -19,6 +28,10 @@ impl SleepBackend {
1928
2029 pub ( crate ) async fn sleep ( self , interval : Duration ) {
2130 match self {
31+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "reqwest" ) ) ]
32+ Self :: Tokio => {
33+ tokio:: time:: sleep ( interval) . await ;
34+ }
2235 #[ cfg( not( target_arch = "wasm32" ) ) ]
2336 Self :: Thread => {
2437 let ( sender, receiver) = futures_channel:: oneshot:: channel :: < ( ) > ( ) ;
@@ -54,6 +67,17 @@ mod test {
5467 use super :: * ;
5568 use meilisearch_test_macro:: meilisearch_test;
5669
70+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "reqwest" ) ) ]
71+ #[ meilisearch_test]
72+ async fn sleep_tokio ( ) {
73+ let sleep_duration = Duration :: from_millis ( 10 ) ;
74+ let now = std:: time:: Instant :: now ( ) ;
75+
76+ SleepBackend :: Tokio . sleep ( sleep_duration) . await ;
77+
78+ assert ! ( now. elapsed( ) >= sleep_duration) ;
79+ }
80+
5781 #[ cfg( not( target_arch = "wasm32" ) ) ]
5882 #[ meilisearch_test]
5983 async fn sleep_thread ( ) {
0 commit comments