-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18b304b
commit ccee808
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod core; | ||
pub mod std; | ||
pub mod tokio; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Mock APIs for `tokio` traits | ||
/// Mock APIs for `tokio::io` traits | ||
#[cfg(feature = "mock-tokio")] | ||
pub mod io { | ||
use core::pin::Pin; | ||
use core::task::{Context, Poll}; | ||
use std::io::IoSlice; | ||
|
||
use tokio::io::{ReadBuf, Result, SeekFrom}; | ||
|
||
use crate::unimock; | ||
|
||
#[unimock(prefix=crate, api=AsyncBufReadMock, mirror=tokio::io::AsyncBufRead)] | ||
pub trait AsyncBufRead { | ||
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>>; | ||
fn consume(self: Pin<&mut Self>, amt: usize); | ||
} | ||
|
||
#[unimock(prefix=crate, api=AsyncReadMock, mirror=tokio::io::AsyncRead)] | ||
pub trait AsyncRead { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<Result<()>>; | ||
} | ||
|
||
#[unimock(prefix=crate, api=AsyncSeekMock, mirror=tokio::io::AsyncSeek)] | ||
pub trait AsyncSeek { | ||
fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> Result<()>; | ||
fn poll_complete(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<u64>>; | ||
} | ||
|
||
#[unimock(prefix=crate, api=AsyncWriteMock, mirror=tokio::io::AsyncWrite)] | ||
pub trait AsyncWrite { | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<Result<usize>>; | ||
|
||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>; | ||
|
||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>; | ||
|
||
fn poll_write_vectored( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &[IoSlice<'_>], | ||
) -> Poll<Result<usize>> { | ||
} | ||
|
||
fn is_write_vectored(&self) -> bool {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use core::task::Poll; | ||
|
||
use unimock::{mock::tokio::io::AsyncReadMock, *}; | ||
|
||
use tokio::io::AsyncReadExt; | ||
|
||
#[tokio::test] | ||
async fn test_tokio_read() { | ||
let mut u = Unimock::new(( | ||
AsyncReadMock::poll_read | ||
.next_call(matching!()) | ||
.mutates(|buf, (_, _)| { | ||
buf.put_slice(&[1, 2, 3]); | ||
|
||
// Can return Poll::Ready explicitly | ||
Poll::Ready(Ok(())) | ||
}), | ||
AsyncReadMock::poll_read | ||
.next_call(matching!()) | ||
.mutates(|buf, _| { | ||
buf.put_slice(&[5, 6, 7]); | ||
|
||
// Also can return just Ok(()) because of `impl From<T> for Poll<T>` | ||
Ok(()) | ||
}), | ||
)); | ||
let mut buf = [0; 10]; | ||
|
||
let n = u.read(&mut buf).await.unwrap(); | ||
assert_eq!(n, 3); | ||
assert_eq!(&buf[0..n], &[1, 2, 3]); | ||
|
||
u.read(&mut buf).await.unwrap(); | ||
} |