use std::time::{Duration, Instant};
use super::*;
#[test]
fn test_config_default_values() {
let config = TailReclaimConfig::default();
assert_eq!(config.min_tail_length, DEFAULT_MIN_TAIL_LENGTH);
assert_eq!(config.stall_timeout_secs, DEFAULT_STALL_TIMEOUT_SECS);
assert!(config.enabled);
}
#[test]
fn test_config_builder_pattern() {
let config = TailReclaimConfig::new()
.with_min_tail_length(512 * 1024)
.with_stall_timeout_secs(60)
.with_enabled(false);
assert_eq!(config.min_tail_length, 512 * 1024);
assert_eq!(config.stall_timeout_secs, 60);
assert!(!config.enabled);
}
#[test]
fn test_should_reclaim_disabled() {
let config = TailReclaimConfig::new().with_enabled(false);
assert!(!config.should_reclaim(0, 10 * 1024 * 1024, 0, 0));
}
#[test]
fn test_should_reclaim_large_remaining() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(config.should_reclaim(0, 2047, 0, 0));
}
#[test]
fn test_should_reclaim_small_remaining() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(!config.should_reclaim(0, 1023, 0, 0));
}
#[test]
fn test_should_reclaim_exact_min_tail_boundary() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(config.should_reclaim(0, 1024, 0, 0));
assert!(!config.should_reclaim(0, 1023, 0, 0));
}
#[test]
fn test_should_reclaim_with_bytes_received() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(!config.should_reclaim(0, 4095, 3072, 0));
assert!(config.should_reclaim(0, 4095, 3071, 0));
}
#[test]
fn test_should_reclaim_with_bytes_in_flight() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(!config.should_reclaim(0, 4095, 2048, 1024));
assert!(config.should_reclaim(0, 4095, 2047, 1024));
}
#[test]
fn test_should_reclaim_all_received() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(!config.should_reclaim(0, 4095, 4096, 0));
}
#[test]
fn test_should_reclaim_over_received() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(!config.should_reclaim(0, 4095, 5000, 0));
}
#[test]
fn test_should_reclaim_inverted_range() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(!config.should_reclaim(100, 50, 0, 0));
}
#[test]
fn test_should_reclaim_nonzero_start() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(config.should_reclaim(1000, 3095, 0, 0));
assert!(!config.should_reclaim(1000, 3095, 1072, 0));
}
#[test]
fn test_calculate_tail_basic() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
let result = config.calculate_tail(0, 4095, 2048, 0);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 2048);
assert_eq!(r.tail_end, 4095);
assert_eq!(r.length(), 2048);
}
#[test]
fn test_calculate_tail_with_in_flight() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
let result = config.calculate_tail(0, 4095, 1024, 1024);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 2048);
assert_eq!(r.tail_end, 4095);
assert_eq!(r.length(), 2048);
}
#[test]
fn test_calculate_tail_no_reclaim_returns_none() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(config.calculate_tail(0, 4095, 3072, 0).is_none());
}
#[test]
fn test_calculate_tail_disabled_returns_none() {
let config = TailReclaimConfig::new().with_enabled(false);
assert!(config.calculate_tail(0, 10 * 1024 * 1024, 0, 0).is_none());
}
#[test]
fn test_calculate_tail_nonzero_start() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
let result = config.calculate_tail(1000, 5095, 1000, 0);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 2000);
assert_eq!(r.tail_end, 5095);
assert_eq!(r.length(), 3096);
}
#[test]
fn test_calculate_tail_invariant_tail_start_greater_than_range_start() {
let config = TailReclaimConfig::new().with_min_tail_length(0);
assert!(config.calculate_tail(0, 0, 0, 0).is_none());
}
#[test]
fn test_calculate_tail_invariant_tail_start_within_range() {
let config = TailReclaimConfig::new().with_min_tail_length(0);
assert!(config.calculate_tail(10, 12, 0, 0).is_none());
let result = config.calculate_tail(10, 12, 1, 0);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 11);
assert_eq!(r.tail_end, 12);
assert_eq!(r.length(), 2);
}
#[test]
fn test_calculate_tail_over_consumed_returns_none() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
assert!(config.calculate_tail(0, 4095, 5000, 0).is_none());
}
#[test]
fn test_result_length_single_byte() {
let result = TailReclaimResult {
tail_start: 100,
tail_end: 100,
};
assert_eq!(result.length(), 1);
}
#[test]
fn test_result_length_range() {
let result = TailReclaimResult {
tail_start: 100,
tail_end: 199,
};
assert_eq!(result.length(), 100);
}
#[test]
fn test_tracker_new_not_stalled() {
let tracker = ConnectionStallTracker::new();
assert!(!tracker.is_stalled());
assert_eq!(tracker.last_bytes_received(), 0);
}
#[test]
fn test_tracker_update_progress_resets_stall() {
let mut tracker = ConnectionStallTracker::new();
tracker.update_progress(1024);
assert!(!tracker.is_stalled());
assert_eq!(tracker.last_bytes_received(), 1024);
}
#[test]
fn test_tracker_update_progress_same_bytes_no_reset() {
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
tracker.update_progress_at(1024, now);
assert_eq!(tracker.last_bytes_received(), 1024);
let later = now + Duration::from_secs(10);
tracker.update_progress_at(1024, later);
assert!(!tracker.is_stalled());
assert_eq!(tracker.last_bytes_received(), 1024);
}
#[test]
fn test_tracker_stall_detection_with_at() {
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
tracker.update_progress_at(1024, now);
let at_timeout = now + Duration::from_secs(30);
assert!(tracker.check_stalled_at(Duration::from_secs(30), at_timeout));
let before_timeout = now + Duration::from_millis(29999);
let mut tracker2 = ConnectionStallTracker::new_at(now);
tracker2.update_progress_at(1024, now);
assert!(!tracker2.check_stalled_at(Duration::from_secs(30), before_timeout));
}
#[test]
fn test_tracker_progress_resets_stall() {
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
tracker.update_progress_at(1024, now);
let at_timeout = now + Duration::from_secs(30);
assert!(tracker.check_stalled_at(Duration::from_secs(30), at_timeout));
assert!(tracker.is_stalled());
tracker.update_progress_at(2048, at_timeout);
assert!(!tracker.is_stalled());
}
#[test]
fn test_tracker_stall_not_reset_by_same_progress() {
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
tracker.update_progress_at(1024, now);
let at_timeout = now + Duration::from_secs(30);
assert!(tracker.check_stalled_at(Duration::from_secs(30), at_timeout));
tracker.update_progress_at(1024, at_timeout);
assert!(tracker.is_stalled());
}
#[test]
fn test_tracker_check_stalled_real_time() {
let mut tracker = ConnectionStallTracker::new();
tracker.update_progress(1024);
let _ = tracker.check_stalled(Duration::from_secs(3600));
assert!(!tracker.is_stalled());
}
#[test]
fn test_tracker_reset() {
let mut tracker = ConnectionStallTracker::new();
tracker.update_progress(2048);
assert_eq!(tracker.last_bytes_received(), 2048);
tracker.reset();
assert!(!tracker.is_stalled());
assert_eq!(tracker.last_bytes_received(), 0);
}
#[test]
fn test_tracker_default() {
let tracker = ConnectionStallTracker::default();
assert!(!tracker.is_stalled());
assert_eq!(tracker.last_bytes_received(), 0);
}
#[test]
fn test_full_tail_reclaim_flow() {
let config = TailReclaimConfig::new()
.with_min_tail_length(1024)
.with_stall_timeout_secs(30);
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
let range_start: u64 = 0;
let range_end: u64 = 4095;
tracker.update_progress_at(1024, now);
let at_timeout = now + Duration::from_secs(30);
assert!(tracker.check_stalled_at(Duration::from_secs(config.stall_timeout_secs), at_timeout));
let result = config.calculate_tail(range_start, range_end, 1024, 0);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 1024);
assert_eq!(r.tail_end, 4095);
assert_eq!(r.length(), 3072);
}
#[test]
fn test_full_tail_reclaim_no_reclaim_when_progressing() {
let config = TailReclaimConfig::new()
.with_min_tail_length(1024)
.with_stall_timeout_secs(30);
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
let range_start: u64 = 0;
let range_end: u64 = 4095;
tracker.update_progress_at(1024, now);
let at_10s = now + Duration::from_secs(10);
tracker.update_progress_at(2048, at_10s);
let at_30s = now + Duration::from_secs(30);
assert!(!tracker.check_stalled_at(Duration::from_secs(30), at_30s));
let result = config.calculate_tail(range_start, range_end, 2048, 0);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 2048);
assert_eq!(r.length(), 2048);
}
#[test]
fn test_full_tail_reclaim_disabled() {
let config = TailReclaimConfig::new()
.with_min_tail_length(1024)
.with_enabled(false);
let now = Instant::now();
let mut tracker = ConnectionStallTracker::new_at(now);
let range_start: u64 = 0;
let range_end: u64 = 4095;
tracker.update_progress_at(1024, now);
let at_timeout = now + Duration::from_secs(30);
assert!(tracker.check_stalled_at(Duration::from_secs(30), at_timeout));
assert!(
config
.calculate_tail(range_start, range_end, 1024, 0)
.is_none()
);
}
#[test]
fn test_tail_reclaim_with_large_in_flight() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
let result = config.calculate_tail(0, 10240, 0, 8192);
assert!(result.is_some());
let r = result.unwrap();
assert_eq!(r.tail_start, 8192);
assert_eq!(r.tail_end, 10240);
assert_eq!(r.length(), 2049);
}
#[test]
fn test_tail_reclaim_saturating_add_no_overflow() {
let config = TailReclaimConfig::new().with_min_tail_length(1024);
let result = config.calculate_tail(0, u64::MAX, u64::MAX / 2, u64::MAX / 2 + 1);
assert!(result.is_none());
let extreme_result = TailReclaimResult {
tail_start: 0,
tail_end: u64::MAX,
};
assert_eq!(extreme_result.length(), u64::MAX);
}