Skip to content

Commit

Permalink
Unit testing connection and request timeouts
Browse files Browse the repository at this point in the history
Closes #297
  • Loading branch information
ecton committed May 16, 2023
1 parent 09c69d2 commit bd862dd
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/bonsaidb-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ impl AsyncClient {

match result {
Ok(response) => response?,
Err(_) => Err(Error::RequestTimeout),
Err(_) => Err(Error::request_timeout()),
}
}

Expand Down Expand Up @@ -1041,7 +1041,7 @@ async fn disconnect_pending_requests(
drop(
pending
.responder
.send(Err(pending_error.take().unwrap_or(Error::Disconnected))),
.send(Err(pending_error.take().unwrap_or(Error::disconnected()))),
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bonsaidb-client/src/client/quic_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn connect_and_process(
{
Ok(Ok(result)) => result,
Ok(Err(err)) => return Err((Some(initial_request), Some(err))),
Err(_) => return Err((Some(initial_request), Some(Error::ConnectTimeout))),
Err(_) => return Err((Some(initial_request), Some(Error::connect_timeout()))),
};

let outstanding_requests = OutstandingRequestMapHandle::default();
Expand Down Expand Up @@ -108,7 +108,7 @@ async fn connect_and_process(
request_receiver,
payload_sender
),
async { request_processor.await.map_err(|_| Error::Disconnected)? }
async { request_processor.await.map_err(|_| Error::disconnected())? }
) {
let mut pending_error = Some(err);
// Our socket was disconnected, clear the outstanding requests before returning.
Expand Down Expand Up @@ -136,7 +136,7 @@ async fn process_requests(
drop(payload_sender.finish());

// Return an error to make sure try_join returns.
Err(Error::Disconnected)
Err(Error::disconnected())
}

pub async fn process(
Expand All @@ -149,7 +149,7 @@ pub async fn process(
super::process_response_payload(payload, &outstanding_requests, &custom_apis).await;
}

Err(Error::Disconnected)
Err(Error::disconnected())
}

async fn connect(
Expand Down
4 changes: 2 additions & 2 deletions crates/bonsaidb-client/src/client/tungstenite_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(super) async fn reconnecting_client_loop(
continue;
}
Err(_) => {
drop(request.responder.send(Err(Error::ConnectTimeout)));
drop(request.responder.send(Err(Error::connect_timeout())));
continue;
}
};
Expand Down Expand Up @@ -115,7 +115,7 @@ async fn request_sender(
);
}

Err(Error::Disconnected)
Err(Error::disconnected())
}

#[allow(clippy::collapsible_else_if)] // not possible due to cfg statement
Expand Down
38 changes: 24 additions & 14 deletions crates/bonsaidb-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bonsaidb_core::arc_bytes::serde::Bytes;
use bonsaidb_core::networking;
use bonsaidb_core::schema::Name;

/// Errors related to working with the BonsaiDb client.
Expand All @@ -17,10 +18,6 @@ pub enum Error {
#[error("invalid url: '{0}'")]
InvalidUrl(String),

/// The connection was interrupted.
#[error("unexpected disconnection")]
Disconnected,

/// The connection was interrupted.
#[error("unexpected disconnection")]
Core(#[from] bonsaidb_core::Error),
Expand All @@ -38,33 +35,46 @@ pub enum Error {
/// The server is incompatible with this version of the client.
#[error("server incompatible with client protocol version")]
ProtocolVersionMismatch,
}

/// A timeout occurred while connecting to the server.
#[error("connection to server timed out")]
ConnectTimeout,
/// A timeout occurred while waiting for a response from the server.
#[error("request timed out")]
RequestTimeout,
impl Error {
pub(crate) fn disconnected() -> Self {
Self::Core(bonsaidb_core::Error::Networking(
networking::Error::Disconnected,
))
}

pub(crate) fn request_timeout() -> Self {
Self::Core(bonsaidb_core::Error::Networking(
networking::Error::RequestTimeout,
))
}

pub(crate) fn connect_timeout() -> Self {
Self::Core(bonsaidb_core::Error::Networking(
networking::Error::ConnectTimeout,
))
}
}

impl<T> From<flume::SendError<T>> for Error {
fn from(_: flume::SendError<T>) -> Self {
Self::Disconnected
Self::disconnected()
}
}

impl From<flume::RecvTimeoutError> for Error {
fn from(err: flume::RecvTimeoutError) -> Self {
match err {
flume::RecvTimeoutError::Timeout => Self::RequestTimeout,
flume::RecvTimeoutError::Disconnected => Self::Disconnected,
flume::RecvTimeoutError::Timeout => Self::request_timeout(),
flume::RecvTimeoutError::Disconnected => Self::disconnected(),
}
}
}

impl From<flume::RecvError> for Error {
fn from(_: flume::RecvError) -> Self {
Self::Disconnected
Self::disconnected()
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/bonsaidb-core/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@ pub enum Error {
#[error("unexpected response: {0}")]
UnexpectedResponse(String),

/// A timeout occurred while connecting to the server.
#[error("connection timeout")]
ConnectTimeout,

/// A timeout occurred waiting on a request to complete.
#[error("request timeout")]
RequestTimeout,

/// The connection was interrupted.
#[error("unexpected disconnection")]
Disconnected,
Expand Down
Loading

0 comments on commit bd862dd

Please sign in to comment.