Skip to main content

camel_api/
delayer.rs

1use std::time::Duration;
2
3#[derive(Debug, Clone)]
4pub struct DelayConfig {
5    pub delay_ms: u64,
6    pub dynamic_header: Option<String>,
7}
8
9impl DelayConfig {
10    pub fn new(delay_ms: u64) -> Self {
11        Self {
12            delay_ms,
13            dynamic_header: None,
14        }
15    }
16
17    pub fn with_dynamic_header(mut self, header: impl Into<String>) -> Self {
18        self.dynamic_header = Some(header.into());
19        self
20    }
21
22    pub fn from_duration(duration: Duration) -> Self {
23        Self {
24            delay_ms: duration.as_millis() as u64,
25            dynamic_header: None,
26        }
27    }
28
29    pub fn from_duration_with_header(duration: Duration, header: impl Into<String>) -> Self {
30        Self {
31            delay_ms: duration.as_millis() as u64,
32            dynamic_header: Some(header.into()),
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn new_sets_delay_no_header() {
43        let cfg = DelayConfig::new(500);
44        assert_eq!(cfg.delay_ms, 500);
45        assert!(cfg.dynamic_header.is_none());
46    }
47
48    #[test]
49    fn with_dynamic_header_sets_header() {
50        let cfg = DelayConfig::new(200).with_dynamic_header("X-Delay");
51        assert_eq!(cfg.delay_ms, 200);
52        assert_eq!(cfg.dynamic_header.as_deref(), Some("X-Delay"));
53    }
54
55    #[test]
56    fn from_duration_converts_ms() {
57        let cfg = DelayConfig::from_duration(Duration::from_millis(1500));
58        assert_eq!(cfg.delay_ms, 1500);
59        assert!(cfg.dynamic_header.is_none());
60    }
61
62    #[test]
63    fn from_duration_with_header() {
64        let cfg = DelayConfig::from_duration_with_header(Duration::from_secs(2), "MyHeader");
65        assert_eq!(cfg.delay_ms, 2000);
66        assert_eq!(cfg.dynamic_header.as_deref(), Some("MyHeader"));
67    }
68
69    #[test]
70    fn clone_preserves_values() {
71        let cfg = DelayConfig::new(100).with_dynamic_header("H");
72        let cloned = cfg.clone();
73        assert_eq!(cloned.delay_ms, 100);
74        assert_eq!(cloned.dynamic_header.as_deref(), Some("H"));
75    }
76}