Skip to content

Commit 6f3a6ce

Browse files
committed
simln-lib/test: update random test generation to use virtual clock
1 parent b20a9f5 commit 6f3a6ce

1 file changed

Lines changed: 12 additions & 24 deletions

File tree

simln-lib/src/lib.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,53 +2148,41 @@ mod tests {
21482148
);
21492149
}
21502150

2151-
#[tokio::test]
2151+
// Run on a paused runtime so that the payment interval sleeps auto-advance rather than
2152+
// consuming real time; elapsed time is measured against the simulation's virtual clock.
2153+
#[tokio::test(start_paused = true)]
21522154
async fn test_deterministic_payments_events_random() {
2153-
let pk1 = PublicKey::from_str(
2154-
"02f6dc1fcf3431f461ff5e6d870f286e134b064fddd3795a98d5903c55e76cfa8c",
2155-
)
2156-
.unwrap();
2157-
let pk2 = PublicKey::from_str(
2158-
"0363a5321c2778e2f13f4d7602a84e866824551b4bd31fdfc66e044a274863d647",
2159-
)
2160-
.unwrap();
2161-
let pk3 = PublicKey::from_str(
2162-
"037b0c8a943a6bc3ebb5b56ed1c377a1d3e6b68e9231ad9bf7afd45ba37374f7a3",
2163-
)
2164-
.unwrap();
2165-
let pk4 = PublicKey::from_str(
2166-
"026389dd21b3611bf4bb78f8939d5912f3175107ff9c14b6881bb4bef0e7c6e905",
2167-
)
2168-
.unwrap();
2155+
let pks = fixed_test_pubkeys();
2156+
let (pk1, pk2, pk3, pk4) = (pks[0], pks[1], pks[2], pks[3]);
21692157

21702158
let (network, payments_list) =
2171-
setup_test_nodes_for_testing_deterministic_events(Some(vec![pk1, pk2, pk3, pk4])).await;
2159+
setup_test_nodes_for_testing_deterministic_events(Some(pks)).await;
21722160

21732161
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
21742162

2163+
let clock = Arc::new(SimulationClock::new(SystemTime::now()));
2164+
21752165
// Create simulation with a defined seed.
21762166
let simulation = Simulation::new(
21772167
SimulationCfg::new(Some(25), 100, 2.0, None, Some(42)),
21782168
network.get_client_hashmap(),
21792169
TaskTracker::new(),
2180-
Arc::new(SimulationClock::new(SystemTime::now())),
2170+
clock.clone(),
21812171
shutdown_trigger,
21822172
shutdown_listener,
21832173
);
21842174

21852175
// Run the simulation
2186-
let start = std::time::Instant::now();
2176+
let start = clock.now();
21872177
let _ = simulation.run(&[]).await;
2188-
let elapsed = start.elapsed();
2178+
let elapsed = clock.now().duration_since(start).unwrap();
21892179

21902180
assert!(
21912181
elapsed >= Duration::from_secs(25),
21922182
"Simulation should have run at least for 25s, took {:?}",
21932183
elapsed
21942184
);
2195-
let expected_payment_list = vec![
2196-
pk1, pk2, pk1, pk1, pk1, pk3, pk3, pk3, pk4, pk3, pk2, pk1, pk4,
2197-
];
2185+
let expected_payment_list = vec![pk1, pk1, pk2, pk2, pk4, pk2, pk2, pk2, pk3, pk3];
21982186

21992187
assert!(
22002188
payments_list.lock().unwrap().as_ref() == expected_payment_list,

0 commit comments

Comments
 (0)