Skip to content

Commit

Permalink
tests: add a test case for task::consume_budget
Browse files Browse the repository at this point in the history
The test case ensures that the task::consume_budget utility
actually burns budget and makes the task yield once the whole
budget is gone.
  • Loading branch information
psarna committed Feb 15, 2022
1 parent 147de9a commit 9f0e842
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tokio/tests/rt_common.rs
Expand Up @@ -1043,6 +1043,30 @@ rt_test! {
});
}

#[test]
fn coop_consume_budget() {
let rt = rt();

rt.block_on(async {
poll_fn(|cx| {
let counter = Arc::new(std::sync::Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let mut worker = Box::pin(async move {
// Consume the budget until a yield happens
for _ in 0..1000 {
*counter.lock().unwrap() += 1;
task::consume_budget().await
}
});
// Assert that the worker was yielded and it didn't manage
// to finish the whole work (assuming the total budget of 128)
assert!(Pin::new(&mut worker).poll(cx).is_pending());
assert!(*counter_clone.lock().unwrap() < 1000);
std::task::Poll::Ready(())
}).await;
});
}

// Tests that the "next task" scheduler optimization is not able to starve
// other tasks.
#[test]
Expand Down

0 comments on commit 9f0e842

Please sign in to comment.