From 3446ca535ecead268a4c8393ba7a48fd783b363a Mon Sep 17 00:00:00 2001 From: Jonathan Conder Date: Fri, 6 Sep 2024 12:52:32 +1200 Subject: [PATCH] coverage: Add test to codify existing behavior Currently `await` is only counted towards coverage if the containing function is suspended and resumed at least once. A future commit will fix this and update the test to reflect the new behavior. --- tests/coverage/await_ready.cov-map | 26 ++++++++++++++++++++ tests/coverage/await_ready.coverage | 38 +++++++++++++++++++++++++++++ tests/coverage/await_ready.rs | 37 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 tests/coverage/await_ready.cov-map create mode 100644 tests/coverage/await_ready.coverage create mode 100644 tests/coverage/await_ready.rs diff --git a/tests/coverage/await_ready.cov-map b/tests/coverage/await_ready.cov-map new file mode 100644 index 00000000000..c6dfc01e861 --- /dev/null +++ b/tests/coverage/await_ready.cov-map @@ -0,0 +1,26 @@ +Function name: await_ready::await_ready +Raw bytes (9): 0x[01, 01, 00, 01, 01, 0a, 01, 00, 1e] +Number of files: 1 +- file 0 => global file 1 +Number of expressions: 0 +Number of file 0 mappings: 1 +- Code(Counter(0)) at (prev + 10, 1) to (start + 0, 30) + +Function name: await_ready::await_ready::{closure#0} +Raw bytes (19): 0x[01, 01, 00, 03, 01, 0a, 1e, 02, 0c, 05, 03, 0a, 00, 0f, 09, 01, 01, 00, 02] +Number of files: 1 +- file 0 => global file 1 +Number of expressions: 0 +Number of file 0 mappings: 3 +- Code(Counter(0)) at (prev + 10, 30) to (start + 2, 12) +- Code(Counter(1)) at (prev + 3, 10) to (start + 0, 15) +- Code(Counter(2)) at (prev + 1, 1) to (start + 0, 2) + +Function name: await_ready::main +Raw bytes (9): 0x[01, 01, 00, 01, 01, 10, 01, 03, 02] +Number of files: 1 +- file 0 => global file 1 +Number of expressions: 0 +Number of file 0 mappings: 1 +- Code(Counter(0)) at (prev + 16, 1) to (start + 3, 2) + diff --git a/tests/coverage/await_ready.coverage b/tests/coverage/await_ready.coverage new file mode 100644 index 00000000000..bddaf15cf80 --- /dev/null +++ b/tests/coverage/await_ready.coverage @@ -0,0 +1,38 @@ + LL| |#![feature(coverage_attribute)] + LL| |#![feature(custom_inner_attributes)] // for #![rustfmt::skip] + LL| |#![feature(noop_waker)] + LL| |#![rustfmt::skip] + LL| |//@ edition: 2021 + LL| | + LL| |#[coverage(off)] + LL| |async fn ready() -> u8 { 1 } + LL| | + LL| 1|async fn await_ready() -> u8 { + LL| 1| // FIXME(#98712): await is only covered if the function yields + LL| 1| ready() + LL| 0| .await + LL| 1|} + LL| | + LL| 1|fn main() { + LL| 1| let mut future = Box::pin(await_ready()); + LL| 1| executor::block_on(future.as_mut()); + LL| 1|} + LL| | + LL| |mod executor { + LL| | use core::future::Future; + LL| | use core::pin::pin; + LL| | use core::task::{Context, Poll, Waker}; + LL| | + LL| | #[coverage(off)] + LL| | pub fn block_on(mut future: F) -> F::Output { + LL| | let mut future = pin!(future); + LL| | let mut context = Context::from_waker(Waker::noop()); + LL| | + LL| | loop { + LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) { + LL| | break val; + LL| | } + LL| | } + LL| | } + LL| |} + diff --git a/tests/coverage/await_ready.rs b/tests/coverage/await_ready.rs new file mode 100644 index 00000000000..884ff16fff6 --- /dev/null +++ b/tests/coverage/await_ready.rs @@ -0,0 +1,37 @@ +#![feature(coverage_attribute)] +#![feature(custom_inner_attributes)] // for #![rustfmt::skip] +#![feature(noop_waker)] +#![rustfmt::skip] +//@ edition: 2021 + +#[coverage(off)] +async fn ready() -> u8 { 1 } + +async fn await_ready() -> u8 { + // FIXME(#98712): await is only covered if the function yields + ready() + .await +} + +fn main() { + let mut future = Box::pin(await_ready()); + executor::block_on(future.as_mut()); +} + +mod executor { + use core::future::Future; + use core::pin::pin; + use core::task::{Context, Poll, Waker}; + + #[coverage(off)] + pub fn block_on(mut future: F) -> F::Output { + let mut future = pin!(future); + let mut context = Context::from_waker(Waker::noop()); + + loop { + if let Poll::Ready(val) = future.as_mut().poll(&mut context) { + break val; + } + } + } +}