@@ -1592,7 +1592,7 @@ async fn track_payment_result(
1592
1592
1593
1593
#[ cfg( test) ]
1594
1594
mod tests {
1595
- use crate :: clock:: { Clock , SimulationClock , SystemClock } ;
1595
+ use crate :: clock:: { Clock , SimulationClock } ;
1596
1596
use crate :: test_utils:: { MockLightningNode , TestNodesResult } ;
1597
1597
use crate :: {
1598
1598
get_payment_delay, test_utils, test_utils:: LightningTestNodeBuilder , LightningError ,
@@ -2031,20 +2031,20 @@ mod tests {
2031
2031
let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
2032
2032
2033
2033
// 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 ( ) ;
2036
2036
let simulation = Simulation :: new (
2037
2037
SimulationCfg :: new ( None , 100 , 2.0 , None , None ) ,
2038
2038
network. get_client_hashmap ( ) ,
2039
2039
TaskTracker :: new ( ) ,
2040
- clock. clone ( ) ,
2040
+ clock. clone ( ) ,
2041
2041
shutdown_trigger,
2042
2042
shutdown_listener,
2043
2043
) ;
2044
2044
2045
2045
// Run the simulation
2046
2046
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 ( ) ;
2048
2048
let expected_payment_list = vec ! [
2049
2049
network. nodes[ 1 ] . pubkey,
2050
2050
network. nodes[ 3 ] . pubkey,
@@ -2062,7 +2062,7 @@ mod tests {
2062
2062
// - from activity_1 there are 5 payments with a wait_time of 2s -> 10s
2063
2063
// - from activity_2 there are 5 payments with a wait_time of 4s -> 20s
2064
2064
// - 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.
2066
2066
assert ! (
2067
2067
elapsed <= Duration :: from_secs( 30 ) ,
2068
2068
"Simulation should have run no more than 30, took {:?}" ,
@@ -2099,55 +2099,71 @@ mod tests {
2099
2099
2100
2100
let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
2101
2101
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 ( ) ) ;
2103
2104
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 ) ) ,
2105
2106
network. get_client_hashmap ( ) ,
2106
2107
TaskTracker :: new ( ) ,
2107
- Arc :: new ( SystemClock { } ) ,
2108
+ clock . clone ( ) ,
2108
2109
shutdown_trigger,
2109
2110
shutdown_listener,
2110
2111
) ;
2111
2112
2112
- // Run the simulation
2113
- let start = std:: time:: Instant :: now ( ) ;
2113
+ let start = clock. now ( ) ;
2114
2114
let _ = simulation. run ( & [ ] ) . await ;
2115
- let elapsed = start. elapsed ( ) ;
2115
+ let elapsed = clock . now ( ) . duration_since ( start) . unwrap ( ) ;
2116
2116
2117
2117
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 {:?}" ,
2120
2120
elapsed
2121
2121
) ;
2122
- let expected_payment_list = vec ! [
2123
- pk1, pk2, pk1, pk1, pk1, pk3, pk3, pk3, pk4, pk3, pk2, pk1, pk4,
2124
- ] ;
2125
2122
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,
2128
2139
"The expected order of payments is not correct: {:?} vs {:?}" ,
2129
- payments_list. lock( ) . unwrap( ) ,
2130
- expected_payment_list,
2140
+ actual_payments, expected_payment_list,
2131
2141
) ;
2132
-
2133
2142
// remove all the payments made in the previous execution
2134
2143
payments_list. lock ( ) . unwrap ( ) . clear ( ) ;
2135
2144
2136
2145
let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
2137
2146
2138
2147
// Create the same simulation as before but with different seed.
2139
2148
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 ) ) ,
2141
2150
network. get_client_hashmap ( ) ,
2142
2151
TaskTracker :: new ( ) ,
2143
- Arc :: new ( SystemClock { } ) ,
2152
+ clock . clone ( ) ,
2144
2153
shutdown_trigger,
2145
2154
shutdown_listener,
2146
2155
) ;
2147
2156
let _ = simulation2. run ( & [ ] ) . await ;
2148
2157
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,
2151
2167
"The expected order of payments shoud be different because a different is used"
2152
2168
) ;
2153
2169
}
0 commit comments