use futures::pin_mut; use futures_test::task::noop_context; use std::io::IoSlice; use std::task::Poll; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}; use tokio_test::task::spawn; use tokio_test::{assert_pending, assert_ready}; use tokio_util::io::simplex;
while read < MSG.len() || write < MSG.len() { if write < MSG.len() { let n = tx.write(&MSG[write..]).await.unwrap();
write += n;
}
if read < MSG.len() { let n = rx.read(&mut buf[read..]).await.unwrap();
read += n;
}
}
assert_eq!(&buf[..], MSG);
}
}
}
/// Sanity check for multi-threaded operation. #[test] #[cfg(not(target_os = "wasi"))] // No thread on wasi. fn multi_thread() { use futures::executor::block_on; use std::thread;
let jh1 = thread::spawn(move || {
block_on(async { for _ in0..N {
tx.write_all(MSG).await.unwrap();
}
});
});
jh0.join().unwrap();
jh1.join().unwrap();
}
}
#[test] #[should_panic(expected = "capacity must be greater than zero")] fn zero_capacity() { let _ = simplex::new(0);
}
/// The `Receiver::poll_read` should return `Poll::Ready(Ok(()))` /// if the `ReadBuf` has zero remaining capacity. #[tokio::test] asyncfn read_buf_is_full() { let (_tx, rx) = simplex::new(32); letmut buf = ReadBuf::new(&mut []);
tokio::pin!(rx);
assert_ready!(rx.as_mut().poll_read(&mut noop_context(), &n style='color:red'>mut buf)).unwrap();
assert_eq!(buf.filled().len(), 0);
}
/// The `Sender::poll_write` should return `Poll::Ready(Ok(0))` /// if the input buffer has zero length. #[tokio::test] asyncfn write_buf_is_empty() { let (tx, _rx) = simplex::new(32);
tokio::pin!(tx); let n = assert_ready!(tx.as_mut().poll_write(&mut noop_context(), &[])).unwrap();
assert_eq!(n, 0);
}
/// The `Sender` should returns error if the `Receiver` has been dropped. #[tokio::test] asyncfn drop_receiver_0() { let (mut tx, rx) = simplex::new(32);
drop(rx);
tx.write_u8(1).await.unwrap_err();
}
/// The `Sender` should be woken up if the `Receiver` has been dropped. #[tokio::test] asyncfn drop_receiver_1() { let (mut tx, rx) = simplex::new(1); letmut write_task = spawn(tx.write_u16(1));
assert_pending!(write_task.poll());
/// The `Receiver` should return error if: /// /// - The `Sender` has been dropped. /// - AND there is no remaining data in the buffer. #[tokio::test] asyncfn drop_sender_0() { const MSG: &[u8] = b"Hello, world!";
/// The `Receiver` should be woken up if: /// /// - The `Sender` has been dropped. /// - AND there is still remaining data in the buffer. #[tokio::test] asyncfn drop_sender_1() { let (mut tx, mut rx) = simplex::new(2); letmut buf = vec![]; letmut read_task = spawn(rx.read_to_end(&mut buf));
assert_pending!(read_task.poll());
/// All following calls to `Sender::poll_write` and `Sender::poll_flush` /// should return error after `shutdown` has been called. #[tokio::test] asyncfn shutdown_sender_0() { const MSG: &[u8] = b"Hello, world!";
let (mut tx, _rx) = simplex::new(32);
tx.shutdown().await.unwrap();
/// The `Sender::poll_shutdown` should be called multiple times /// without error. #[tokio::test] asyncfn shutdown_sender_1() { let (mut tx, _rx) = simplex::new(32);
tx.shutdown().await.unwrap();
tx.shutdown().await.unwrap();
}
/// The `Sender::poll_shutdown` should wake up the `Receiver` #[tokio::test] asyncfn shutdown_sender_2() { let (mut tx, mut rx) = simplex::new(32);
/// The capacity is exactly same as the total length of the vectored buffers. #[tokio::test] asyncfn poll_write_vectored_0() { const MSG1: &[u8] = b"1"; const MSG2: &[u8] = b"22"; const MSG3: &[u8] = b"333"; const MSG_LEN: usize = MSG1.len() + MSG2.len() + MSG3.len();
let io_slices = &[IoSlice::new(MSG1), IoSlice::new(MSG2), IoSlice::new(MSG3)];
let (tx, mut rx) = simplex::new(MSG_LEN);
tokio::pin!(tx); let res = tx.poll_write_vectored(&mut noop_context(), io_slices); let n = assert_ready!(res).unwrap();
assert_eq!(n, MSG_LEN); letmut buf = [0; MSG_LEN]; let n = rx.read_exact(&mut buf).await.unwrap();
assert_eq!(n, MSG_LEN);
assert_eq!(&buf, b"122333");
}
/// The capacity is smaller than the total length of the vectored buffers. #[tokio::test] asyncfn poll_write_vectored_1() { const MSG1: &[u8] = b"1"; const MSG2: &[u8] = b"22"; const MSG3: &[u8] = b"333"; const CAPACITY: usize = MSG1.len() + MSG2.len() + 1;
let io_slices = &[IoSlice::new(MSG1), IoSlice::new(MSG2), IoSlice::new(MSG3)];
let (tx, mut rx) = simplex::new(CAPACITY);
tokio::pin!(tx);
// ==== The poll_write_vectored should write MSG1 and MSG2 fully, and MSG3 partially. ==== let res = tx.poll_write_vectored(&mut noop_context(), io_slices); let n = assert_ready!(res).unwrap();
assert_eq!(n, CAPACITY); letmut buf = [0; CAPACITY]; let n = rx.read_exact(&mut buf).await.unwrap();
assert_eq!(n, CAPACITY);
assert_eq!(&buf, b"1223");
}
let io_slices = &[
IoSlice::new(MSG1),
IoSlice::new(MSG2),
IoSlice::new(MSG3),
IoSlice::new(MSG4),
IoSlice::new(MSG5),
];
let (tx, mut rx) = simplex::new(MSG_LEN);
tokio::pin!(tx); let res = tx.poll_write_vectored(&mut noop_context(), io_slices); let n = assert_ready!(res).unwrap();
assert_eq!(n, MSG_LEN); letmut buf = [0; MSG_LEN]; let n = rx.read_exact(&mut buf).await.unwrap();
assert_eq!(n, MSG_LEN);
assert_eq!(&buf, b"122333");
}
/// The `Sender::poll_write_vectored` should return `Poll::Ready(Ok(0))` /// if all the input buffers have zero length. #[tokio::test] asyncfn poll_write_vectored_3() { let io_slices = &[IoSlice::new(&[]), IoSlice::new(&[]), IoSlice::new(&[])]; let (tx, _rx) = simplex::new(32);
tokio::pin!(tx); let n = assert_ready!(tx.poll_write_vectored(&mut noop_context(), io_slices)).unwrap();
assert_eq!(n, 0);
}
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.27Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-08-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.