mithril_dmq/test/double/timestamp.rs
1use mithril_common::StdResult;
2
3use crate::model::UnixTimestampProvider;
4
5/// Fake implementation of a Unix current timestamp provider. For testing purposes only.
6pub struct FakeUnixTimestampProvider(u64);
7
8impl FakeUnixTimestampProvider {
9 /// Creates a new `FakeUnixTimestampProvider` with the given timestamp.
10 pub fn new(timestamp: u64) -> Self {
11 Self(timestamp)
12 }
13
14 /// Computes the maximum timestamp that can be used with the given TTL and builds a new FakeUnixTimestampProvider.
15 ///
16 /// This is useful to create messages that are valid for the maximum possible time.
17 pub fn max_timestamp_for_ttl(ttl: u16) -> Self {
18 Self(u32::MAX as u64 - ttl as u64 - 1)
19 }
20}
21
22impl UnixTimestampProvider for FakeUnixTimestampProvider {
23 fn current_timestamp(&self) -> StdResult<u64> {
24 Ok(self.0)
25 }
26}