Skip to content

Commit 1fe5adf

Browse files
committed
Add unit tests for session errors
1 parent ea77308 commit 1fe5adf

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

crates/hotfix/src/session/error.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,71 @@ impl<T> InternalSendResultExt<T> for Result<T, InternalSendError> {
134134
self.map_err(|source| SessionOperationError::Send { source, context })
135135
}
136136
}
137+
138+
#[cfg(test)]
139+
mod tests {
140+
use super::*;
141+
142+
fn test_store_error() -> StoreError {
143+
StoreError::Initialization("test".into())
144+
}
145+
146+
#[test]
147+
fn mpsc_send_error_converts_to_session_gone() {
148+
let err: SendError = tokio::sync::mpsc::error::SendError(()).into();
149+
assert!(matches!(err, SendError::SessionGone));
150+
}
151+
152+
#[tokio::test]
153+
async fn oneshot_recv_error_converts_to_session_gone() {
154+
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
155+
drop(tx);
156+
// await the receiver to get RecvError (not TryRecvError)
157+
let recv_err = rx.await.unwrap_err();
158+
159+
let err: SendError = recv_err.into();
160+
assert!(matches!(err, SendError::SessionGone));
161+
}
162+
163+
#[test]
164+
fn internal_send_error_disconnected_converts_to_send_error() {
165+
let internal_err = InternalSendError::Disconnected;
166+
let send_err: SendError = internal_err.into();
167+
assert!(matches!(send_err, SendError::Disconnected));
168+
}
169+
170+
#[test]
171+
fn internal_send_error_persist_converts_to_send_error() {
172+
let internal_err = InternalSendError::Persist(test_store_error());
173+
let send_err: SendError = internal_err.into();
174+
assert!(matches!(send_err, SendError::Persist(_)));
175+
}
176+
177+
#[test]
178+
fn internal_send_error_sequence_number_converts_to_send_error() {
179+
let internal_err = InternalSendError::SequenceNumber(test_store_error());
180+
let send_err: SendError = internal_err.into();
181+
assert!(matches!(send_err, SendError::SequenceNumber(_)));
182+
}
183+
184+
#[test]
185+
fn with_send_context_converts_error() {
186+
let result: Result<(), InternalSendError> =
187+
Err(InternalSendError::Persist(test_store_error()));
188+
189+
let op_err = result.with_send_context("heartbeat").unwrap_err();
190+
match op_err {
191+
SessionOperationError::Send { context, .. } => {
192+
assert_eq!(context, "heartbeat");
193+
}
194+
_ => panic!("expected SessionOperationError::Send"),
195+
}
196+
}
197+
198+
#[test]
199+
fn with_send_context_passes_through_ok() {
200+
let result: Result<u64, InternalSendError> = Ok(42);
201+
let op_result = result.with_send_context("heartbeat");
202+
assert_eq!(op_result.unwrap(), 42);
203+
}
204+
}

0 commit comments

Comments
 (0)