use std::fmt;
use crate::Instant;
use crate::display_ext::DisplayInstantExt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct SendStamp<I> {
pub(crate) time: I,
pub(crate) heartbeat_round: u64,
}
impl<I> SendStamp<I> {
pub(crate) fn new(time: I, heartbeat_round: u64) -> Self {
Self { time, heartbeat_round }
}
}
impl<I> fmt::Display for SendStamp<I>
where I: Instant
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}(heartbeat_round={})", self.time.display(), self.heartbeat_round)
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::SendStamp;
use crate::engine::testing::UTConfig;
use crate::type_config::TypeConfigExt;
#[test]
fn test_order_by_time_then_heartbeat_round() {
let t1 = UTConfig::<()>::now();
let t2 = t1 + Duration::from_millis(1);
let replication_at_t1 = SendStamp::new(t1, 0);
let heartbeat_at_t1 = SendStamp::new(t1, 1);
let replication_at_t2 = SendStamp::new(t2, 0);
assert!(
heartbeat_at_t1 > replication_at_t1,
"a heartbeat round breaks a tie in time"
);
assert!(replication_at_t2 > heartbeat_at_t1, "a later time wins over any round");
}
}