File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ serde_json = { workspace = true, optional = true }
3232anyhow.workspace = true
3333clap.workspace = true
3434futures-lite.workspace = true
35+ futures-concurrency.workspace = true
3536humantime.workspace = true
3637serde = { workspace = true , features = [" derive" ] }
3738serde_json.workspace = true
@@ -63,6 +64,7 @@ cargo_metadata = "0.18.1"
6364clap = { version = " 4.5.26" , features = [" derive" ] }
6465futures-core = " 0.3.19"
6566futures-lite = " 1.12.0"
67+ futures-concurrency = " 7.6"
6668humantime = " 2.1.0"
6769heck = " 0.5"
6870http = " 1.1"
Original file line number Diff line number Diff line change @@ -321,4 +321,31 @@ mod test {
321321 ) ;
322322 } )
323323 }
324+
325+ #[ test]
326+ fn cooperative_concurrency ( ) {
327+ crate :: runtime:: block_on ( async {
328+ let cpu_heavy = async move {
329+ // Simulating a CPU-heavy task that runs for 1 second and yields occasionally
330+ for _ in 0 ..10 {
331+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
332+ futures_lite:: future:: yield_now ( ) . await ;
333+ }
334+ true
335+ } ;
336+ let timeout = async move {
337+ crate :: time:: Timer :: after ( crate :: time:: Duration :: from_millis ( 200 ) )
338+ . wait ( )
339+ . await ;
340+ false
341+ } ;
342+ let mut future_group = futures_concurrency:: future:: FutureGroup :: <
343+ Pin < Box < dyn std:: future:: Future < Output = bool > > > ,
344+ > :: new ( ) ;
345+ future_group. insert ( Box :: pin ( cpu_heavy) ) ;
346+ future_group. insert ( Box :: pin ( timeout) ) ;
347+ let result = futures_lite:: StreamExt :: next ( & mut future_group) . await ;
348+ assert_eq ! ( result, Some ( false ) , "cpu_heavy task should have timed out" ) ;
349+ } ) ;
350+ }
324351}
You can’t perform that action at this time.
0 commit comments