#async-io #read-write #io-operations #partial #quickcheck #proptest #io-error

dev partial-io

Helpers to test partial, interrupted and would-block I/O operations, with support for property-based testing through proptest and quickcheck

7 releases

0.5.4 Sep 28, 2022
0.5.3 Sep 27, 2022
0.5.0 Jan 28, 2021
0.4.0 Sep 25, 2020
0.3.1 Jun 7, 2019

#133 in Asynchronous

Download history 2872/week @ 2024-08-04 3916/week @ 2024-08-11 2626/week @ 2024-08-18 3760/week @ 2024-08-25 2063/week @ 2024-09-01 1826/week @ 2024-09-08 1811/week @ 2024-09-15 2933/week @ 2024-09-22 2449/week @ 2024-09-29 1982/week @ 2024-10-06 2191/week @ 2024-10-13 1142/week @ 2024-10-20 2066/week @ 2024-10-27 1674/week @ 2024-11-03 1968/week @ 2024-11-10 2062/week @ 2024-11-17

7,833 downloads per month
Used in 14 crates

MIT license

67KB
1K SLoC

partial-io

partial-io on crates.io Documentation (latest release) Documentation (main) License

Helpers for testing I/O behavior with partial, interrupted and blocking reads and writes.

This library provides:

  • PartialRead and PartialWrite, which wrap existing Read and Write implementations and allow specifying arbitrary behavior on the next read, write or flush call.
  • With the optional futures03 and tokio1 features, PartialAsyncRead and PartialAsyncWrite to wrap existing AsyncRead and AsyncWrite implementations. These implementations are task-aware, so they will know how to pause and unpause tasks if they return a WouldBlock error.
  • With the optional proptest1 (proptest) and quickcheck1 (quickcheck) features, generation of random sequences of operations for property-based testing. See the proptest_types and quickcheck_types documentation for more.

Motivation

A Read or Write wrapper is conceptually simple but can be difficult to get right, especially if the wrapper has an internal buffer. Common issues include:

  • A partial read or write, even without an error, might leave the wrapper in an invalid state (example fix).

With the AsyncRead and AsyncWrite provided by futures03 and tokio1:

  • A call to read_to_end or write_all within the wrapper might be partly successful but then error out. These functions will return the error without informing the caller of how much was read or written. Wrappers with an internal buffer will want to advance their state corresponding to the partial success, so they can't use read_to_end or write_all (example fix).
  • Instances must propagate Poll::Pending up, but that shouldn't leave them in an invalid state.

These situations can be hard to think about and hard to test.

partial-io can help in two ways:

  1. For a known bug involving any of these situations, partial-io can help you write a test.
  2. With the quickcheck1 feature enabled, partial-io can also help shake out bugs in your wrapper. See quickcheck_types for more.

Examples

use std::io::{self, Cursor, Read};

use partial_io::{PartialOp, PartialRead};

let data = b"Hello, world!".to_vec();
let cursor = Cursor::new(data);  // Cursor<Vec<u8>> implements io::Read
let ops = vec![PartialOp::Limited(7), PartialOp::Err(io::ErrorKind::Interrupted)];
let mut partial_read = PartialRead::new(cursor, ops);

let mut out = vec![0; 256];

// The first read will read 7 bytes.
assert_eq!(partial_read.read(&mut out).unwrap(), 7);
assert_eq!(&out[..7], b"Hello, ");
// The second read will fail with ErrorKind::Interrupted.
assert_eq!(partial_read.read(&mut out[7..]).unwrap_err().kind(), io::ErrorKind::Interrupted);
// The iterator has run out of operations, so it no longer truncates reads.
assert_eq!(partial_read.read(&mut out[7..]).unwrap(), 6);
assert_eq!(&out[..13], b"Hello, world!");

For a real-world example, see the tests in zstd-rs.

Minimum supported Rust version

The minimum supported Rust version (MSRV) is 1.56.

While a crate is pre-release status (0.x.x) it may have its MSRV bumped in a patch release. Once a crate has reached 1.x, any MSRV bump will be accompanied with a new minor version.

Contributing

See the CONTRIBUTING file for how to help out.

License

This project is available under the MIT license.

Dependencies

~0–6.5MB
~36K SLoC