use std::time::Duration;
use crate::{DEFAULT_KEEP_ALIVE_INTERVAL, DEFAULT_KEEP_ALIVE_TIMEOUT};
#[derive(Debug, Default, Clone)]
pub enum ConnectionKeepAlive {
#[default]
Disabled,
ActiveConnections {
interval: Duration,
timeout: Duration,
},
ActiveAndIdleConnections {
interval: Duration,
timeout: Duration,
},
}
impl ConnectionKeepAlive {
#[cfg_attr(test, mutants::skip)] #[must_use]
pub fn disabled() -> Self {
Self::Disabled
}
#[must_use]
pub fn active_connections(interval: impl Into<Option<Duration>>, timeout: impl Into<Option<Duration>>) -> Self {
Self::ActiveConnections {
interval: interval.into().unwrap_or(DEFAULT_KEEP_ALIVE_INTERVAL),
timeout: timeout.into().unwrap_or(DEFAULT_KEEP_ALIVE_TIMEOUT),
}
}
#[must_use]
pub fn active_and_idle_connections(interval: impl Into<Option<Duration>>, timeout: impl Into<Option<Duration>>) -> Self {
Self::ActiveAndIdleConnections {
interval: interval.into().unwrap_or(DEFAULT_KEEP_ALIVE_INTERVAL),
timeout: timeout.into().unwrap_or(DEFAULT_KEEP_ALIVE_TIMEOUT),
}
}
}
#[cfg(test)]
mod tests {
use std::fmt::Debug;
use insta::assert_debug_snapshot;
use super::*;
#[cfg_attr(miri, ignore)]
#[test]
fn assert_connection_keep_alive_type() {
static_assertions::assert_impl_all!(
ConnectionKeepAlive: Send,
Sync,
Clone,
Debug,
Default
);
}
#[cfg_attr(miri, ignore)]
#[test]
fn connection_keep_alive_default() {
assert_debug_snapshot!(ConnectionKeepAlive::default());
}
#[test]
fn connection_keep_alive_disabled() {
assert!(matches!(ConnectionKeepAlive::disabled(), ConnectionKeepAlive::Disabled));
}
#[cfg_attr(miri, ignore)]
#[test]
fn connection_keep_alive_active_connections() {
assert_debug_snapshot!(ConnectionKeepAlive::active_connections(
Duration::from_secs(10),
Duration::from_secs(15)
));
assert_debug_snapshot!(ConnectionKeepAlive::active_connections(None, None));
assert_debug_snapshot!(ConnectionKeepAlive::active_connections(
Some(Duration::from_secs(5)),
Some(Duration::from_secs(5))
));
}
#[cfg_attr(miri, ignore)]
#[test]
fn connection_keep_alive_active_and_idle_connections() {
assert_debug_snapshot!(ConnectionKeepAlive::active_and_idle_connections(
Duration::from_secs(10),
Duration::from_secs(15)
));
assert_debug_snapshot!(ConnectionKeepAlive::active_and_idle_connections(None, None));
assert_debug_snapshot!(ConnectionKeepAlive::active_and_idle_connections(
Some(Duration::from_secs(5)),
Some(Duration::from_secs(5))
));
}
}