Skip to main content

carrier_core/
sim.rs

1//! What the simulated peers of both interfaces share: how long they take to answer.
2//!
3//! The peers (`wbci::sim`, `spri::sim`) read no clock. A peer answers an inbound call with its
4//! technical receipt after [`Latency::receipt`] and sends a business answer [`Latency::answer`]
5//! after the event it answers. The documents set the deadlines, not the moment inside them;
6//! the defaults are well inside every deadline: a receipt after 2 seconds (WBCI escalates after
7//! 2 hours, Anl. 1 V5.4.1 §2.6.1; S/PRI expects it within 30 seconds, S/PRI 4.4 §11.2.3), an
8//! answer after 1 hour (the shortest business deadline is 24 hours counted on working days,
9//! Anl. 1 V5.4.1 §2.9–§2.11).
10
11use jiff::SignedDuration;
12
13/// How long a simulated peer takes: to send a receipt, and to answer.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct Latency {
16    /// From an inbound call to its technical receipt (TEQ).
17    pub receipt: SignedDuration,
18    /// From the event a business message answers to the message.
19    pub answer: SignedDuration,
20}
21
22impl Latency {
23    /// 2 seconds to the receipt, 1 hour to an answer.
24    pub const DEFAULT: Self = Self { receipt: SignedDuration::from_secs(2), answer: SignedDuration::from_hours(1) };
25
26    /// The receipt delay, at least zero and at most `max` (the receipt's deadline), so a
27    /// receipt never leaves its deadline.
28    #[must_use]
29    pub fn receipt_within(&self, max: SignedDuration) -> SignedDuration {
30        self.receipt.clamp(SignedDuration::ZERO, max.max(SignedDuration::ZERO))
31    }
32
33    /// The answer delay, at least zero.
34    #[must_use]
35    pub fn answer(&self) -> SignedDuration {
36        self.answer.max(SignedDuration::ZERO)
37    }
38}
39
40impl Default for Latency {
41    fn default() -> Self {
42        Self::DEFAULT
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn defaults_and_clamping() {
52        let l = Latency::default();
53        assert_eq!(l.receipt, SignedDuration::from_secs(2));
54        assert_eq!(l.answer(), SignedDuration::from_hours(1));
55        let slow = Latency { receipt: SignedDuration::from_mins(5), answer: SignedDuration::from_secs(-1) };
56        assert_eq!(slow.receipt_within(SignedDuration::from_secs(30)), SignedDuration::from_secs(30));
57        assert_eq!(slow.answer(), SignedDuration::ZERO);
58        let negative = Latency { receipt: SignedDuration::from_secs(-3), ..l };
59        assert_eq!(negative.receipt_within(SignedDuration::from_secs(30)), SignedDuration::ZERO);
60    }
61}