Skip to content

Commit

Permalink
add WaitNTicks to make tests stronger
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Sep 8, 2024
1 parent 4f2ab14 commit 216bff4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions capnp-rpc/test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,33 @@ fn null_capability() {
assert!(root.get_interface_field().is_err());
}

struct WaitNTicks {
remaining: u32,
}

impl WaitNTicks {
fn new(n: u32) -> Self {
Self { remaining: n }
}
}

impl Future for WaitNTicks {
type Output = ();

fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
if self.remaining > 0 {
self.remaining -= 1;
cx.waker().wake_by_ref(); // Wake up the task again
std::task::Poll::Pending
} else {
std::task::Poll::Ready(())
}
}
}

#[test]
fn set_pipeline() {
use std::cell::Cell;
Expand Down Expand Up @@ -514,6 +541,9 @@ fn set_pipeline() {
let response2 = pipeline_promise2.await?;
crate::test_util::CheckTestMessage::check_test_message(response2.get()?);

// Give the original promise an opportunity to complete.
WaitNTicks::new(5).await;

// The original promise never completed.
assert!(!promise_completed.get());
Ok(())
Expand Down Expand Up @@ -557,6 +587,9 @@ fn set_pipeline_local() {
let response2 = pipeline_promise2.await?;
crate::test_util::CheckTestMessage::check_test_message(response2.get()?);

// Give the original promise an opportunity to complete.
WaitNTicks::new(5).await;

// The original promise never completed.
assert!(!promise_completed.get());
Ok(())
Expand Down

0 comments on commit 216bff4

Please sign in to comment.