Skip to content

Commit b20a9f5

Browse files
committed
simln-lib/test: update defined activities test to use sped up clock
This test previously required some sleeps, which slowed our test down. Update to use our virtual clock.
1 parent 6d9a610 commit b20a9f5

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

simln-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ ntest = "0.9.0"
4242
mockall = "0.13.1"
4343
futures = "0.3.31"
4444
tempfile = "3"
45-
tokio = { version = "1.31.0", features = ["macros", "rt-multi-thread"] }
45+
tokio = { version = "1.31.0", features = ["macros", "rt-multi-thread", "test-util"] }

simln-lib/src/lib.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,7 @@ async fn track_payment_result(
16091609

16101610
#[cfg(test)]
16111611
mod tests {
1612-
use crate::clock::SimulationClock;
1612+
use crate::clock::{Clock, SimulationClock};
16131613
use crate::test_utils::{MockLightningNode, TestNodesResult};
16141614
use crate::{
16151615
get_payment_delay, test_utils, test_utils::LightningTestNodeBuilder, LightningError,
@@ -2004,6 +2004,21 @@ mod tests {
20042004
});
20052005
}
20062006

2007+
/// Fixed pubkeys for payment ordering tests. Payment events that are scheduled for the same
2008+
/// instant are tie-broken on their source pubkey, so node keys must be stable across runs
2009+
/// for the order of payments to be deterministic.
2010+
fn fixed_test_pubkeys() -> Vec<PublicKey> {
2011+
[
2012+
"02f6dc1fcf3431f461ff5e6d870f286e134b064fddd3795a98d5903c55e76cfa8c",
2013+
"0363a5321c2778e2f13f4d7602a84e866824551b4bd31fdfc66e044a274863d647",
2014+
"037b0c8a943a6bc3ebb5b56ed1c377a1d3e6b68e9231ad9bf7afd45ba37374f7a3",
2015+
"026389dd21b3611bf4bb78f8939d5912f3175107ff9c14b6881bb4bef0e7c6e905",
2016+
]
2017+
.iter()
2018+
.map(|pk| PublicKey::from_str(pk).unwrap())
2019+
.collect()
2020+
}
2021+
20072022
#[allow(clippy::type_complexity)]
20082023
/// Helper to create and configure mock nodes for testing
20092024
async fn setup_test_nodes_for_testing_deterministic_events(
@@ -2052,10 +2067,12 @@ mod tests {
20522067
(network, payments_list)
20532068
}
20542069

2055-
#[tokio::test]
2070+
// Run on a paused runtime so that the payment interval sleeps auto-advance rather than
2071+
// consuming real time; elapsed time is measured against the simulation's virtual clock.
2072+
#[tokio::test(start_paused = true)]
20562073
async fn test_deterministic_payments_events_defined_activities() {
20572074
let (network, payments_list) =
2058-
setup_test_nodes_for_testing_deterministic_events(None).await;
2075+
setup_test_nodes_for_testing_deterministic_events(Some(fixed_test_pubkeys())).await;
20592076

20602077
// Define two activities
20612078
// Activity 1: From node_1 to node_2
@@ -2080,29 +2097,34 @@ mod tests {
20802097

20812098
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
20822099

2100+
let clock = Arc::new(SimulationClock::new(SystemTime::now()));
2101+
20832102
// Create simulation without a timeout.
20842103
let simulation = Simulation::new(
20852104
SimulationCfg::new(None, 100, 2.0, None, None),
20862105
network.get_client_hashmap(),
20872106
TaskTracker::new(),
2088-
Arc::new(SimulationClock::new(SystemTime::now())),
2107+
clock.clone(),
20892108
shutdown_trigger,
20902109
shutdown_listener,
20912110
);
20922111

20932112
// Run the simulation
2094-
let start = std::time::Instant::now();
2113+
let start = clock.now();
20952114
let _ = simulation.run(&vec![activity_1, activity_2]).await;
2096-
let elapsed = start.elapsed();
2115+
let elapsed = clock.now().duration_since(start).unwrap();
20972116

2117+
// Activity 1 pays at t = 2, 4, 6, 8, 10 and activity 2 at t = 4, 8, 12, 16, 20. At the
2118+
// instants where both activities fire (t = 4, 8), activity 1 dispatches first because
2119+
// simultaneous events are ordered by source pubkey and its source sorts first.
20982120
let expected_payment_list = vec![
2121+
network.nodes[1].pubkey,
20992122
network.nodes[1].pubkey,
21002123
network.nodes[3].pubkey,
21012124
network.nodes[1].pubkey,
21022125
network.nodes[1].pubkey,
21032126
network.nodes[3].pubkey,
21042127
network.nodes[1].pubkey,
2105-
network.nodes[1].pubkey,
21062128
network.nodes[3].pubkey,
21072129
network.nodes[3].pubkey,
21082130
network.nodes[3].pubkey,
@@ -2120,7 +2142,9 @@ mod tests {
21202142

21212143
assert!(
21222144
payments_list.lock().unwrap().as_ref() == expected_payment_list,
2123-
"The expected order of payments is not correct"
2145+
"The expected order of payments is not correct: {:?} vs {:?}",
2146+
payments_list.lock().unwrap(),
2147+
expected_payment_list,
21242148
);
21252149
}
21262150

0 commit comments

Comments
 (0)