Skip to content

Commit

Permalink
coverage: Add test to codify existing behavior
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonathan-conder committed Sep 6, 2024
1 parent 9c01301 commit 3446ca5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/coverage/await_ready.cov-map
Original file line number Diff line number Diff line change
@@ -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)

38 changes: 38 additions & 0 deletions tests/coverage/await_ready.coverage
Original file line number Diff line number Diff line change
@@ -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<F: Future>(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| |}

37 changes: 37 additions & 0 deletions tests/coverage/await_ready.rs
Original file line number Diff line number Diff line change
@@ -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<F: Future>(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;
}
}
}
}

0 comments on commit 3446ca5

Please sign in to comment.