Skip to content

Commit 4ae7920

Browse files
committed
coverage: Add a test for async blocks
We have coverage tests that use async functions, but none that use async blocks.
1 parent 5810dee commit 4ae7920

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

tests/coverage/async_block.cov-map

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Function name: async_block::main
2+
Raw bytes (38): 0x[01, 01, 02, 01, 05, 03, 05, 06, 01, 05, 01, 00, 0b, 05, 01, 09, 00, 0a, 03, 00, 0e, 00, 13, 05, 00, 14, 01, 16, 05, 07, 0a, 02, 06, 06, 03, 01, 00, 02]
3+
Number of files: 1
4+
- file 0 => global file 1
5+
Number of expressions: 2
6+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
7+
- expression 1 operands: lhs = Expression(0, Add), rhs = Counter(1)
8+
Number of file 0 mappings: 6
9+
- Code(Counter(0)) at (prev + 5, 1) to (start + 0, 11)
10+
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 10)
11+
- Code(Expression(0, Add)) at (prev + 0, 14) to (start + 0, 19)
12+
= (c0 + c1)
13+
- Code(Counter(1)) at (prev + 0, 20) to (start + 1, 22)
14+
- Code(Counter(1)) at (prev + 7, 10) to (start + 2, 6)
15+
- Code(Expression(1, Sub)) at (prev + 3, 1) to (start + 0, 2)
16+
= ((c0 + c1) - c1)
17+
18+
Function name: async_block::main::{closure#0}
19+
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 07, 1c, 01, 17, 05, 01, 18, 02, 0e, 02, 02, 14, 02, 0e, 07, 03, 09, 00, 0a]
20+
Number of files: 1
21+
- file 0 => global file 1
22+
Number of expressions: 2
23+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
24+
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
25+
Number of file 0 mappings: 4
26+
- Code(Counter(0)) at (prev + 7, 28) to (start + 1, 23)
27+
- Code(Counter(1)) at (prev + 1, 24) to (start + 2, 14)
28+
- Code(Expression(0, Sub)) at (prev + 2, 20) to (start + 2, 14)
29+
= (c0 - c1)
30+
- Code(Expression(1, Add)) at (prev + 3, 9) to (start + 0, 10)
31+
= (c1 + (c0 - c1))
32+

tests/coverage/async_block.coverage

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
LL| |#![feature(coverage_attribute)]
2+
LL| |#![feature(noop_waker)]
3+
LL| |// edition: 2021
4+
LL| |
5+
LL| 1|fn main() {
6+
LL| 17| for i in 0..16 {
7+
^16
8+
LL| 16| let future = async {
9+
LL| 16| if i >= 12 {
10+
LL| 4| println!("big");
11+
LL| 12| } else {
12+
LL| 12| println!("small");
13+
LL| 12| }
14+
LL| 16| };
15+
LL| 16| executor::block_on(future);
16+
LL| 16| }
17+
LL| 1|}
18+
LL| |
19+
LL| |mod executor {
20+
LL| | use core::future::Future;
21+
LL| | use core::pin::pin;
22+
LL| | use core::task::{Context, Poll, Waker};
23+
LL| |
24+
LL| | #[coverage(off)]
25+
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
26+
LL| | let mut future = pin!(future);
27+
LL| | let waker = Waker::noop();
28+
LL| | let mut context = Context::from_waker(&waker);
29+
LL| |
30+
LL| | loop {
31+
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
32+
LL| | break val;
33+
LL| | }
34+
LL| | }
35+
LL| | }
36+
LL| |}
37+

tests/coverage/async_block.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(coverage_attribute)]
2+
#![feature(noop_waker)]
3+
// edition: 2021
4+
5+
fn main() {
6+
for i in 0..16 {
7+
let future = async {
8+
if i >= 12 {
9+
println!("big");
10+
} else {
11+
println!("small");
12+
}
13+
};
14+
executor::block_on(future);
15+
}
16+
}
17+
18+
mod executor {
19+
use core::future::Future;
20+
use core::pin::pin;
21+
use core::task::{Context, Poll, Waker};
22+
23+
#[coverage(off)]
24+
pub fn block_on<F: Future>(mut future: F) -> F::Output {
25+
let mut future = pin!(future);
26+
let waker = Waker::noop();
27+
let mut context = Context::from_waker(&waker);
28+
29+
loop {
30+
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
31+
break val;
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)