test(peer): consolidate temp dir helper

Move the repeated test TempDir implementations into a single peer
test_support module. The shared helper keeps the existing automatic cleanup
behavior and uses an atomic suffix plus timestamp so parallel tests do not
collide on the same path.

This is intentionally limited to test hygiene. It does not change the
availability model, split download.rs, or touch production scan/install
behavior beyond importing the shared helper from test modules.

Test Plan:
- git diff --check
- just fmt
- just clippy
- just test

Follow-up-Plan: FOLLOW_UP_2.md
This commit is contained in:
2026-05-16 09:21:43 +02:00
parent 7731a9daa0
commit 894eb5af6a
10 changed files with 103 additions and 307 deletions
@@ -334,11 +334,8 @@ mod tests {
use std::{
collections::HashSet,
path::{Path, PathBuf},
sync::{
Arc,
atomic::{AtomicU64, Ordering},
},
time::{Duration, SystemTime, UNIX_EPOCH},
sync::Arc,
time::Duration,
};
use notify::EventKind;
@@ -346,39 +343,13 @@ mod tests {
use tokio_util::{sync::CancellationToken, task::TaskTracker};
use super::*;
use crate::{UnpackFuture, Unpacker, context::OperationKind, peer_db::PeerGameDB};
struct TempDir(PathBuf);
static NEXT_TEMP_ID: AtomicU64 = AtomicU64::new(0);
impl TempDir {
fn new() -> Self {
let mut path = std::env::temp_dir();
let unique_id = NEXT_TEMP_ID.fetch_add(1, Ordering::Relaxed);
path.push(format!(
"lanspread-local-monitor-{}-{}-{}",
std::process::id(),
unique_id,
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
));
std::fs::create_dir_all(&path).expect("temp dir should be created");
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
use crate::{
UnpackFuture,
Unpacker,
context::OperationKind,
peer_db::PeerGameDB,
test_support::TempDir,
};
struct NoopUnpacker;
@@ -467,7 +438,7 @@ mod tests {
#[tokio::test]
async fn watch_event_for_active_game_is_dropped() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-monitor");
let ctx = test_ctx(
temp.path().to_path_buf(),
HashSet::from(["game".to_string()]),
@@ -501,7 +472,7 @@ mod tests {
#[tokio::test]
async fn burst_watch_events_collapse_to_two_rescans_for_same_game() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-monitor");
let game_root = temp.path().join("game");
write_file(&game_root.join("version.ini"), b"20250101");
let ctx = test_ctx(
@@ -538,7 +509,7 @@ mod tests {
#[tokio::test]
async fn fallback_scan_picks_up_sideloaded_catalog_game() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-monitor");
write_file(&temp.path().join("game").join("version.ini"), b"20250101");
let ctx = test_ctx(
temp.path().to_path_buf(),
@@ -560,7 +531,7 @@ mod tests {
#[tokio::test]
async fn fallback_scan_ignores_non_catalog_game_without_library_delta() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-local-monitor");
write_file(
&temp.path().join("non-catalog").join("version.ini"),
b"20250101",
+4 -39
View File
@@ -398,11 +398,7 @@ mod tests {
use std::{
collections::HashSet,
path::{Path, PathBuf},
sync::{
Arc,
atomic::{AtomicU64, Ordering},
},
time::{SystemTime, UNIX_EPOCH},
sync::Arc,
};
use tokio::sync::{RwLock, mpsc};
@@ -414,40 +410,9 @@ mod tests {
Unpacker,
context::{Ctx, OperationKind},
peer_db::PeerGameDB,
test_support::TempDir,
};
struct TempDir(PathBuf);
static NEXT_TEMP_ID: AtomicU64 = AtomicU64::new(0);
impl TempDir {
fn new() -> Self {
let mut path = std::env::temp_dir();
let unique_id = NEXT_TEMP_ID.fetch_add(1, Ordering::Relaxed);
path.push(format!(
"lanspread-stream-{}-{}-{}",
std::process::id(),
unique_id,
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
));
std::fs::create_dir_all(&path).expect("temp dir should be created");
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
struct NoopUnpacker;
impl Unpacker for NoopUnpacker {
@@ -488,7 +453,7 @@ mod tests {
#[tokio::test]
async fn get_game_response_respects_serve_gates() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-stream");
write_file(&temp.path().join("ready").join("version.ini"), b"20250101");
write_file(
&temp.path().join("non-catalog").join("version.ini"),
@@ -531,7 +496,7 @@ mod tests {
#[tokio::test]
async fn file_transfer_dispatch_respects_serve_gates() {
let temp = TempDir::new();
let temp = TempDir::new("lanspread-stream");
write_file(&temp.path().join("ready").join("version.ini"), b"20250101");
write_file(
&temp.path().join("non-catalog").join("version.ini"),