Skip to content

Commit 1932831

Browse files
pchickeySilverMira
andcommitted
add failing test for cooperative concurrency
as provided by @SilverMira in #70, and tracked in #73 Co-authored-by: SilverMira <[email protected]>
1 parent eab1d6e commit 1932831

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ serde_json = { workspace = true, optional = true }
3232
anyhow.workspace = true
3333
clap.workspace = true
3434
futures-lite.workspace = true
35+
futures-concurrency.workspace = true
3536
humantime.workspace = true
3637
serde = { workspace = true, features = ["derive"] }
3738
serde_json.workspace = true
@@ -63,6 +64,7 @@ cargo_metadata = "0.18.1"
6364
clap = { version = "4.5.26", features = ["derive"] }
6465
futures-core = "0.3.19"
6566
futures-lite = "1.12.0"
67+
futures-concurrency = "7.6"
6668
humantime = "2.1.0"
6769
heck = "0.5"
6870
http = "1.1"

src/runtime/reactor.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)