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

Add error handling tests to PaaS I/O #1947

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions exercises/practice/paasio/tests/paasio.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::{Error, ErrorKind, Read, Result, Write};

/// test a few read scenarios
macro_rules! test_read {
($(#[$attr:meta])* $modname:ident ($input:expr, $len:expr)) => {
Expand Down Expand Up @@ -207,3 +209,67 @@ fn read_stats_by_ref_returns_wrapped_reader() {
let reader = ReadStats::new(input);
assert_eq!(reader.get_ref(), &input);
}

/// a Read type that always errors
struct ReadFails;

impl ReadFails {
const MESSAGE: &'static str = "this reader always fails";
}

impl Read for ReadFails {
fn read(&mut self, _buf: &mut [u8]) -> Result<usize> {
Err(Error::other(Self::MESSAGE))
}
}

/// a Write type that always errors
struct WriteFails;

impl WriteFails {
const MESSAGE: &'static str = "this writer always fails";
}

impl Write for WriteFails {
fn write(&mut self, _buf: &[u8]) -> Result<usize> {
Err(Error::other(Self::MESSAGE))
}

fn flush(&mut self) -> Result<()> {
Err(Error::other(Self::MESSAGE))
}
}

#[test]
#[ignore]
fn read_propagates_errors() {
use paasio::ReadStats;

let mut reader = ReadStats::new(ReadFails);
let mut buffer = Vec::new();

let Err(e) = reader.read(&mut buffer) else {
panic!("read error not propagated")
};

// check that the correct error was returned
assert_eq!(e.kind(), ErrorKind::Other);
assert_eq!(e.get_ref().unwrap().to_string(), ReadFails::MESSAGE);
}

#[test]
#[ignore]
fn write_propagates_errors() {
use paasio::WriteStats;

let mut writer = WriteStats::new(WriteFails);
let buffer = "This text won't be written";

let Err(e) = writer.write(buffer.as_bytes()) else {
panic!("write error not propagated")
};

// check that the correct error is returned
assert_eq!(e.kind(), ErrorKind::Other);
assert_eq!(e.get_ref().unwrap().to_string(), WriteFails::MESSAGE);
}