use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use super::session_serializer::{self, SessionEntry};
use crate::request::request_group::RequestGroup;
use crate::util::rwlock_ext::RwLockRecover;
pub struct ActiveSessionManager {
pub session_path: PathBuf,
pub auto_save_interval: Duration,
dirty_flag: AtomicBool,
}
impl ActiveSessionManager {
pub fn new(session_path: PathBuf, auto_save_interval: Duration) -> Self {
tracing::info!(
"Creating ActiveSessionManager: path={}, interval={:?}",
session_path.display(),
auto_save_interval
);
ActiveSessionManager {
session_path,
auto_save_interval,
dirty_flag: AtomicBool::new(false),
}
}
pub async fn load_session(&self) -> Result<Vec<SessionEntry>, String> {
if !self.session_path.exists() {
tracing::debug!(
"Session file not found, returning empty list: {}",
self.session_path.display()
);
return Ok(vec![]);
}
match session_serializer::load_from_file(&self.session_path).await {
Ok(entries) => {
tracing::info!(
"Session file loaded successfully: {}, entries: {}",
self.session_path.display(),
entries.len()
);
Ok(entries)
}
Err(e) => {
let err_msg = format!("Failed to load session file: {}", e);
tracing::error!("{}", err_msg);
Err(err_msg)
}
}
}
pub async fn save_session(
&self,
groups: &[Arc<std::sync::RwLock<RequestGroup>>],
) -> Result<usize, String> {
let mut entries = Vec::new();
for group_lock in groups {
let group = group_lock.recover();
if let Some(entry) = session_serializer::group_to_entry(&group) {
entries.push(entry);
}
}
match session_serializer::save_to_file_with_entries(&self.session_path, &entries).await {
Ok(_) => {
tracing::info!(
"Session file saved successfully: {}, entries: {}",
self.session_path.display(),
entries.len()
);
Ok(entries.len())
}
Err(e) => {
let err_msg = format!("Failed to save session file: {}", e);
tracing::error!("{}", err_msg);
Err(err_msg)
}
}
}
pub fn mark_dirty(&self) {
self.dirty_flag.store(true, Ordering::Relaxed);
tracing::debug!("Marking session as dirty");
}
pub fn is_dirty(&self) -> bool {
self.dirty_flag.load(Ordering::Relaxed)
}
pub fn start_auto_save(
self: &Arc<Self>,
groups: Arc<tokio::sync::RwLock<Vec<Arc<std::sync::RwLock<RequestGroup>>>>>,
) {
let mgr = Arc::clone(self);
tracing::info!(
"Starting auto-save task, interval: {:?}",
mgr.auto_save_interval
);
tokio::spawn(async move {
let mut interval = tokio::time::interval(mgr.auto_save_interval);
loop {
interval.tick().await;
if !mgr.is_dirty() {
tracing::debug!("Auto-save check: no changes, skipping");
continue;
}
tracing::debug!("Auto-save check: changes detected, starting save");
let groups_read = groups.read().await;
match mgr.save_session(&groups_read).await {
Ok(n) => {
tracing::debug!("Auto-save succeeded: saved {} entries", n);
mgr.dirty_flag.store(false, Ordering::Relaxed);
}
Err(e) => {
tracing::warn!("Auto-save failed: {} (keeping dirty flag for retry)", e);
}
}
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::request::request_group::{DownloadOptions, GroupId};
use tempfile::TempDir;
#[test]
fn test_new_manager() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("session.txt");
let interval = Duration::from_secs(60);
let manager = ActiveSessionManager::new(session_path.clone(), interval);
assert_eq!(
manager.session_path, session_path,
"Path should be set correctly"
);
assert_eq!(
manager.auto_save_interval, interval,
"Interval should be set correctly"
);
assert!(
!manager.is_dirty(),
"Newly created manager should not be dirty"
);
}
#[tokio::test]
async fn test_load_nonexistent_file_returns_empty() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let nonexistent_path = temp_dir.path().join("nonexistent_session.txt");
let manager = ActiveSessionManager::new(nonexistent_path, Duration::from_secs(60));
let result = manager.load_session().await;
assert!(result.is_ok(), "Non-existent file should not return error");
let entries = result.unwrap();
assert!(
entries.is_empty(),
"Non-existent file should return empty list"
);
}
#[tokio::test]
async fn test_load_save_roundtrip() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("roundtrip_session.txt");
let manager = ActiveSessionManager::new(session_path.clone(), Duration::from_secs(60));
let gid1 = GroupId::new(0xd270c8a2);
let options1 = DownloadOptions {
dir: Some("/downloads".to_string()),
split: Some(4),
..Default::default()
};
let group1 = Arc::new(std::sync::RwLock::new(RequestGroup::new(
gid1,
vec!["http://example.com/file1.zip".to_string()],
options1,
)));
let gid2 = GroupId::new(0xabcdef01);
let group2 = Arc::new(std::sync::RwLock::new(RequestGroup::new(
gid2,
vec![
"http://mirror.com/file2.iso".to_string(),
"ftp://backup.com/file2.iso".to_string(),
],
DownloadOptions::default(),
)));
let groups = vec![group1, group2];
let save_result = manager.save_session(&groups).await;
assert!(save_result.is_ok(), "Save should succeed");
let saved_count = save_result.unwrap();
assert!(saved_count > 0, "Should save at least 1 entry");
let load_result = manager.load_session().await;
assert!(load_result.is_ok(), "Load should succeed");
let entries = load_result.unwrap();
assert_eq!(
entries.len(),
saved_count,
"Loaded entry count should match saved count"
);
assert!(
entries
.iter()
.any(|e| e.uris.contains(&"http://example.com/file1.zip".to_string())),
"Should contain the first URI"
);
assert!(
entries
.iter()
.any(|e| e.uris.contains(&"http://mirror.com/file2.iso".to_string())),
"Should contain the second URI"
);
}
#[test]
fn test_mark_dirty_and_check() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("dirty_test.txt");
let manager = ActiveSessionManager::new(session_path, Duration::from_secs(30));
assert!(!manager.is_dirty(), "Initial state should be clean");
manager.mark_dirty();
assert!(manager.is_dirty(), "Should be dirty after mark_dirty");
manager.mark_dirty();
assert!(
manager.is_dirty(),
"Repeated mark_dirty should keep dirty state"
);
}
#[tokio::test]
async fn test_auto_save_skips_when_clean() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("auto_skip_test.txt");
let manager = Arc::new(ActiveSessionManager::new(
session_path.clone(),
Duration::from_millis(50), ));
let groups: Arc<tokio::sync::RwLock<Vec<Arc<std::sync::RwLock<RequestGroup>>>>> =
Arc::new(tokio::sync::RwLock::new(Vec::new()));
manager.start_auto_save(Arc::clone(&groups));
tokio::time::sleep(Duration::from_millis(200)).await;
assert!(
!session_path.exists(),
"Should not create session file when dirty=false"
);
}
#[tokio::test]
async fn test_save_creates_file() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("file_creation_test.txt");
let manager = ActiveSessionManager::new(session_path.clone(), Duration::from_secs(60));
assert!(
!session_path.exists(),
"File should not exist before saving"
);
let gid = GroupId::new(12345);
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
gid,
vec!["http://test.com/file.bin".to_string()],
DownloadOptions::default(),
)));
let result = manager.save_session(&[group]).await;
assert!(result.is_ok(), "Save should succeed");
assert!(
session_path.exists(),
"File should exist at specified path after saving"
);
let content = tokio::fs::read_to_string(&session_path)
.await
.expect("Failed to read file");
assert!(!content.is_empty(), "File content should not be empty");
assert!(
content.contains("http://test.com/file.bin"),
"File should contain the saved URI"
);
}
#[tokio::test]
async fn test_multiple_saves_overwrite() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("overwrite_test.txt");
let manager = ActiveSessionManager::new(session_path.clone(), Duration::from_secs(60));
let gid1 = GroupId::new(1);
let group1 = Arc::new(std::sync::RwLock::new(RequestGroup::new(
gid1,
vec!["http://first.com/a.txt".to_string()],
DownloadOptions::default(),
)));
let result1 = manager.save_session(&[group1]).await;
assert!(result1.is_ok());
let gid2 = GroupId::new(2);
let group2 = Arc::new(std::sync::RwLock::new(RequestGroup::new(
gid2,
vec!["http://second.com/b.txt".to_string()],
DownloadOptions::default(),
)));
let result2 = manager.save_session(&[group2]).await;
assert!(result2.is_ok());
let entries = manager.load_session().await.expect("Failed to load");
assert_eq!(entries.len(), 1, "Should have only 1 entry (the latest)");
assert!(
entries[0]
.uris
.contains(&"http://second.com/b.txt".to_string()),
"Should contain the latest saved URI"
);
assert!(
!entries[0]
.uris
.contains(&"http://first.com/a.txt".to_string()),
"Should not contain the old URI"
);
}
#[tokio::test]
async fn test_save_empty_groups() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("empty_groups_test.txt");
let manager = ActiveSessionManager::new(session_path.clone(), Duration::from_secs(60));
let stale_group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(123),
vec!["http://stale.example/old.bin".to_string()],
DownloadOptions::default(),
)));
manager
.save_session(&[stale_group])
.await
.expect("seed stale session");
let empty_groups: Vec<Arc<std::sync::RwLock<RequestGroup>>> = vec![];
let result = manager.save_session(&empty_groups).await;
assert!(result.is_ok(), "Saving empty list should succeed");
assert_eq!(result.unwrap(), 0, "Should return 0 entries");
assert!(
session_path.exists(),
"Saving an empty group list must clear stale session state"
);
let content = tokio::fs::read_to_string(&session_path)
.await
.expect("Failed to read file");
assert!(
content.is_empty(),
"Empty group list should produce an empty session file"
);
}
#[tokio::test]
async fn test_auto_save_triggers_on_dirty() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let session_path = temp_dir.path().join("auto_trigger_test.txt");
let manager = Arc::new(ActiveSessionManager::new(
session_path.clone(),
Duration::from_millis(50), ));
let gid = GroupId::new(99999);
let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
gid,
vec!["http://auto-save-test.com/data.bin".to_string()],
DownloadOptions::default(),
)));
let groups: Arc<tokio::sync::RwLock<Vec<Arc<std::sync::RwLock<RequestGroup>>>>> =
Arc::new(tokio::sync::RwLock::new(vec![group]));
manager.start_auto_save(Arc::clone(&groups));
manager.mark_dirty();
tokio::time::sleep(Duration::from_millis(300)).await;
if session_path.exists() {
let content = tokio::fs::read_to_string(&session_path)
.await
.expect("Failed to read file");
assert!(
content.contains("http://auto-save-test.com/data.bin") || content.is_empty(),
"File should contain saved data or be empty (depending on timing)"
);
}
}
#[test]
fn test_different_intervals() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let intervals = [
Duration::from_secs(1),
Duration::from_secs(30),
Duration::from_secs(60),
Duration::from_secs(300),
Duration::from_millis(500),
];
for (i, interval) in intervals.iter().enumerate() {
let path = temp_dir.path().join(format!("interval_test_{}.txt", i));
let manager = ActiveSessionManager::new(path, *interval);
assert_eq!(
manager.auto_save_interval, *interval,
"Interval {} should be set correctly",
i
);
}
}
}