}
/// A state in which time has no meaning.
+#[derive(Debug, PartialEq, Eq)]
pub struct Eternity;
impl Time for Eternity {
})
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::{Eternity, ScoringParameters, ScorerUsingTime, Time};
+
+ use routing::Score;
+ use routing::network_graph::NodeId;
+
+ use bitcoin::secp256k1::PublicKey;
+ use core::cell::Cell;
+ use core::ops::Sub;
+ use core::time::Duration;
+
+ /// Time that can be advanced manually in tests.
+ #[derive(Debug, PartialEq, Eq)]
+ struct SinceEpoch(Duration);
+
+ impl SinceEpoch {
+ thread_local! {
+ static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
+ }
+
+ fn advance(duration: Duration) {
+ Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
+ }
+ }
+
+ impl Time for SinceEpoch {
+ fn now() -> Self {
+ Self(Self::duration_since_epoch())
+ }
+
+ fn duration_since_epoch() -> Duration {
+ Self::ELAPSED.with(|elapsed| elapsed.get())
+ }
+
+ fn elapsed(&self) -> Duration {
+ Self::duration_since_epoch() - self.0
+ }
+ }
+
+ impl Sub<Duration> for SinceEpoch {
+ type Output = Self;
+
+ fn sub(self, other: Duration) -> Self {
+ Self(self.0 - other)
+ }
+ }
+
+ #[test]
+ fn time_passes_when_advanced() {
+ let now = SinceEpoch::now();
+ assert_eq!(now.elapsed(), Duration::from_secs(0));
+
+ SinceEpoch::advance(Duration::from_secs(1));
+ SinceEpoch::advance(Duration::from_secs(1));
+
+ let elapsed = now.elapsed();
+ let later = SinceEpoch::now();
+
+ assert_eq!(elapsed, Duration::from_secs(2));
+ assert_eq!(later - elapsed, now);
+ }
+
+ #[test]
+ fn time_never_passes_in_an_eternity() {
+ let now = Eternity::now();
+ let elapsed = now.elapsed();
+ let later = Eternity::now();
+
+ assert_eq!(now.elapsed(), Duration::from_secs(0));
+ assert_eq!(later - elapsed, now);
+ }
+}