Skip to main content

swf_core/models/
timeout.rs

1use crate::models::duration::*;
2use serde::{Deserialize, Serialize};
3
4/// Represents the definition of a timeout
5#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct TimeoutDefinition {
7    /// Gets/sets the duration after which to timeout
8    pub after: OneOfDurationOrIso8601Expression,
9}
10
11define_one_of_or_reference!(
12    /// Represents a value that can be either a TimeoutDefinition or a reference to a TimeoutDefinition
13    OneOfTimeoutDefinitionOrReference, Timeout(TimeoutDefinition)
14);
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[test]
21    fn test_timeout_inline_duration() {
22        let json = r#"{"after": "PT30S"}"#;
23        let timeout: TimeoutDefinition = serde_json::from_str(json).unwrap();
24        match timeout.after {
25            OneOfDurationOrIso8601Expression::Iso8601Expression(expr) => assert_eq!(expr, "PT30S"),
26            _ => panic!("Expected Iso8601Expression variant"),
27        }
28    }
29
30    #[test]
31    fn test_timeout_reference() {
32        let json = r#""myTimeout""#;
33        let ref_val: OneOfTimeoutDefinitionOrReference = serde_json::from_str(json).unwrap();
34        match ref_val {
35            OneOfTimeoutDefinitionOrReference::Reference(r) => assert_eq!(r, "myTimeout"),
36            _ => panic!("Expected Reference variant"),
37        }
38    }
39
40    #[test]
41    fn test_timeout_roundtrip() {
42        let json = r#"{"after": "PT10M"}"#;
43        let timeout: TimeoutDefinition = serde_json::from_str(json).unwrap();
44        let serialized = serde_json::to_string(&timeout).unwrap();
45        let deserialized: TimeoutDefinition = serde_json::from_str(&serialized).unwrap();
46        assert_eq!(timeout, deserialized);
47    }
48
49    // Additional tests matching Go SDK's timeout_test.go
50
51    #[test]
52    fn test_timeout_inline_duration_struct() {
53        let json = r#"{"after": {"days": 1, "hours": 2}}"#;
54        let timeout: TimeoutDefinition = serde_json::from_str(json).unwrap();
55        match &timeout.after {
56            OneOfDurationOrIso8601Expression::Duration(d) => {
57                assert_eq!(d.days, Some(1));
58                assert_eq!(d.hours, Some(2));
59            }
60            _ => panic!("Expected Duration variant"),
61        }
62    }
63
64    #[test]
65    fn test_timeout_iso8601_with_milliseconds() {
66        let json = r#"{"after": "PT2S500MS"}"#;
67        let timeout: TimeoutDefinition = serde_json::from_str(json).unwrap();
68        match &timeout.after {
69            OneOfDurationOrIso8601Expression::Iso8601Expression(expr) => {
70                assert_eq!(expr, "PT2S500MS");
71            }
72            _ => panic!("Expected Iso8601Expression variant"),
73        }
74    }
75
76    #[test]
77    fn test_timeout_reference_roundtrip() {
78        let ref_val = OneOfTimeoutDefinitionOrReference::Reference("myTimeout".to_string());
79        let serialized = serde_json::to_string(&ref_val).unwrap();
80        assert_eq!(serialized, r#""myTimeout""#);
81        let deserialized: OneOfTimeoutDefinitionOrReference =
82            serde_json::from_str(&serialized).unwrap();
83        assert_eq!(ref_val, deserialized);
84    }
85
86    #[test]
87    fn test_timeout_or_reference_inline() {
88        let json = r#"{"after": {"days": 1, "hours": 2}}"#;
89        let tor: OneOfTimeoutDefinitionOrReference = serde_json::from_str(json).unwrap();
90        match tor {
91            OneOfTimeoutDefinitionOrReference::Timeout(t) => match t.after {
92                OneOfDurationOrIso8601Expression::Duration(d) => {
93                    assert_eq!(d.days, Some(1));
94                    assert_eq!(d.hours, Some(2));
95                }
96                _ => panic!("Expected Duration variant"),
97            },
98            _ => panic!("Expected Timeout variant"),
99        }
100    }
101
102    #[test]
103    fn test_timeout_or_reference_string() {
104        let json = r#""some-timeout-reference""#;
105        let tor: OneOfTimeoutDefinitionOrReference = serde_json::from_str(json).unwrap();
106        match tor {
107            OneOfTimeoutDefinitionOrReference::Reference(r) => {
108                assert_eq!(r, "some-timeout-reference");
109            }
110            _ => panic!("Expected Reference variant"),
111        }
112    }
113
114    // Additional tests matching Go SDK's timeout_test.go
115
116    #[test]
117    fn test_timeout_iso8601_full() {
118        // Matches Go SDK: Valid ISO 8601 duration
119        let json = r#"{"after": "P3DT4H5M6S250MS"}"#;
120        let timeout: TimeoutDefinition = serde_json::from_str(json).unwrap();
121        match &timeout.after {
122            OneOfDurationOrIso8601Expression::Iso8601Expression(expr) => {
123                assert_eq!(expr, "P3DT4H5M6S250MS");
124            }
125            _ => panic!("Expected Iso8601Expression variant"),
126        }
127    }
128
129    #[test]
130    fn test_timeout_or_reference_roundtrip_inline() {
131        // Matches Go SDK: Valid Timeout
132        let json = r#"{"after": {"days": 1, "hours": 2}}"#;
133        let tor: OneOfTimeoutDefinitionOrReference = serde_json::from_str(json).unwrap();
134        let serialized = serde_json::to_string(&tor).unwrap();
135        let deserialized: OneOfTimeoutDefinitionOrReference =
136            serde_json::from_str(&serialized).unwrap();
137        assert_eq!(tor, deserialized);
138    }
139
140    #[test]
141    fn test_timeout_or_reference_roundtrip_string_ref() {
142        // Matches Go SDK: Valid Ref
143        let ref_val =
144            OneOfTimeoutDefinitionOrReference::Reference("some-timeout-reference".to_string());
145        let serialized = serde_json::to_string(&ref_val).unwrap();
146        assert_eq!(serialized, r#""some-timeout-reference""#);
147        let deserialized: OneOfTimeoutDefinitionOrReference =
148            serde_json::from_str(&serialized).unwrap();
149        assert_eq!(ref_val, deserialized);
150    }
151
152    #[test]
153    fn test_timeout_inline_roundtrip() {
154        let json = r#"{"after": {"days": 1, "hours": 2, "minutes": 30}}"#;
155        let timeout: TimeoutDefinition = serde_json::from_str(json).unwrap();
156        let serialized = serde_json::to_string(&timeout).unwrap();
157        let deserialized: TimeoutDefinition = serde_json::from_str(&serialized).unwrap();
158        assert_eq!(timeout, deserialized);
159    }
160
161    #[test]
162    fn test_timeout_default() {
163        let timeout = TimeoutDefinition::default();
164        match &timeout.after {
165            OneOfDurationOrIso8601Expression::Duration(d) => {
166                assert_eq!(d.total_milliseconds(), 0);
167            }
168            _ => panic!("Expected default Duration variant"),
169        }
170    }
171
172    #[test]
173    fn test_oneof_timeout_default() {
174        let oneof = OneOfTimeoutDefinitionOrReference::default();
175        match oneof {
176            OneOfTimeoutDefinitionOrReference::Timeout(_) => {}
177            _ => panic!("Expected default Timeout variant"),
178        }
179    }
180}