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 }
32
32
anyhow.workspace = true
33
33
clap.workspace = true
34
34
futures-lite.workspace = true
35
+ futures-concurrency.workspace = true
35
36
humantime.workspace = true
36
37
serde = { workspace = true , features = [" derive" ] }
37
38
serde_json.workspace = true
@@ -63,6 +64,7 @@ cargo_metadata = "0.18.1"
63
64
clap = { version = " 4.5.26" , features = [" derive" ] }
64
65
futures-core = " 0.3.19"
65
66
futures-lite = " 1.12.0"
67
+ futures-concurrency = " 7.6"
66
68
humantime = " 2.1.0"
67
69
heck = " 0.5"
68
70
http = " 1.1"
Original file line number Diff line number Diff line change @@ -321,4 +321,31 @@ mod test {
321
321
) ;
322
322
} )
323
323
}
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
+ }
324
351
}
You can’t perform that action at this time.
0 commit comments