@@ -1592,7 +1592,7 @@ async fn track_payment_result(
15921592
15931593#[ cfg( test) ]
15941594mod 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,76 @@ 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+
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 20 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 20
2127+ // payments.
21222128 let expected_payment_list = vec ! [
2123- pk1, pk2, pk1, pk1, pk1, pk3, pk3, pk3, pk4, pk3, pk2, pk1, pk4,
2129+ pk1, pk2, pk1, pk1, pk1, pk3, pk3, pk3, pk4, pk3, pk3, pk1, pk4, pk2, pk2, pk2, pk2,
2130+ pk1, pk3, pk1,
21242131 ] ;
21252132
2126- assert ! (
2127- payments_list. lock( ) . unwrap( ) . as_ref( ) == expected_payment_list,
2133+ let actual_payments: Vec < PublicKey > = payments_list
2134+ . lock ( )
2135+ . unwrap ( )
2136+ . iter ( )
2137+ . cloned ( )
2138+ . take ( 20 )
2139+ . collect ( ) ;
2140+ assert_eq ! (
2141+ actual_payments,
2142+ expected_payment_list,
21282143 "The expected order of payments is not correct: {:?} vs {:?}" ,
21292144 payments_list. lock( ) . unwrap( ) ,
21302145 expected_payment_list,
21312146 ) ;
2132-
21332147 // remove all the payments made in the previous execution
21342148 payments_list. lock ( ) . unwrap ( ) . clear ( ) ;
21352149
21362150 let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
21372151
21382152 // Create the same simulation as before but with different seed.
21392153 let simulation2 = Simulation :: new (
2140- SimulationCfg :: new ( Some ( 25 ) , 100 , 2.0 , None , Some ( 500 ) ) ,
2154+ SimulationCfg :: new ( Some ( 45 ) , 100 , 2.0 , None , Some ( 500 ) ) ,
21412155 network. get_client_hashmap ( ) ,
21422156 TaskTracker :: new ( ) ,
2143- Arc :: new ( SystemClock { } ) ,
2157+ clock . clone ( ) ,
21442158 shutdown_trigger,
21452159 shutdown_listener,
21462160 ) ;
21472161 let _ = simulation2. run ( & [ ] ) . await ;
21482162
2149- assert ! (
2150- payments_list. lock( ) . unwrap( ) . as_ref( ) != expected_payment_list,
2163+ let actual_payments: Vec < PublicKey > = payments_list
2164+ . lock ( )
2165+ . unwrap ( )
2166+ . iter ( )
2167+ . cloned ( )
2168+ . take ( 20 )
2169+ . collect ( ) ;
2170+ assert_ne ! (
2171+ actual_payments, expected_payment_list,
21512172 "The expected order of payments shoud be different because a different is used"
21522173 ) ;
21532174 }
0 commit comments