#io-stream #mocking #io #async-io #mock-stream

mock-io

A crate with mock IO stream and listener implementations

7 releases

0.3.2 Jun 23, 2021
0.3.1 May 13, 2021
0.3.0 Jan 27, 2021
0.2.1 Oct 20, 2020
0.1.1 Oct 20, 2020

#382 in Testing

Download history 23/week @ 2024-07-27 136/week @ 2024-08-03 21/week @ 2024-08-10 25/week @ 2024-08-17 9/week @ 2024-08-24 41/week @ 2024-08-31 4/week @ 2024-09-07 62/week @ 2024-09-21 25/week @ 2024-09-28 4/week @ 2024-10-05 4/week @ 2024-10-12 9/week @ 2024-10-19 44/week @ 2024-10-26 26/week @ 2024-11-02 10/week @ 2024-11-09

89 downloads per month
Used in 2 crates

MIT/Apache

31KB
713 lines

mock-io

CI Crates.io Documentation License

A crate with mock IO stream and listener implementations.

Usage

Add mock-io in your Cargo.toml's dependencies section:

[dependencies]
mock-io = "0.3"

Here is a sample usage of this crate:

use mock_io::sync::{MockListener, MockStream};

let (listener, handle) = MockListener::new();

thread::spawn(move || {
    let mut stream = MockStream::connect(&handle).unwrap();
    stream.write(&1u64.to_be_bytes()).unwrap();
    stream.write(&2u64.to_be_bytes()).unwrap();
});

while let Ok(mut stream) = listener.accept() {
    let mut buf = [0; 8];

    stream.read(&mut buf).unwrap();
    assert_eq!(1u64.to_be_bytes(), buf);
    
    stream.read(&mut buf).unwrap();
    assert_eq!(2u64.to_be_bytes(), buf);
}

Features

  • sync: Enables sync mock IO stream and listener
    • Enabled by default
  • async-futures: Enables async mock IO stream and listener (using futures::io::{AsyncRead, AsyncWrite})
    • Disabled by default
  • async-tokio: Enables async mock IO stream and listener (using tokio::io::{AsyncRead, AsyncWrite})
    • Disabled by default

Note: Some functions in this crate returns a Future. So, you'll need an executor to drive Futures returned from these functions. async-std and tokio are two popular options.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.2–6MB
~36K SLoC