Skip to content

Commit 360b6e5

Browse files
joostjagerclaude
andcommitted
Refactor monitor file listing in tests to filter .tmp files
Extract common logic for listing monitor files into a helper function that filters out temporary .tmp files created during persistence operations. This simplifies test code and improves reliability on systems where directory iteration order is non-deterministic. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9627101 commit 360b6e5

File tree

1 file changed

+41
-34
lines changed
  • lightning-background-processor/src

1 file changed

+41
-34
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,33 @@ mod tests {
20432043
const EVENT_DEADLINE: Duration =
20442044
Duration::from_millis(5 * (FRESHNESS_TIMER.as_millis() as u64));
20452045

2046+
/// Reads a directory and returns only non-`.tmp` files.
2047+
/// The file system may return files in any order, and during persistence
2048+
/// operations there may be temporary `.tmp` files present.
2049+
fn list_monitor_files(dir: &str) -> Vec<std::fs::DirEntry> {
2050+
let result: Vec<_> = std::fs::read_dir(dir)
2051+
.unwrap()
2052+
.filter_map(|entry| {
2053+
let entry = entry.unwrap();
2054+
let path_str = entry.path().to_str().unwrap().to_lowercase();
2055+
// Skip any .tmp files that may exist during persistence.
2056+
// On Windows, ReplaceFileW creates backup files with .TMP (uppercase).
2057+
if path_str.ends_with(".tmp") {
2058+
None
2059+
} else {
2060+
Some(entry)
2061+
}
2062+
})
2063+
.collect();
2064+
if result.len() > 1 {
2065+
println!("[DEBUG] list_monitor_files found {} files:", result.len());
2066+
for e in &result {
2067+
println!("[DEBUG] - {:?}", e.path());
2068+
}
2069+
}
2070+
result
2071+
}
2072+
20462073
#[derive(Clone, Hash, PartialEq, Eq)]
20472074
struct TestDescriptor {}
20482075
impl SocketDescriptor for TestDescriptor {
@@ -3860,30 +3887,20 @@ mod tests {
38603887
);
38613888

38623889
let dir = format!("{}_persister_1/monitors", &persist_dir);
3863-
let mut mons = std::fs::read_dir(&dir).unwrap();
3864-
let mut mon = mons.next().unwrap().unwrap();
3865-
if mon.path().to_str().unwrap().ends_with(".tmp") {
3866-
mon = mons.next().unwrap().unwrap();
3867-
assert_eq!(mon.path().extension(), None);
3868-
}
3869-
assert!(mons.next().is_none());
3890+
let mut mons = list_monitor_files(&dir);
3891+
assert_eq!(mons.len(), 1);
3892+
let mon = mons.pop().unwrap();
38703893

38713894
// Because the channel wasn't funded, we'll archive the ChannelMonitor immedaitely after
38723895
// its force-closed (at least on node B, which didn't put their money into it).
38733896
nodes[1].node.force_close_all_channels_broadcasting_latest_txn("".to_owned());
38743897
loop {
3875-
let mut mons = std::fs::read_dir(&dir).unwrap();
3876-
if let Some(new_mon) = mons.next() {
3877-
let mut new_mon = new_mon.unwrap();
3878-
if new_mon.path().to_str().unwrap().ends_with(".tmp") {
3879-
new_mon = mons.next().unwrap().unwrap();
3880-
assert_eq!(new_mon.path().extension(), None);
3881-
}
3882-
assert_eq!(new_mon.path(), mon.path());
3883-
assert!(mons.next().is_none());
3884-
} else {
3898+
let mons = list_monitor_files(&dir);
3899+
if mons.is_empty() {
38853900
break;
38863901
}
3902+
assert_eq!(mons.len(), 1);
3903+
assert_eq!(mons[0].path(), mon.path());
38873904
}
38883905

38893906
bp.stop().unwrap();
@@ -3928,30 +3945,20 @@ mod tests {
39283945
));
39293946

39303947
let dir = format!("{}_persister_1/monitors", &persist_dir);
3931-
let mut mons = std::fs::read_dir(&dir).unwrap();
3932-
let mut mon = mons.next().unwrap().unwrap();
3933-
if mon.path().to_str().unwrap().ends_with(".tmp") {
3934-
mon = mons.next().unwrap().unwrap();
3935-
assert_eq!(mon.path().extension(), None);
3936-
}
3937-
assert!(mons.next().is_none());
3948+
let mut mons = list_monitor_files(&dir);
3949+
assert_eq!(mons.len(), 1);
3950+
let mon = mons.pop().unwrap();
39383951

39393952
// Because the channel wasn't funded, we'll archive the ChannelMonitor immedaitely after
39403953
// its force-closed (at least on node B, which didn't put their money into it).
39413954
nodes[1].node.force_close_all_channels_broadcasting_latest_txn("".to_owned());
39423955
loop {
3943-
let mut mons = std::fs::read_dir(&dir).unwrap();
3944-
if let Some(new_mon) = mons.next() {
3945-
let mut new_mon = new_mon.unwrap();
3946-
if new_mon.path().to_str().unwrap().ends_with(".tmp") {
3947-
new_mon = mons.next().unwrap().unwrap();
3948-
assert_eq!(new_mon.path().extension(), None);
3949-
}
3950-
assert_eq!(new_mon.path(), mon.path());
3951-
assert!(mons.next().is_none());
3952-
} else {
3956+
let mons = list_monitor_files(&dir);
3957+
if mons.is_empty() {
39533958
break;
39543959
}
3960+
assert_eq!(mons.len(), 1);
3961+
assert_eq!(mons[0].path(), mon.path());
39553962
tokio::task::yield_now().await;
39563963
}
39573964

0 commit comments

Comments
 (0)