1use std::time::Duration;
2
3#[derive(Debug, Clone)]
7pub struct RedisConfig {
8 pub max_connections: usize,
10 pub connect_timeout: u64,
12 pub wait_timeout: u64,
14 pub enable_logging: bool,
16}
17
18impl Default for RedisConfig {
19 fn default() -> Self {
27 Self {
28 max_connections: 10,
29 connect_timeout: 5,
30 wait_timeout: 10,
31 enable_logging: false,
32 }
33 }
34}
35
36impl RedisConfig {
37 pub fn new(
54 max_connections: usize,
55 connect_timeout: u64,
56 wait_timeout: u64,
57 enable_logging: bool,
58 ) -> Self {
59 Self {
60 max_connections,
61 connect_timeout,
62 wait_timeout,
63 enable_logging,
64 }
65 }
66
67 #[allow(dead_code)]
69 pub(crate) fn connect_timeout_duration(&self) -> Duration {
70 Duration::from_secs(self.connect_timeout)
71 }
72
73 #[allow(dead_code)]
75 pub(crate) fn wait_timeout_duration(&self) -> Duration {
76 Duration::from_secs(self.wait_timeout)
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_default_config() {
86 let config = RedisConfig::default();
87 assert_eq!(config.max_connections, 10);
88 assert_eq!(config.connect_timeout, 5);
89 assert_eq!(config.wait_timeout, 10);
90 assert!(!config.enable_logging);
91 }
92
93 #[test]
94 fn test_new_config() {
95 let config = RedisConfig::new(20, 10, 15, true);
96 assert_eq!(config.max_connections, 20);
97 assert_eq!(config.connect_timeout, 10);
98 assert_eq!(config.wait_timeout, 15);
99 assert!(config.enable_logging);
100 }
101
102 #[test]
103 fn test_connect_timeout_duration() {
104 let config = RedisConfig::new(10, 5, 10, false);
105 assert_eq!(config.connect_timeout_duration(), Duration::from_secs(5));
106 }
107
108 #[test]
109 fn test_wait_timeout_duration() {
110 let config = RedisConfig::new(10, 5, 10, false);
111 assert_eq!(config.wait_timeout_duration(), Duration::from_secs(10));
112 }
113
114 #[test]
115 fn test_clone() {
116 let config = RedisConfig::new(15, 8, 12, true);
117 let cloned = config.clone();
118 assert_eq!(config.max_connections, cloned.max_connections);
119 assert_eq!(config.connect_timeout, cloned.connect_timeout);
120 assert_eq!(config.wait_timeout, cloned.wait_timeout);
121 assert_eq!(config.enable_logging, cloned.enable_logging);
122 }
123
124 #[test]
125 fn test_debug() {
126 let config = RedisConfig::default();
127 let debug_str = format!("{:?}", config);
128 assert!(debug_str.contains("RedisConfig"));
129 assert!(debug_str.contains("max_connections"));
130 }
131}