Skip to content

Commit

Permalink
Change test to count polls instead
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Jan 23, 2025
1 parent d761c88 commit 411a803
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions tokio/tests/rt_poll_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,48 @@

#[cfg(tokio_unstable)]
mod unstable {
use std::sync::{atomic::AtomicUsize, Arc, Mutex};
use std::{
future::Future,
sync::{atomic::AtomicUsize, Arc, Mutex},
};

use tokio::task::yield_now;

pin_project_lite::pin_project! {
struct PollCounter<F> {
#[pin]
inner: F,
counter: Arc<AtomicUsize>,
}
}

impl<F: Future> Future for PollCounter<F> {
type Output = F::Output;

fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let this = self.project();
this.counter
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
this.inner.poll(cx)
}
}

impl PollCounter<()> {
fn new<F: Future>(future: F) -> (PollCounter<F>, Arc<AtomicUsize>) {
let counter = Arc::new(AtomicUsize::new(0));
(
PollCounter {
inner: future,
counter: counter.clone(),
},
counter,
)
}
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn callbacks_fire_multi_thread() {
Expand Down Expand Up @@ -39,11 +77,12 @@ mod unstable {
})
.build()
.unwrap();
let task = rt.spawn(async {
let (task, count) = PollCounter::new(async {
yield_now().await;
yield_now().await;
yield_now().await;
});
let task = rt.spawn(task);

let spawned_task_id = task.id();

Expand All @@ -57,8 +96,15 @@ mod unstable {
after_task_poll_callback_task_id.lock().unwrap().unwrap(),
spawned_task_id
);
assert_eq!(poll_start.load(std::sync::atomic::Ordering::SeqCst), 4);
assert_eq!(poll_stop.load(std::sync::atomic::Ordering::SeqCst), 4);
let actual_count = count.load(std::sync::atomic::Ordering::SeqCst);
assert_eq!(
poll_start.load(std::sync::atomic::Ordering::SeqCst),
actual_count
);
assert_eq!(
poll_stop.load(std::sync::atomic::Ordering::SeqCst),
actual_count
);
}

#[test]
Expand Down

0 comments on commit 411a803

Please sign in to comment.