@@ -1984,6 +1984,26 @@ mod tests {
19841984 const EVENT_DEADLINE : Duration =
19851985 Duration :: from_millis ( 5 * ( FRESHNESS_TIMER . as_millis ( ) as u64 ) ) ;
19861986
1987+ /// Reads a directory and returns only non-`.tmp` files.
1988+ /// The file system may return files in any order, and during persistence
1989+ /// operations there may be temporary `.tmp` files present.
1990+ fn list_monitor_files ( dir : & str ) -> Vec < std:: fs:: DirEntry > {
1991+ std:: fs:: read_dir ( dir)
1992+ . unwrap ( )
1993+ . filter_map ( |entry| {
1994+ let entry = entry. unwrap ( ) ;
1995+ let path_str = entry. path ( ) . to_str ( ) . unwrap ( ) . to_lowercase ( ) ;
1996+ // Skip any .tmp files that may exist during persistence.
1997+ // On Windows, ReplaceFileW creates backup files with .TMP (uppercase).
1998+ if path_str. ends_with ( ".tmp" ) {
1999+ None
2000+ } else {
2001+ Some ( entry)
2002+ }
2003+ } )
2004+ . collect ( )
2005+ }
2006+
19872007 #[ derive( Clone , Hash , PartialEq , Eq ) ]
19882008 struct TestDescriptor { }
19892009 impl SocketDescriptor for TestDescriptor {
@@ -3787,30 +3807,20 @@ mod tests {
37873807 ) ;
37883808
37893809 let dir = format ! ( "{}_persister_1/monitors" , & persist_dir) ;
3790- let mut mons = std:: fs:: read_dir ( & dir) . unwrap ( ) ;
3791- let mut mon = mons. next ( ) . unwrap ( ) . unwrap ( ) ;
3792- if mon. path ( ) . to_str ( ) . unwrap ( ) . ends_with ( ".tmp" ) {
3793- mon = mons. next ( ) . unwrap ( ) . unwrap ( ) ;
3794- assert_eq ! ( mon. path( ) . extension( ) , None ) ;
3795- }
3796- assert ! ( mons. next( ) . is_none( ) ) ;
3810+ let mut mons = list_monitor_files ( & dir) ;
3811+ assert_eq ! ( mons. len( ) , 1 ) ;
3812+ let mon = mons. pop ( ) . unwrap ( ) ;
37973813
37983814 // Because the channel wasn't funded, we'll archive the ChannelMonitor immedaitely after
37993815 // its force-closed (at least on node B, which didn't put their money into it).
38003816 nodes[ 1 ] . node . force_close_all_channels_broadcasting_latest_txn ( "" . to_owned ( ) ) ;
38013817 loop {
3802- let mut mons = std:: fs:: read_dir ( & dir) . unwrap ( ) ;
3803- if let Some ( new_mon) = mons. next ( ) {
3804- let mut new_mon = new_mon. unwrap ( ) ;
3805- if new_mon. path ( ) . to_str ( ) . unwrap ( ) . ends_with ( ".tmp" ) {
3806- new_mon = mons. next ( ) . unwrap ( ) . unwrap ( ) ;
3807- assert_eq ! ( new_mon. path( ) . extension( ) , None ) ;
3808- }
3809- assert_eq ! ( new_mon. path( ) , mon. path( ) ) ;
3810- assert ! ( mons. next( ) . is_none( ) ) ;
3811- } else {
3818+ let mons = list_monitor_files ( & dir) ;
3819+ if mons. is_empty ( ) {
38123820 break ;
38133821 }
3822+ assert_eq ! ( mons. len( ) , 1 ) ;
3823+ assert_eq ! ( mons[ 0 ] . path( ) , mon. path( ) ) ;
38143824 }
38153825
38163826 bp. stop ( ) . unwrap ( ) ;
@@ -3855,30 +3865,20 @@ mod tests {
38553865 ) ) ;
38563866
38573867 let dir = format ! ( "{}_persister_1/monitors" , & persist_dir) ;
3858- let mut mons = std:: fs:: read_dir ( & dir) . unwrap ( ) ;
3859- let mut mon = mons. next ( ) . unwrap ( ) . unwrap ( ) ;
3860- if mon. path ( ) . to_str ( ) . unwrap ( ) . ends_with ( ".tmp" ) {
3861- mon = mons. next ( ) . unwrap ( ) . unwrap ( ) ;
3862- assert_eq ! ( mon. path( ) . extension( ) , None ) ;
3863- }
3864- assert ! ( mons. next( ) . is_none( ) ) ;
3868+ let mut mons = list_monitor_files ( & dir) ;
3869+ assert_eq ! ( mons. len( ) , 1 ) ;
3870+ let mon = mons. pop ( ) . unwrap ( ) ;
38653871
38663872 // Because the channel wasn't funded, we'll archive the ChannelMonitor immedaitely after
38673873 // its force-closed (at least on node B, which didn't put their money into it).
38683874 nodes[ 1 ] . node . force_close_all_channels_broadcasting_latest_txn ( "" . to_owned ( ) ) ;
38693875 loop {
3870- let mut mons = std:: fs:: read_dir ( & dir) . unwrap ( ) ;
3871- if let Some ( new_mon) = mons. next ( ) {
3872- let mut new_mon = new_mon. unwrap ( ) ;
3873- if new_mon. path ( ) . to_str ( ) . unwrap ( ) . ends_with ( ".tmp" ) {
3874- new_mon = mons. next ( ) . unwrap ( ) . unwrap ( ) ;
3875- assert_eq ! ( new_mon. path( ) . extension( ) , None ) ;
3876- }
3877- assert_eq ! ( new_mon. path( ) , mon. path( ) ) ;
3878- assert ! ( mons. next( ) . is_none( ) ) ;
3879- } else {
3876+ let mons = list_monitor_files ( & dir) ;
3877+ if mons. is_empty ( ) {
38803878 break ;
38813879 }
3880+ assert_eq ! ( mons. len( ) , 1 ) ;
3881+ assert_eq ! ( mons[ 0 ] . path( ) , mon. path( ) ) ;
38823882 tokio:: task:: yield_now ( ) . await ;
38833883 }
38843884
0 commit comments