Skip to content

Commit 90387e5

Browse files
committed
simln-lib/test: speed up random payment deterministic test clock
Speed up this test by using our sim clock that can speed up the simulation. The downside of this clock is that we may stop one payment over/under if we try to match exactly to the number of payments that we expect in a given period of time. Instead, we generate our set of expected payment and then run the simulation for much longer than we need to get this expected list, so we should always get at least this subset. We then just assert that the payments we get exactly match this first set.
1 parent 9ea442a commit 90387e5

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

simln-lib/src/lib.rs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ async fn track_payment_result(
15921592

15931593
#[cfg(test)]
15941594
mod tests {
1595-
use crate::clock::{Clock, SimulationClock, SystemClock};
1595+
use crate::clock::{Clock, SimulationClock};
15961596
use crate::test_utils::{MockLightningNode, TestNodesResult};
15971597
use crate::{
15981598
get_payment_delay, test_utils, test_utils::LightningTestNodeBuilder, LightningError,
@@ -2031,20 +2031,20 @@ mod tests {
20312031
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
20322032

20332033
// Create simulation without a timeout.
2034-
let clock = Arc::new(SimulationClock::new(10).unwrap());
2035-
let start = clock.now();
2034+
let clock = Arc::new(SimulationClock::new(10).unwrap());
2035+
let start = clock.now();
20362036
let simulation = Simulation::new(
20372037
SimulationCfg::new(None, 100, 2.0, None, None),
20382038
network.get_client_hashmap(),
20392039
TaskTracker::new(),
2040-
clock.clone(),
2040+
clock.clone(),
20412041
shutdown_trigger,
20422042
shutdown_listener,
20432043
);
20442044

20452045
// Run the simulation
20462046
let _ = simulation.run(&vec![activity_1, activity_2]).await;
2047-
let elapsed = clock.now().duration_since(start).unwrap();
2047+
let elapsed = clock.now().duration_since(start).unwrap();
20482048
let expected_payment_list = vec![
20492049
network.nodes[1].pubkey,
20502050
network.nodes[3].pubkey,
@@ -2062,7 +2062,7 @@ mod tests {
20622062
// - from activity_1 there are 5 payments with a wait_time of 2s -> 10s
20632063
// - from activity_2 there are 5 payments with a wait_time of 4s -> 20s
20642064
// - but the wait time is interleave between the payments.
2065-
// Since we're running with a sped up clock, we allow a little more leeway.
2065+
// Since we're running with a sped up clock, we allow a little more leeway.
20662066
assert!(
20672067
elapsed <= Duration::from_secs(30),
20682068
"Simulation should have run no more than 30, took {:?}",
@@ -2099,55 +2099,71 @@ mod tests {
20992099

21002100
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
21012101

2102-
// Create simulation with a defined seed.
2102+
// Create simulation with a defined seed, and limit it to running for 45 seconds.
2103+
let clock = Arc::new(SimulationClock::new(20).unwrap());
21032104
let simulation = Simulation::new(
2104-
SimulationCfg::new(Some(25), 100, 2.0, None, Some(42)),
2105+
SimulationCfg::new(Some(45), 100, 2.0, None, Some(42)),
21052106
network.get_client_hashmap(),
21062107
TaskTracker::new(),
2107-
Arc::new(SystemClock {}),
2108+
clock.clone(),
21082109
shutdown_trigger,
21092110
shutdown_listener,
21102111
);
21112112

2112-
// Run the simulation
2113-
let start = std::time::Instant::now();
2113+
let start = clock.now();
21142114
let _ = simulation.run(&[]).await;
2115-
let elapsed = start.elapsed();
2115+
let elapsed = clock.now().duration_since(start).unwrap();
21162116

21172117
assert!(
2118-
elapsed >= Duration::from_secs(25),
2119-
"Simulation should have run at least for 25s, took {:?}",
2118+
elapsed >= Duration::from_secs(45),
2119+
"Simulation should have run at least for 45s, took {:?}",
21202120
elapsed
21212121
);
2122-
let expected_payment_list = vec![
2123-
pk1, pk2, pk1, pk1, pk1, pk3, pk3, pk3, pk4, pk3, pk2, pk1, pk4,
2124-
];
21252122

2126-
assert!(
2127-
payments_list.lock().unwrap().as_ref() == expected_payment_list,
2123+
// We're running with a sped up clock, so we're not going to hit exactly the same number
2124+
// of payments each time. We settle for asserting that our first 12 are deterministic.
2125+
// This ordering is set by running the simulation for 25 seconds, and we run for a total
2126+
// of 45 seconds so we can reasonably expect that we'll always get at least these 12
2127+
// payments.
2128+
let expected_payment_list =
2129+
vec![pk1, pk2, pk1, pk1, pk1, pk3, pk3, pk3, pk4, pk3, pk3, pk1];
2130+
let actual_payments: Vec<PublicKey> = payments_list
2131+
.lock()
2132+
.unwrap()
2133+
.iter()
2134+
.cloned()
2135+
.take(12)
2136+
.collect();
2137+
assert_eq!(
2138+
actual_payments, expected_payment_list,
21282139
"The expected order of payments is not correct: {:?} vs {:?}",
2129-
payments_list.lock().unwrap(),
2130-
expected_payment_list,
2140+
actual_payments, expected_payment_list,
21312141
);
2132-
21332142
// remove all the payments made in the previous execution
21342143
payments_list.lock().unwrap().clear();
21352144

21362145
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
21372146

21382147
// Create the same simulation as before but with different seed.
21392148
let simulation2 = Simulation::new(
2140-
SimulationCfg::new(Some(25), 100, 2.0, None, Some(500)),
2149+
SimulationCfg::new(Some(45), 100, 2.0, None, Some(500)),
21412150
network.get_client_hashmap(),
21422151
TaskTracker::new(),
2143-
Arc::new(SystemClock {}),
2152+
clock.clone(),
21442153
shutdown_trigger,
21452154
shutdown_listener,
21462155
);
21472156
let _ = simulation2.run(&[]).await;
21482157

2149-
assert!(
2150-
payments_list.lock().unwrap().as_ref() != expected_payment_list,
2158+
let actual_payments: Vec<PublicKey> = payments_list
2159+
.lock()
2160+
.unwrap()
2161+
.iter()
2162+
.cloned()
2163+
.take(12)
2164+
.collect();
2165+
assert_ne!(
2166+
actual_payments, expected_payment_list,
21512167
"The expected order of payments shoud be different because a different is used"
21522168
);
21532169
}

0 commit comments

Comments
 (0)