From e4f5d29c5ecf5149791feb34e50d2b3468d75bc4 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 19 Dec 2025 02:24:11 +1100 Subject: [PATCH] fix: add test-only `LockFile::read_pid` for cross-platform testing Fixes an issue encountered in the CI overhaul PR #253. Open lock file with read+write access and add test-only `read_pid` function that reads from the same file handle. This works on Windows where exclusive locks prevent reading through another file handle: ``` thread 'storage::lockfile::tests::test_lock_file_contains_pid' (8952) panicked at dash-spv\src\storage\lockfile.rs:72:59: Should read lock file: Os { code: 33, kind: Uncategorized, message: "The process cannot access the file because another process has locked a portion of the file." } ``` --- dash-spv/src/storage/lockfile.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/dash-spv/src/storage/lockfile.rs b/dash-spv/src/storage/lockfile.rs index 739c1db1d..03d3a3141 100644 --- a/dash-spv/src/storage/lockfile.rs +++ b/dash-spv/src/storage/lockfile.rs @@ -1,6 +1,6 @@ //! Lock file implementation and related unit tests. -use std::fs::File; +use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::PathBuf; @@ -14,7 +14,12 @@ pub(super) struct LockFile { impl LockFile { pub(super) fn new(path: PathBuf) -> StorageResult { - let mut file = File::create(&path) + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(true) + .open(&path) .map_err(|e| StorageError::WriteFailed(format!("Failed to create lock file: {}", e)))?; file.try_lock().map_err(|e| match e { @@ -49,8 +54,22 @@ impl Drop for LockFile { #[cfg(test)] mod tests { use super::*; + use std::io::{Read, Seek, SeekFrom}; use tempfile::TempDir; + impl LockFile { + /// Reads the PID from the lock file. + fn read_pid(&mut self) -> std::io::Result { + self._file.seek(SeekFrom::Start(0))?; + let mut content = String::new(); + self._file.read_to_string(&mut content)?; + content + .trim() + .parse() + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) + } + } + #[test] fn test_lock_file_creation() { let temp_dir = TempDir::new().expect("Failed to create temp directory"); @@ -67,11 +86,8 @@ mod tests { let temp_dir = TempDir::new().expect("Failed to create temp directory"); let lock_path = temp_dir.path().join(".lock"); - let _lock = LockFile::new(lock_path.clone()).unwrap(); - - let content = std::fs::read_to_string(&lock_path).expect("Should read lock file"); - let pid: u32 = content.trim().parse().expect("Lock file should contain valid PID"); - assert_eq!(pid, std::process::id()); + let mut lock = LockFile::new(lock_path.clone()).unwrap(); + assert_eq!(lock.read_pid().unwrap(), std::process::id()); } #[test]