Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net): add from_std for UdpSocket #302

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compio-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ pin-project-lite = { version = "0.2.14", optional = true }

[dev-dependencies]
compio-runtime = { workspace = true }
# use tokio to show this crate doesn't depend on the compio runtime
# use tokio & futures to show this crate doesn't depend on the compio runtime
tokio = { workspace = true, features = ["macros", "rt"] }
futures-executor = "0.3.30"

[features]
default = []
Expand Down
113 changes: 61 additions & 52 deletions compio-io/tests/compat.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,83 @@
use std::io::Cursor;

use compio_io::compat::AsyncStream;
use futures_executor::block_on;
use futures_util::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};

#[tokio::test]
async fn async_compat_read() {
let src = &[1u8, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0][..];
let mut stream = AsyncStream::new(src);
#[test]
fn async_compat_read() {
block_on(async {
let src = &[1u8, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0][..];
let mut stream = AsyncStream::new(src);

let mut buf = [0; 6];
let len = stream.read(&mut buf).await.unwrap();
let mut buf = [0; 6];
let len = stream.read(&mut buf).await.unwrap();

assert_eq!(len, 6);
assert_eq!(buf, [1, 1, 4, 5, 1, 4]);
assert_eq!(len, 6);
assert_eq!(buf, [1, 1, 4, 5, 1, 4]);

let mut buf = [0; 20];
let len = stream.read(&mut buf).await.unwrap();
assert_eq!(len, 7);
assert_eq!(&buf[..7], [1, 9, 1, 9, 8, 1, 0]);
let mut buf = [0; 20];
let len = stream.read(&mut buf).await.unwrap();
assert_eq!(len, 7);
assert_eq!(&buf[..7], [1, 9, 1, 9, 8, 1, 0]);
})
}

#[tokio::test]
async fn async_compat_bufread() {
let src = &[1u8, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0][..];
let mut stream = AsyncStream::new(src);
#[test]
fn async_compat_bufread() {
block_on(async {
let src = &[1u8, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0][..];
let mut stream = AsyncStream::new(src);

let slice = stream.fill_buf().await.unwrap();
assert_eq!(slice, [1, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0]);
stream.consume_unpin(6);
let slice = stream.fill_buf().await.unwrap();
assert_eq!(slice, [1, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0]);
stream.consume_unpin(6);

let mut buf = [0; 7];
let len = stream.read(&mut buf).await.unwrap();
let mut buf = [0; 7];
let len = stream.read(&mut buf).await.unwrap();

assert_eq!(len, 7);
assert_eq!(buf, [1, 9, 1, 9, 8, 1, 0]);
assert_eq!(len, 7);
assert_eq!(buf, [1, 9, 1, 9, 8, 1, 0]);
})
}

#[tokio::test]
async fn async_compat_write() {
let dst = Cursor::new([0u8; 10]);
let mut stream = AsyncStream::new(dst);
#[test]
fn async_compat_write() {
block_on(async {
let dst = Cursor::new([0u8; 10]);
let mut stream = AsyncStream::new(dst);

let len = stream.write(&[1, 1, 4, 5, 1, 4]).await.unwrap();
stream.flush().await.unwrap();
let len = stream.write(&[1, 1, 4, 5, 1, 4]).await.unwrap();
stream.flush().await.unwrap();

assert_eq!(len, 6);
assert_eq!(stream.get_ref().position(), 6);
assert_eq!(stream.get_ref().get_ref(), &[1, 1, 4, 5, 1, 4, 0, 0, 0, 0]);
assert_eq!(len, 6);
assert_eq!(stream.get_ref().position(), 6);
assert_eq!(stream.get_ref().get_ref(), &[1, 1, 4, 5, 1, 4, 0, 0, 0, 0]);

let dst = Cursor::new([0u8; 10]);
let mut stream = AsyncStream::with_capacity(10, dst);
let len = stream
.write(&[1, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0])
.await
.unwrap();
assert_eq!(len, 10);
let dst = Cursor::new([0u8; 10]);
let mut stream = AsyncStream::with_capacity(10, dst);
let len = stream
.write(&[1, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0])
.await
.unwrap();
assert_eq!(len, 10);

stream.flush().await.unwrap();
assert_eq!(stream.get_ref().get_ref(), &[1, 1, 4, 5, 1, 4, 1, 9, 1, 9]);
stream.flush().await.unwrap();
assert_eq!(stream.get_ref().get_ref(), &[1, 1, 4, 5, 1, 4, 1, 9, 1, 9]);
})
}

#[tokio::test]
async fn async_compat_flush_fail() {
let dst = Cursor::new([0u8; 10]);
let mut stream = AsyncStream::new(dst);
let len = stream
.write(&[1, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0])
.await
.unwrap();
assert_eq!(len, 13);
let err = stream.flush().await.unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
#[test]
fn async_compat_flush_fail() {
block_on(async {
let dst = Cursor::new([0u8; 10]);
let mut stream = AsyncStream::new(dst);
let len = stream
.write(&[1, 1, 4, 5, 1, 4, 1, 9, 1, 9, 8, 1, 0])
.await
.unwrap();
assert_eq!(len, 13);
let err = stream.flush().await.unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
})
}
Loading