1use std::time::Duration;
4
5include!("timeouts.generated.rs");
6
7pub fn script_operation(target: &str, payload: &str) -> Option<Operation> {
8 let forwarded = FORWARD_PREFIXES
9 .iter()
10 .find_map(|prefix| target.strip_prefix(prefix));
11 let target = forwarded.unwrap_or(target);
12 if STORED_SCRIPT_SAVE.contains(&target) {
13 return Some(Operation::ScriptSave);
14 }
15 let saving = SOURCE_SCRIPT_SAVE.contains(&target);
16 if !saving && !SOURCE_SCRIPT_VALIDATION.contains(&target) {
17 return None;
18 }
19 let root: serde_json::Value = serde_json::from_str(payload).ok()?;
20 let input = if forwarded.is_some() {
21 root.get("call")?.get("input")?
22 } else if target.starts_with("/app/") {
23 root.get("input")?
24 } else {
25 &root
26 };
27 let scripts = input.get("script")?.get("scripts")?.as_object()?;
28 (!scripts.is_empty()).then_some(if saving {
29 Operation::ScriptSave
30 } else {
31 Operation::ScriptValidation
32 })
33}
34
35pub fn ordinary_operation(target: &str) -> Operation {
36 let target = FORWARD_PREFIXES
37 .iter()
38 .find_map(|prefix| target.strip_prefix(prefix))
39 .unwrap_or(target);
40 ORDINARY_OVERRIDES
41 .iter()
42 .find(|(value, _)| *value == target)
43 .map_or(Operation::CoreRequest, |(_, operation)| *operation)
44}
45
46pub fn request_operation(target: &str, payload: &str) -> Operation {
47 script_operation(target, payload).unwrap_or_else(|| ordinary_operation(target))
48}
49
50pub fn reply_timeout(operation: Operation, profile: WaitProfile) -> Duration {
51 operation.timeout() + profile.grace(operation)
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn event_processing_is_one_bounded_execution_slice() {
60 assert_eq!(Operation::EventProcessing.id(), "eventProcessing");
61 assert_eq!(
62 Operation::EventProcessing.timeout(),
63 Duration::from_secs(60)
64 );
65 }
66
67 #[test]
68 fn shared_selection_and_wait_fixtures() {
69 let cases: serde_json::Value = serde_json::from_str(include_str!(
70 "../fixtures/operation-timeouts.conformance.json"
71 ))
72 .unwrap();
73 for case in cases.as_array().unwrap() {
74 let operation = request_operation(
75 case["target"].as_str().unwrap(),
76 &case["payload"].to_string(),
77 );
78 assert_eq!(
79 operation.id(),
80 case["operation"].as_str().unwrap(),
81 "{case}"
82 );
83 assert_eq!(
84 operation.timeout().as_millis(),
85 case["totalMs"].as_u64().unwrap() as u128
86 );
87 if let Some(wait) = case["lionWaitMs"].as_u64() {
88 assert_eq!(
89 reply_timeout(operation, WaitProfile::LionForward).as_millis(),
90 wait as u128
91 );
92 }
93 }
94 }
95}