use wallclock where possible in tests

This commit is contained in:
trinity-1686a 2022-02-26 00:33:44 +01:00
parent 96daba7747
commit f06b256010
18 changed files with 64 additions and 23 deletions

View File

@ -265,6 +265,7 @@ mod test {
let dirinfo = (&netdir).into();
let config = PathConfig::default();
let guards: OptDummyGuardMgr<'_> = None;
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
for _ in 0..1000 {
@ -310,6 +311,7 @@ mod test {
.unwrap();
let dirinfo = (&netdir).into();
let guards: OptDummyGuardMgr<'_> = None;
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let config = PathConfig::default();
@ -357,6 +359,7 @@ mod test {
let dirinfo = (&netdir).into();
let guards: OptDummyGuardMgr<'_> = None;
let config = PathConfig::default();
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
// With target ports

View File

@ -728,6 +728,7 @@ mod test {
let di = (&netdir).into();
let config = crate::PathConfig::default();
let guards: OptDummyGuardMgr<'_> = None;
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
// Only doing basic tests for now. We'll test the path
@ -805,6 +806,7 @@ mod test {
let di = (&netdir).into();
let config = crate::PathConfig::default();
let guards: OptDummyGuardMgr<'_> = None;
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let (path, usage, _, _) = TargetCircUsage::TimeoutTesting

View File

@ -478,6 +478,7 @@ mod test {
) -> (Result<()>, Vec<u8>) {
// We don't need to do anything fancy here, since we aren't simulating
// a timeout.
#[allow(clippy::disallowed_methods)]
let mock_time = MockSleepProvider::new(std::time::SystemTime::now());
let mut output = Vec::new();

View File

@ -492,6 +492,7 @@ mod test {
.unwrap();
let d2 = b"blah blah blah 12 blah blah blah";
#[allow(clippy::disallowed_methods)]
let d3 = SystemTime::now();
let mut req = ConsensusRequest::default();

View File

@ -353,9 +353,11 @@ mod test {
use std::convert::TryInto;
use std::sync::Mutex;
use tor_netdoc::doc::microdesc::MdDigest;
use tor_rtcompat::SleepProvider;
#[test]
fn week() {
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_day = Duration::new(86400, 0);
@ -500,14 +502,13 @@ mod test {
fn all_in_cache() {
// Let's try bootstrapping when everything is in the cache.
tor_rtcompat::test_with_one_runtime!(|rt| async {
let now = rt.wallclock();
let (_tempdir, mgr) = new_mgr(rt);
{
let mut store = mgr.store_if_rw().unwrap().lock().unwrap();
for h in [H1, H2, H3, H4, H5] {
store
.store_microdescs(&[("ignore", &h)], SystemTime::now())
.unwrap();
store.store_microdescs(&[("ignore", &h)], now).unwrap();
}
}
let mgr = Arc::new(mgr);
@ -533,14 +534,13 @@ mod test {
// Let's try bootstrapping with all of phase1 and part of
// phase 2 in cache.
tor_rtcompat::test_with_one_runtime!(|rt| async {
let now = rt.wallclock();
let (_tempdir, mgr) = new_mgr(rt);
{
let mut store = mgr.store_if_rw().unwrap().lock().unwrap();
for h in [H1, H2, H3] {
store
.store_microdescs(&[("ignore", &h)], SystemTime::now())
.unwrap();
store.store_microdescs(&[("ignore", &h)], now).unwrap();
}
}
{

View File

@ -657,6 +657,7 @@ mod test {
#[test]
fn dir_status_basics() {
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let hour = Duration::new(3600, 0);

View File

@ -959,6 +959,7 @@ mod test {
use std::time::Duration;
use tempfile::TempDir;
use tor_netdoc::doc::{authcert::AuthCertKeyIds, netstatus::Lifetime};
use tor_rtcompat::SleepProvider;
pub(crate) fn new_mgr<R: Runtime>(runtime: R) -> (TempDir, DirMgr<R>) {
let dir = TempDir::new().unwrap();
@ -984,12 +985,12 @@ mod test {
#[test]
fn load_and_store_internals() {
tor_rtcompat::test_with_one_runtime!(|rt| async {
let (_tempdir, mgr) = new_mgr(rt);
let now = SystemTime::now();
let now = rt.wallclock();
let tomorrow = now + Duration::from_secs(86400);
let later = tomorrow + Duration::from_secs(86400);
let (_tempdir, mgr) = new_mgr(rt);
// Seed the storage with a bunch of junk.
let d1 = [5_u8; 32];
let d2 = [7; 32];
@ -1099,12 +1100,12 @@ mod test {
#[test]
fn make_consensus_request() {
tor_rtcompat::test_with_one_runtime!(|rt| async {
let (_tempdir, mgr) = new_mgr(rt);
let now = SystemTime::now();
let now = rt.wallclock();
let tomorrow = now + Duration::from_secs(86400);
let later = tomorrow + Duration::from_secs(86400);
let (_tempdir, mgr) = new_mgr(rt);
// Try with an empty store.
let req = mgr
.make_consensus_request(ConsensusFlavor::Microdesc)
@ -1194,6 +1195,9 @@ mod test {
#[test]
fn expand_response() {
tor_rtcompat::test_with_one_runtime!(|rt| async {
let now = rt.wallclock();
let day = Duration::from_secs(86400);
let (_tempdir, mgr) = new_mgr(rt);
// Try a simple request: nothing should happen.
@ -1217,8 +1221,6 @@ mod test {
// we can ask for a diff.
{
let mut store = mgr.store.lock().unwrap();
let now = SystemTime::now();
let day = Duration::from_secs(86400);
let d_in = [0x99; 32]; // This one, we can fake.
let cmeta = ConsensusMeta::new(
Lifetime::new(now, now + day, now + 2 * day).unwrap(),

View File

@ -709,6 +709,7 @@ mod test {
fn basic_guard() -> Guard {
let id = basic_id();
let ports = vec!["127.0.0.7:7777".parse().unwrap()];
#[allow(clippy::disallowed_methods)]
let added = SystemTime::now();
Guard::new(id, ports, added)
}
@ -848,9 +849,11 @@ mod test {
fn record_success() {
let t1 = Instant::now() - Duration::from_secs(10);
// has to be in the future, since the guard's "added_at" time is based on now.
let t2 = SystemTime::now() + Duration::from_secs(300 * 86400);
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let t2 = now + Duration::from_secs(300 * 86400);
let t3 = Instant::now() + Duration::from_secs(310 * 86400);
let t4 = SystemTime::now() + Duration::from_secs(320 * 86400);
let t4 = now + Duration::from_secs(320 * 86400);
let mut g = basic_guard();
g.record_failure(t1, true);
@ -906,6 +909,7 @@ mod test {
fn expiration() {
const DAY: Duration = Duration::from_secs(24 * 60 * 60);
let params = GuardParams::default();
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let g = basic_guard();
@ -937,6 +941,7 @@ mod test {
.unwrap_if_sufficient()
.unwrap();
let params = GuardParams::default();
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
// Construct a guard from a relay from the netdir.
@ -993,6 +998,7 @@ mod test {
.unwrap();
//let params = GuardParams::default();
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
// Try a guard that isn't in the netdir at all.
@ -1075,6 +1081,7 @@ mod test {
let mut g = basic_guard();
let params = GuardParams::default();
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let _ignore = g.record_success(now, &params);

View File

@ -826,6 +826,7 @@ mod test {
let mut samples: Vec<HashSet<GuardId>> = Vec::new();
for _ in 0..3 {
let mut guards = GuardSet::default();
#[allow(clippy::disallowed_methods)]
guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
assert_eq!(guards.guards.len(), params.min_filtered_sample_size);
assert_eq!(guards.confirmed.len(), 0);
@ -838,10 +839,14 @@ mod test {
assert!(relay.is_flagged_guard());
assert!(relay.is_dir_cache());
assert!(guards.contains_relay(&relay));
assert!(!guard.is_expired(&params, SystemTime::now()));
#[allow(clippy::disallowed_methods)]
{
assert!(!guard.is_expired(&params, SystemTime::now()));
}
}
// Make sure that the sample doesn't expand any further.
#[allow(clippy::disallowed_methods)]
guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
assert_eq!(guards.guards.len(), params.min_filtered_sample_size);
guards.assert_consistency();
@ -861,8 +866,10 @@ mod test {
min_filtered_sample_size: 5,
..GuardParams::default()
};
#[allow(clippy::disallowed_methods)]
let t1 = SystemTime::now();
let t2 = SystemTime::now() + Duration::from_secs(20);
let t2 = t1 + Duration::from_secs(20);
let mut guards = GuardSet::default();
guards.extend_sample_as_needed(t1, &params, &netdir);
@ -897,9 +904,10 @@ mod test {
n_primary: 4,
..GuardParams::default()
};
#[allow(clippy::disallowed_methods)]
let t1 = SystemTime::now();
let t2 = SystemTime::now() + Duration::from_secs(20);
let t3 = SystemTime::now() + Duration::from_secs(30);
let t2 = t1 + Duration::from_secs(20);
let t3 = t2 + Duration::from_secs(30);
let mut guards = GuardSet::default();
guards.extend_sample_as_needed(t1, &params, &netdir);
@ -943,6 +951,7 @@ mod test {
fn expiration() {
let netdir = netdir();
let params = GuardParams::default();
#[allow(clippy::disallowed_methods)]
let t1 = SystemTime::now();
let mut guards = GuardSet::default();
@ -977,6 +986,7 @@ mod test {
n_primary: 2,
..GuardParams::default()
};
#[allow(clippy::disallowed_methods)]
let st1 = SystemTime::now();
let i1 = Instant::now();
let sec = Duration::from_secs(1);
@ -1100,6 +1110,7 @@ mod test {
max_sample_bw_fraction: 1.0,
..GuardParams::default()
};
#[allow(clippy::disallowed_methods)]
let mut st = SystemTime::now();
let mut inst = Instant::now();
let sec = Duration::from_secs(1);
@ -1142,6 +1153,7 @@ mod test {
let mut guards = GuardSet::default();
#[allow(clippy::disallowed_methods)]
guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
guards.select_primary_guards(&params);
@ -1179,11 +1191,13 @@ mod test {
};
let usage = crate::GuardUsageBuilder::default().build().unwrap();
let mut guards = GuardSet::default();
#[allow(clippy::disallowed_methods)]
guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
guards.select_primary_guards(&params);
assert_eq!(guards.primary.len(), 2);
let (_kind, p_id1) = guards.pick_guard(&usage, &params).unwrap();
#[allow(clippy::disallowed_methods)]
guards.record_success(&p_id1, &params, SystemTime::now());
assert_eq!(guards.missing_primary_microdescriptors(&netdir), 0);

View File

@ -66,6 +66,7 @@ mod test {
#[test]
fn test_randomize_time() {
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_hour = Duration::from_secs(3600);
let ten_sec = Duration::from_secs(10);

View File

@ -138,6 +138,7 @@ where
f | RelayFlags::EXIT | RelayFlags::GUARD,
];
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_day = Duration::new(86400, 0);
let mut bld = MdConsensus::builder();

View File

@ -619,6 +619,7 @@ mod test {
#[test]
fn weightset_from_consensus() {
use rand::Rng;
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_hour = Duration::new(3600, 0);
let mut rng = rand::thread_rng();

View File

@ -146,6 +146,7 @@ mod test {
#[test]
fn simple_cert() {
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_hour = Duration::new(3600, 0);
let later = now + one_hour * 2;
@ -166,6 +167,7 @@ mod test {
#[test]
fn failing_cert() {
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_hour = Duration::new(3600, 0);
let later = now + one_hour * 2;

View File

@ -377,6 +377,7 @@ mod test {
#[test]
fn consensus() {
#[allow(clippy::disallowed_methods)]
let now = SystemTime::now();
let one_hour = Duration::new(3600, 0);

View File

@ -385,7 +385,7 @@ mod test {
use native_tls_crate as native_tls;
use std::io::Result as IoResult;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::time::{Duration, Instant, SystemTime};
use std::time::{Duration, Instant};
// Test "sleep" with a tiny delay, and make sure that at least that
// much delay happens.
@ -436,14 +436,14 @@ mod test {
let rt = runtime.clone();
runtime.block_on(async {
let i1 = Instant::now();
let now = SystemTime::now();
let now = runtime.wallclock();
let one_millis = Duration::from_millis(1);
let one_millis_later = now + one_millis;
rt.sleep_until_wallclock(one_millis_later).await;
let i2 = Instant::now();
let newtime = SystemTime::now();
let newtime = runtime.wallclock();
assert!(newtime >= one_millis_later);
assert!(i2 - i1 >= one_millis);
});

View File

@ -203,6 +203,7 @@ mod test {
}
let minute = Duration::from_secs(60);
let second = Duration::from_secs(1);
#[allow(clippy::disallowed_methods)]
let start = SystemTime::now();
let target = start + 30 * minute;

View File

@ -439,6 +439,7 @@ mod test {
#[test]
fn basics_of_time_travel() {
#[allow(clippy::disallowed_methods)]
let w1 = SystemTime::now();
let sp = MockSleepProvider::new(w1);
let i1 = sp.now();
@ -461,6 +462,7 @@ mod test {
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
#[allow(clippy::disallowed_methods)]
let sp = MockSleepProvider::new(SystemTime::now());
let one_hour = Duration::new(3600, 0);

View File

@ -17,6 +17,7 @@ fn timeouts() {
oneshot::Sender<()>,
Timeout<oneshot::Receiver<()>, tor_rtmock::time::Sleeping>,
) {
#[allow(clippy::disallowed_methods)]
let start = SystemTime::now();
let (send, recv) = oneshot::channel::<()>();
let mock_sp = MockSleepProvider::new(start);