rustapi_ws/
heartbeat.rs

1//! WebSocket heartbeat configuration
2//!
3//! This module provides configuration for WebSocket heartbeats (ping/pong).
4
5use std::time::Duration;
6
7/// Configuration for WebSocket heartbeats
8#[derive(Debug, Clone, Copy)]
9pub struct WsHeartbeatConfig {
10    /// Interval between ping messages
11    pub interval: Duration,
12    /// Timeout for waiting for a pong response
13    pub timeout: Duration,
14}
15
16impl Default for WsHeartbeatConfig {
17    fn default() -> Self {
18        Self {
19            interval: Duration::from_secs(30),
20            timeout: Duration::from_secs(10),
21        }
22    }
23}
24
25impl WsHeartbeatConfig {
26    /// Create a new heartbeat config with default values
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Set the ping interval
32    pub fn interval(mut self, interval: Duration) -> Self {
33        self.interval = interval;
34        self
35    }
36
37    /// Set the pong timeout
38    pub fn timeout(mut self, timeout: Duration) -> Self {
39        self.timeout = timeout;
40        self
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use proptest::prelude::*;
48
49    proptest! {
50        // **Property 12: Heartbeat interval accuracy (Configuration)**
51        // **Validates: Requirements 6.1, 6.4**
52        //
53        // Ensures that the configuration builder correctly sets the interval and timeout,
54        // and that they remain positive and valid durations.
55        #[test]
56        fn test_heartbeat_config_roundtrip(
57            interval_secs in 1u64..3600,
58            timeout_secs in 1u64..3600,
59        ) {
60            let config = WsHeartbeatConfig::new()
61                .interval(Duration::from_secs(interval_secs))
62                .timeout(Duration::from_secs(timeout_secs));
63
64            prop_assert_eq!(config.interval, Duration::from_secs(interval_secs));
65            prop_assert_eq!(config.timeout, Duration::from_secs(timeout_secs));
66        }
67    }
68}