MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / cooperative_concurrency

Function cooperative_concurrency

src/runtime/reactor.rs:434–458  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

432
433 #[test]
434 fn cooperative_concurrency() {
435 crate::runtime::block_on(async {
436 let cpu_heavy = async move {
437 // Simulating a CPU-heavy task that runs for 1 second and yields occasionally
438 for _ in 0..10 {
439 std::thread::sleep(std::time::Duration::from_millis(100));
440 futures_lite::future::yield_now().await;
441 }
442 true
443 };
444 let timeout = async move {
445 crate::time::Timer::after(crate::time::Duration::from_millis(200))
446 .wait()
447 .await;
448 false
449 };
450 let mut future_group = futures_concurrency::future::FutureGroup::<
451 Pin<Box<dyn std::future::Future<Output = bool>>>,
452 >::new();
453 future_group.insert(Box::pin(cpu_heavy));
454 future_group.insert(Box::pin(timeout));
455 let result = futures_lite::StreamExt::next(&mut future_group).await;
456 assert_eq!(result, Some(false), "cpu_heavy task should have timed out");
457 });
458 }
459}

Callers

nothing calls this directly

Calls 3

block_onFunction · 0.85
sleepFunction · 0.85
waitMethod · 0.80

Tested by

no test coverage detected