Skip to content

Commit 6b241ed

Browse files
committed
move SinceEpoch to lightning::util::test_utils
1 parent 6354d8b commit 6b241ed

File tree

4 files changed

+71
-67
lines changed

4 files changed

+71
-67
lines changed

lightning-invoice/src/payment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ mod tests {
881881
#[cfg(feature = "std")]
882882
#[test]
883883
fn fails_paying_invoice_after_max_retry_timeout() {
884-
use lightning::util::time::SinceEpoch;
884+
use lightning::util::test_utils::SinceEpoch;
885885
let event_handled = core::cell::RefCell::new(false);
886886
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
887887

lightning/src/routing/scoring.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,8 @@ use util::time::Time;
12731273

12741274
#[cfg(test)]
12751275
mod tests {
1276-
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
1277-
use util::time::SinceEpoch;
1276+
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime};
1277+
use util::test_utils::SinceEpoch;
12781278

12791279
use ln::features::{ChannelFeatures, NodeFeatures};
12801280
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
@@ -1289,6 +1289,7 @@ mod tests {
12891289
use bitcoin::network::constants::Network;
12901290
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
12911291
use core::time::Duration;
1292+
use util::time::Time;
12921293
use io;
12931294

12941295
/// A scorer for testing with time that can be manually advanced.

lightning/src/util/test_utils.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
2626
use util::events;
2727
use util::logger::{Logger, Level, Record};
2828
use util::ser::{Readable, ReadableArgs, Writer, Writeable};
29+
use util::time::Time;
2930

3031
use bitcoin::blockdata::constants::genesis_block;
3132
use bitcoin::blockdata::transaction::{Transaction, TxOut};
@@ -46,6 +47,8 @@ use core::time::Duration;
4647
use sync::{Mutex, Arc};
4748
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
4849
use core::{cmp, mem};
50+
use core::cell::Cell;
51+
use core::ops::Sub;
4952
use bitcoin::bech32::u5;
5053
use chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial};
5154

@@ -725,3 +728,43 @@ impl core::fmt::Debug for OnRegisterOutput {
725728

726729
/// A scorer useful in testing, when the passage of time isn't a concern.
727730
pub type TestScorer = FixedPenaltyScorer;
731+
732+
/// Time that can be advanced manually in tests.
733+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
734+
pub struct SinceEpoch(Duration);
735+
736+
impl SinceEpoch {
737+
thread_local! {
738+
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
739+
}
740+
741+
pub fn advance(duration: Duration) {
742+
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
743+
}
744+
}
745+
746+
impl Time for SinceEpoch {
747+
fn now() -> Self {
748+
Self(Self::duration_since_epoch())
749+
}
750+
751+
fn duration_since(&self, earlier: Self) -> Duration {
752+
self.0 - earlier.0
753+
}
754+
755+
fn duration_since_epoch() -> Duration {
756+
Self::ELAPSED.with(|elapsed| elapsed.get())
757+
}
758+
759+
fn elapsed(&self) -> Duration {
760+
Self::duration_since_epoch() - self.0
761+
}
762+
}
763+
764+
impl Sub<Duration> for SinceEpoch {
765+
type Output = Self;
766+
767+
fn sub(self, other: Duration) -> Self {
768+
Self(self.0 - other)
769+
}
770+
}

lightning/src/util/time.rs

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
1010
use core::ops::Sub;
1111
use core::time::Duration;
12-
use core::cell::Cell;
1312
/// A measurement of time.
1413
pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
1514
/// Returns an instance corresponding to the current moment.
@@ -31,46 +30,6 @@ pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
3130
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3231
pub struct Eternity;
3332

34-
#[cfg(not(feature = "no-std"))]
35-
impl Time for std::time::Instant {
36-
fn now() -> Self {
37-
std::time::Instant::now()
38-
}
39-
40-
fn duration_since(&self, earlier: Self) -> Duration {
41-
self.duration_since(earlier)
42-
}
43-
44-
fn duration_since_epoch() -> Duration {
45-
use std::time::SystemTime;
46-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
47-
}
48-
fn elapsed(&self) -> Duration {
49-
std::time::Instant::elapsed(self)
50-
}
51-
}
52-
53-
#[cfg(not(feature = "no-std"))]
54-
impl Time for std::time::SystemTime {
55-
fn now() -> Self {
56-
std::time::SystemTime::now()
57-
}
58-
59-
fn duration_since(&self, earlier: Self) -> Duration {
60-
self.duration_since(earlier).unwrap()
61-
}
62-
63-
fn duration_since_epoch() -> Duration {
64-
use std::time::SystemTime;
65-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
66-
}
67-
68-
fn elapsed(&self) -> Duration {
69-
std::time::SystemTime::elapsed(self).unwrap()
70-
}
71-
}
72-
73-
7433
impl Time for Eternity {
7534
fn now() -> Self {
7635
Self
@@ -97,50 +56,51 @@ impl Sub<Duration> for Eternity {
9756
}
9857
}
9958

100-
/// Time that can be advanced manually in tests.
101-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
102-
pub struct SinceEpoch(Duration);
103-
104-
impl SinceEpoch {
105-
thread_local! {
106-
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
59+
#[cfg(not(feature = "no-std"))]
60+
impl Time for std::time::Instant {
61+
fn now() -> Self {
62+
std::time::Instant::now()
10763
}
108-
109-
pub fn advance(duration: Duration) {
110-
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
64+
65+
fn duration_since(&self, earlier: Self) -> Duration {
66+
self.duration_since(earlier)
67+
}
68+
69+
fn duration_since_epoch() -> Duration {
70+
use std::time::SystemTime;
71+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
72+
}
73+
fn elapsed(&self) -> Duration {
74+
std::time::Instant::elapsed(self)
11175
}
11276
}
11377

114-
impl Time for SinceEpoch {
78+
#[cfg(not(feature = "no-std"))]
79+
impl Time for std::time::SystemTime {
11580
fn now() -> Self {
116-
Self(Self::duration_since_epoch())
81+
std::time::SystemTime::now()
11782
}
11883

11984
fn duration_since(&self, earlier: Self) -> Duration {
120-
self.0 - earlier.0
85+
self.duration_since(earlier).unwrap()
12186
}
12287

12388
fn duration_since_epoch() -> Duration {
124-
Self::ELAPSED.with(|elapsed| elapsed.get())
89+
use std::time::SystemTime;
90+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
12591
}
12692

12793
fn elapsed(&self) -> Duration {
128-
Self::duration_since_epoch() - self.0
94+
std::time::SystemTime::elapsed(self).unwrap()
12995
}
13096
}
13197

132-
impl Sub<Duration> for SinceEpoch {
133-
type Output = Self;
134-
135-
fn sub(self, other: Duration) -> Self {
136-
Self(self.0 - other)
137-
}
138-
}
13998

14099

141100
#[cfg(test)]
142101
pub mod tests {
143-
use util::time::{Eternity, SinceEpoch, Time};
102+
use util::time::{Time, Eternity};
103+
use util::test_utils::SinceEpoch;
144104

145105
use core::time::Duration;
146106

0 commit comments

Comments
 (0)