Skip to content

Commit

Permalink
v0.3.3 - Adjust shutdown on disconnect error
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-leinz committed Dec 13, 2023
1 parent 1a35332 commit 134cc4b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stubborn-io"
version = "0.3.2"
version = "0.3.3"
authors = ["David Raifaizen <david.raifaizen@protonmail.com>"]
edition = "2021"
description = "io traits/structs that automatically recover from potential disconnections/interruptions."
Expand Down
20 changes: 16 additions & 4 deletions src/tokio/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,24 @@ enum Status<T, C> {
FailedAndExhausted, // the way one feels after programming in dynamically typed languages
}

#[inline]
fn poll_err<T>(
kind: ErrorKind,
reason: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> Poll<io::Result<T>> {
let io_err = io::Error::new(kind, reason);
Poll::Ready(Err(io_err))
}

fn exhausted_err<T>() -> Poll<io::Result<T>> {
let io_err = io::Error::new(
poll_err(
ErrorKind::NotConnected,
"Disconnected. Connection attempts have been exhausted.",
);
Poll::Ready(Err(io_err))
)
}

fn disconnected_err<T>() -> Poll<io::Result<T>> {
poll_err(ErrorKind::NotConnected, "Underlying I/O is disconnected.")
}

impl<T, C> Deref for StubbornIo<T, C> {
Expand Down Expand Up @@ -381,7 +393,7 @@ where

poll
}
Status::Disconnected(_) => exhausted_err(),
Status::Disconnected(_) => disconnected_err(),
Status::FailedAndExhausted => exhausted_err(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ async fn back_to_back_shutdown_attempts() {
let elapsed = tokio::time::timeout(Duration::from_secs(5), connection.shutdown()).await;

let result = elapsed.unwrap();
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), std::io::ErrorKind::NotConnected);
}

0 comments on commit 134cc4b

Please sign in to comment.