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 May 30, 2022
1 parent 4edffb1 commit 945c75f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tokio/tests/rt_common.rs
Expand Up @@ -1054,6 +1054,31 @@ rt_test! {
});
}

#[cfg(tokio_unstable)]
#[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 945c75f

Please sign in to comment.