Skip to content

Commit

Permalink
replace TestFuture helper with std::future::poll_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mueller678 committed Feb 4, 2025
1 parent 623f1a2 commit 0724269
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions tests/udp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::future::poll_fn;
use std::task::Poll;
use std::{
io::{self, ErrorKind},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
Expand Down Expand Up @@ -168,45 +167,39 @@ fn poll_recv() -> Result {
let mut sim = Builder::new().build();

sim.client("server", async {
struct TestFuture<F: FnMut(&mut Context)>(F);
impl<F: FnMut(&mut Context) + Unpin> Future for TestFuture<F> {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::into_inner(self).0(cx);
Poll::Ready(())
}
}

let expected_origin = lookup("client");
let sock = bind().await?;
let buffer = &mut [0u8; 64];
let mut read_buf = ReadBuf::new(buffer);

TestFuture(|cx| {
poll_fn(|cx| {
let received = sock.poll_recv_from(cx, &mut read_buf);
assert!(matches!(received, Poll::Pending));
Poll::Ready(())
})
.await;

// before client sends
sleep(Duration::from_millis(1000)).await;
// after client sends

TestFuture(|cx| {
poll_fn(|cx| {
let received = sock.poll_recv_from(cx, &mut read_buf);
assert!(matches!(received , Poll::Ready(Ok(x)) if x.ip() == expected_origin));
Poll::Ready(())
})
.await;
sock.readable().await?;
TestFuture(|cx| {
poll_fn(|cx| {
let received = sock.poll_recv_from(cx, &mut read_buf);
assert!(matches!(received , Poll::Ready(Ok(x)) if x.ip() == expected_origin));
Poll::Ready(())
})
.await;
TestFuture(|cx| {
poll_fn(|cx| {
let received = sock.poll_recv_from(cx, &mut read_buf);
assert!(matches!(received, Poll::Pending));
Poll::Ready(())
})
.await;
assert_eq!(read_buf.filled(), b"pingping");
Expand Down

0 comments on commit 0724269

Please sign in to comment.