Giter Site home page Giter Site logo

yancouto / rust-partial-io Goto Github PK

View Code? Open in Web Editor NEW

This project forked from facebookarchive/rust-partial-io

0.0 1.0 0.0 32.99 MB

Rust library that provides helpers for testing resilience of IO operations

Home Page: https://facebookincubator.github.io/rust-partial-io

License: MIT License

Rust 97.78% Shell 2.22%

rust-partial-io's Introduction

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 quickcheck1 feature, generation of random sequences of operations which can be provided to one of the wrappers. See the 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.

Contributing

See the CONTRIBUTING file for how to help out.

License

This project is available under the MIT license.

rust-partial-io's People

Contributors

bolinfest avatar dependabot[bot] avatar facebook-github-bot avatar jsgf avatar kulshrax avatar ljw1004 avatar lukaspiatkowski avatar sid0 avatar stanislavglebik avatar sunshowers avatar yancouto avatar zertosh avatar zpao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.