#![allow(dead_code)]
use super::error::ClientError;
pub fn should_exit_immediately(error: &ClientError) -> bool {
matches!(error, ClientError::DuplicateClientId(_))
}
pub fn should_attempt_reconnect(
error: &ClientError,
current_attempt: u32,
max_attempts: u32,
) -> bool {
if should_exit_immediately(error) {
return false;
}
current_attempt < max_attempts
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_exit_immediately_with_duplicate_client_id() {
let error = ClientError::DuplicateClientId("alice".to_string());
let result = should_exit_immediately(&error);
assert!(result);
}
#[test]
fn test_should_exit_immediately_with_connection_error() {
let error = ClientError::ConnectionError("network error".to_string());
let result = should_exit_immediately(&error);
assert!(!result);
}
#[test]
fn test_should_attempt_reconnect_with_duplicate_client_id() {
let error = ClientError::DuplicateClientId("alice".to_string());
let result = should_attempt_reconnect(&error, 0, 5);
assert!(!result);
}
#[test]
fn test_should_attempt_reconnect_within_limit() {
let error = ClientError::ConnectionError("network error".to_string());
let result = should_attempt_reconnect(&error, 3, 5);
assert!(result);
}
#[test]
fn test_should_attempt_reconnect_at_limit() {
let error = ClientError::ConnectionError("network error".to_string());
let result = should_attempt_reconnect(&error, 5, 5);
assert!(!result);
}
#[test]
fn test_should_attempt_reconnect_first_attempt() {
let error = ClientError::ConnectionError("network error".to_string());
let result = should_attempt_reconnect(&error, 0, 5);
assert!(result);
}
#[test]
fn test_should_attempt_reconnect_one_before_limit() {
let error = ClientError::ConnectionError("network error".to_string());
let result = should_attempt_reconnect(&error, 4, 5);
assert!(result);
}
}