use std::time::Duration;
#[derive(Debug, Clone)]
pub struct HttpTailReclaimState {
pub protocol: String,
pub p2p_involved: bool,
pub total_length: i64,
pub pending_length: i64,
pub has_missing_unused_piece: bool,
pub num_concurrent_command: i32,
pub num_stream_command: i32,
pub current_session_download_length: u64,
pub last_session_download_length: u64,
pub no_progress_time: Duration,
pub stall_time: Duration,
}
pub fn is_http_tail_blocked(state: &HttpTailReclaimState) -> bool {
if !is_http_protocol(&state.protocol)
|| state.p2p_involved
|| state.total_length <= 0
|| state.pending_length <= 0
|| state.has_missing_unused_piece
|| state.num_concurrent_command <= 1
|| state.num_stream_command <= 0
|| state.num_stream_command >= state.num_concurrent_command
{
return false;
}
true
}
pub fn should_reclaim_http_tail_segment(state: &HttpTailReclaimState) -> bool {
if !is_http_tail_blocked(state) {
return false;
}
if state.current_session_download_length > state.last_session_download_length {
return false;
}
state.no_progress_time >= state.stall_time
}
fn is_http_protocol(protocol: &str) -> bool {
protocol.eq_ignore_ascii_case("http") || protocol.eq_ignore_ascii_case("https")
}
#[cfg(test)]
mod tests {
use super::*;
fn blocked_state() -> HttpTailReclaimState {
HttpTailReclaimState {
protocol: "http".into(),
p2p_involved: false,
total_length: 10_000,
pending_length: 5_000,
has_missing_unused_piece: false,
num_concurrent_command: 3,
num_stream_command: 1,
current_session_download_length: 100,
last_session_download_length: 100,
no_progress_time: Duration::from_secs(30),
stall_time: Duration::from_secs(20),
}
}
#[test]
fn test_is_http_protocol_lowercase() {
assert!(is_http_protocol("http"));
assert!(is_http_protocol("https"));
}
#[test]
fn test_is_http_protocol_uppercase() {
assert!(is_http_protocol("HTTP"));
assert!(is_http_protocol("HTTPS"));
}
#[test]
fn test_is_http_protocol_mixed_case() {
assert!(is_http_protocol("HtTp"));
assert!(is_http_protocol("HtTpS"));
}
#[test]
fn test_is_http_protocol_non_http() {
assert!(!is_http_protocol("ftp"));
assert!(!is_http_protocol(""));
assert!(!is_http_protocol("websocket"));
}
#[test]
fn test_blocked_returns_true_with_default_state() {
assert!(is_http_tail_blocked(&blocked_state()));
}
#[test]
fn test_not_blocked_when_non_http_protocol() {
let mut s = blocked_state();
s.protocol = "ftp".into();
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_p2p_involved() {
let mut s = blocked_state();
s.p2p_involved = true;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_total_length_zero() {
let mut s = blocked_state();
s.total_length = 0;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_total_length_negative() {
let mut s = blocked_state();
s.total_length = -1;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_pending_length_zero() {
let mut s = blocked_state();
s.pending_length = 0;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_pending_length_negative() {
let mut s = blocked_state();
s.pending_length = -1;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_has_missing_unused_piece() {
let mut s = blocked_state();
s.has_missing_unused_piece = true;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_only_one_concurrent_command() {
let mut s = blocked_state();
s.num_concurrent_command = 1;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_zero_concurrent_commands() {
let mut s = blocked_state();
s.num_concurrent_command = 0;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_zero_stream_commands() {
let mut s = blocked_state();
s.num_stream_command = 0;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_stream_equals_concurrent() {
let mut s = blocked_state();
s.num_stream_command = 3;
s.num_concurrent_command = 3;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_not_blocked_when_stream_exceeds_concurrent() {
let mut s = blocked_state();
s.num_stream_command = 4;
s.num_concurrent_command = 3;
assert!(!is_http_tail_blocked(&s));
}
#[test]
fn test_blocked_with_https_protocol() {
let mut s = blocked_state();
s.protocol = "https".into();
assert!(is_http_tail_blocked(&s));
}
#[test]
fn test_blocked_with_https_uppercase() {
let mut s = blocked_state();
s.protocol = "HTTPS".into();
assert!(is_http_tail_blocked(&s));
}
#[test]
fn test_blocked_when_stream_less_than_concurrent() {
let s = blocked_state();
assert_eq!(s.num_stream_command, 1);
assert_eq!(s.num_concurrent_command, 3);
assert!(is_http_tail_blocked(&s));
}
#[test]
fn test_no_reclaim_when_tail_not_blocked() {
let mut s = blocked_state();
s.protocol = "ftp".into();
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_when_progress_being_made() {
let mut s = blocked_state();
s.current_session_download_length = 200;
s.last_session_download_length = 100;
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_when_progress_equal_sessions() {
let mut s = blocked_state();
s.current_session_download_length = 100;
s.last_session_download_length = 100;
assert!(should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_reclaim_when_stalled_long_enough() {
let s = blocked_state();
assert!(s.no_progress_time >= s.stall_time);
assert!(should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_when_not_stalled_long_enough() {
let mut s = blocked_state();
s.no_progress_time = Duration::from_secs(10);
s.stall_time = Duration::from_secs(20);
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_reclaim_when_stall_time_exactly_met() {
let mut s = blocked_state();
s.no_progress_time = Duration::from_secs(20);
s.stall_time = Duration::from_secs(20);
assert!(should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_reclaim_blocked_zero_progress_stalled() {
let mut s = blocked_state();
s.current_session_download_length = 0;
s.last_session_download_length = 0;
s.no_progress_time = Duration::from_secs(60);
s.stall_time = Duration::from_secs(30);
assert!(should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_blocked_but_still_progressing() {
let mut s = blocked_state();
s.current_session_download_length = 500;
s.last_session_download_length = 100;
s.no_progress_time = Duration::from_secs(60);
s.stall_time = Duration::from_secs(30);
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_p2p_overrides_stall() {
let mut s = blocked_state();
s.p2p_involved = true;
s.no_progress_time = Duration::from_secs(600);
s.stall_time = Duration::from_secs(1);
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_single_command_overrides_stall() {
let mut s = blocked_state();
s.num_concurrent_command = 1;
s.no_progress_time = Duration::from_secs(600);
s.stall_time = Duration::from_secs(1);
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_missing_unused_piece_overrides_stall() {
let mut s = blocked_state();
s.has_missing_unused_piece = true;
s.no_progress_time = Duration::from_secs(600);
s.stall_time = Duration::from_secs(1);
assert!(!should_reclaim_http_tail_segment(&s));
}
#[test]
fn test_no_reclaim_all_commands_are_streams() {
let mut s = blocked_state();
s.num_concurrent_command = 2;
s.num_stream_command = 2;
assert!(!should_reclaim_http_tail_segment(&s));
}
}