#[derive(Debug, Clone)]
pub struct PoolStats {
pub available: usize,
pub active: usize,
pub total: usize,
}
impl PoolStats {
#[inline]
pub fn checked_out(&self) -> usize {
self.active.saturating_sub(self.available)
}
#[inline]
pub fn has_available(&self) -> bool {
self.available > 0
}
#[inline]
pub fn is_empty(&self) -> bool {
self.active == 0
}
}
impl std::fmt::Display for PoolStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"PoolStats {{ available: {}, active: {}, total: {} }}",
self.available, self.active, self.total
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pool_stats_structure() {
let stats = PoolStats {
available: 5,
active: 3,
total: 8,
};
assert_eq!(
stats.available, 5,
"Available browsers should be accessible"
);
assert_eq!(stats.active, 3, "Active browsers should be accessible");
assert_eq!(stats.total, 8, "Total browsers should be accessible");
}
#[test]
fn test_checked_out() {
let stats = PoolStats {
available: 2,
active: 5,
total: 5,
};
assert_eq!(stats.checked_out(), 3);
}
#[test]
fn test_checked_out_saturating() {
let stats = PoolStats {
available: 10,
active: 5,
total: 5,
};
assert_eq!(stats.checked_out(), 0); }
#[test]
fn test_has_available() {
let stats_with = PoolStats {
available: 1,
active: 1,
total: 1,
};
assert!(stats_with.has_available());
let stats_without = PoolStats {
available: 0,
active: 1,
total: 1,
};
assert!(!stats_without.has_available());
}
#[test]
fn test_is_empty() {
let empty = PoolStats {
available: 0,
active: 0,
total: 0,
};
assert!(empty.is_empty());
let not_empty = PoolStats {
available: 0,
active: 1,
total: 1,
};
assert!(!not_empty.is_empty());
}
#[test]
fn test_display() {
let stats = PoolStats {
available: 3,
active: 5,
total: 5,
};
assert_eq!(
stats.to_string(),
"PoolStats { available: 3, active: 5, total: 5 }"
);
}
#[test]
fn test_clone() {
let stats = PoolStats {
available: 3,
active: 5,
total: 5,
};
let cloned = stats.clone();
assert_eq!(cloned.available, stats.available);
assert_eq!(cloned.active, stats.active);
assert_eq!(cloned.total, stats.total);
}
#[test]
fn test_debug() {
let stats = PoolStats {
available: 3,
active: 5,
total: 5,
};
let debug_str = format!("{:?}", stats);
assert!(debug_str.contains("PoolStats"));
assert!(debug_str.contains("available"));
}
}