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

(WIP): Adding Stream mock to tokio-test #4463

Closed
wants to merge 18 commits into from
Closed
80 changes: 80 additions & 0 deletions tokio-test/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ pub struct Builder {
// Sequence of actions for the Mock to take
actions: VecDeque<Action>,
}
#[derive(Debug, Clone, Default)]
pub struct StreamBuilder {
// Sequence of actions for the Mock to take
actions: VecDeque<Action>,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should go in its own file.


#[derive(Debug, Clone)]
enum Action {
Expand Down Expand Up @@ -143,6 +148,81 @@ impl Builder {
}
}


impl StreamBuilder {
/// Return a new, empty `Builder.
pub fn new() -> Self {
Self::default()
}

/// call next value ?
pub fn poll_next(&mut self, buf: &[u8]) {
/// TODO
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not be able to call poll_next or next on the builder. You have to actually build it first. The io builder also doesn't implement AsyncRead or AsyncWrite.


/// Sequence a `read` operation.
///
/// The next operation in the mock's script will be to expect a `read` call
/// and return `buf`.
pub fn read(&mut self, buf: &[u8]) -> &mut Self {
self.actions.push_back(Action::Read(buf.into()));
self
}

/// Sequence a `read` operation that produces an error.
///
/// The next operation in the mock's script will be to expect a `read` call
/// and return `error`.
pub fn read_error(&mut self, error: io::Error) -> &mut Self {
let error = Some(error.into());
self.actions.push_back(Action::ReadError(error));
self
}

/// Sequence a `write` operation.
///
/// The next operation in the mock's script will be to expect a `write`
/// call.
pub fn write(&mut self, buf: &[u8]) -> &mut Self {
self.actions.push_back(Action::Write(buf.into()));
self
}

/// Sequence a `write` operation that produces an error.
///
/// The next operation in the mock's script will be to expect a `write`
/// call that provides `error`.
pub fn write_error(&mut self, error: io::Error) -> &mut Self {
let error = Some(error.into());
self.actions.push_back(Action::WriteError(error));
self
}

/// Sequence a wait.
///
/// The next operation in the mock's script will be to wait without doing so
/// for `duration` amount of time.
pub fn wait(&mut self, duration: Duration) -> &mut Self {
let duration = cmp::max(duration, Duration::from_millis(1));
self.actions.push_back(Action::Wait(duration));
self
}

/// Build a `Mock` value according to the defined script.
pub fn build(&mut self) -> Mock {
let (mock, _) = self.build_with_handle();
mock
}

/// Build a `Mock` value paired with a handle
pub fn build_with_handle(&mut self) -> (Mock, Handle) {
let (inner, handle) = Inner::new(self.actions.clone());

let mock = Mock { inner };

(mock, handle)
}
}
impl Handle {
/// Sequence a `read` operation.
///
Expand Down
17 changes: 17 additions & 0 deletions tokio-test/tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,20 @@ async fn mock_panics_write_data_left() {
use tokio_test::io::Builder;
Builder::new().write(b"write").build();
}

#[tokio::test]
async fn stream_read(){

let mut mock = Builder::new().read(b"hello ").read(b"world!").build();
// let res = mock.poll_next(); or something like this ?
assert_eq!(res, b"hello ");
}

#[tokio::test]
async fn stream_write(){
let unit: u8 = 1;
let mut mock = Builder::new().write(b"hello ").write(b"world!").build();
// let mut task = get task ?
// mock.poll_write(task, unit ).assert(b"hello " or something like this ?
//
}