A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
Add-on download: a test for a request that runs out of time mid-file (1042)
gelenmedyaf4c2bb6533da
diff --git a/crates/rf-app/src/addons.rs b/crates/rf-app/src/addons.rsindex da769f14..1064edfd 100644--- a/crates/rf-app/src/addons.rs+++ b/crates/rf-app/src/addons.rs@@ -95,10 +95,18 @@ pub fn newer(a: &str, b: &str) -> bool {parts(a) > parts(b)}+/// How long one request may take to bring its body. It covers the WHOLE body, not a pause in it, which is why a download asks+/// again for the rest when it runs out ([`download`]).+const BODY_LIMIT: std::time::Duration = std::time::Duration::from_secs(60);+fn agent() -> ureq::Agent {+ agent_with(BODY_LIMIT)+}++fn agent_with(body_limit: std::time::Duration) -> ureq::Agent {ureq::Agent::config_builder().timeout_connect(Some(std::time::Duration::from_secs(15)))- .timeout_recv_body(Some(std::time::Duration::from_secs(60)))+ .timeout_recv_body(Some(body_limit)).http_status_as_error(false).tls_config(ureq::tls::TlsConfig::builder().root_certs(ureq::tls::RootCerts::PlatformVerifier).build()).build()@@ -159,9 +167,9 @@ enum Got {/// One request for `url` from the byte the sink stands at. A transfer that breaks off is not an error here: the caller asks again/// from where it stopped.-fn fetch(url: &str, sink: &mut Sink, cancelled: &dyn Fn() -> bool) -> Result<Got, String> {+fn fetch(url: &str, sink: &mut Sink, body_limit: std::time::Duration, cancelled: &dyn Fn() -> bool) -> Result<Got, String> {let from = sink.done;- let mut req = agent().get(url);+ let mut req = agent_with(body_limit).get(url);if from > 0 {req = req.header("Range", format!("bytes={from}-"));}@@ -200,6 +208,11 @@ const DRY_TRIES: u32 = 3;/// download is given up only when [`DRY_TRIES`] requests in a row bring nothing. The SHA-256 check afterwards is unchanged, so a/// file put together wrongly is still refused.fn download(url: &str, to: &Path, total: u64, progress: &dyn Fn(u64), cancelled: &dyn Fn() -> bool) -> Result<String, String> {+ download_within(BODY_LIMIT, url, to, total, progress, cancelled)+}++/// [`download`] with the time one request may take (the tests use a short one).+fn download_within(body_limit: std::time::Duration, url: &str, to: &Path, total: u64, progress: &dyn Fn(u64), cancelled: &dyn Fn() -> bool) -> Result<String, String> {let out = std::fs::File::create(to).map_err(|e| format!("{}: {e}", to.display()))?;let mut sink = Sink { out, hash: Sha256::new(), done: 0, progress };if !(url.starts_with("http://") || url.starts_with("https://")) {@@ -219,7 +232,7 @@ fn download(url: &str, to: &Path, total: u64, progress: &dyn Fn(u64), cancelled:let mut dry = 0u32;while total == 0 || sink.done < total {let before = sink.done;- let why = match fetch(url, &mut sink, cancelled)? {+ let why = match fetch(url, &mut sink, body_limit, cancelled)? {Got::All if total == 0 || sink.done >= total => break,Got::All => "the connection closed before the file was complete".to_string(),Got::Cut(e) => e,@@ -321,6 +334,12 @@ mod tests {/// transfer that breaks off; with `continues` it goes on from the `Range` asked for, without it it always starts again./// Returns its address and the count of requests it got.fn breaking_server(body: Vec<u8>, chunk: usize, continues: bool) -> (String, std::sync::Arc<std::sync::atomic::AtomicU32>) {+ stalling_server(body, chunk, continues, std::time::Duration::ZERO)+ }++ /// The same, but after its part the server keeps the connection open and silent for `stall` (a slow line: the request's time+ /// runs out with the body unfinished).+ fn stalling_server(body: Vec<u8>, chunk: usize, continues: bool, stall: std::time::Duration) -> (String, std::sync::Arc<std::sync::atomic::AtomicU32>) {use std::io::{BufRead, BufReader, Write};let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();let url = format!("http://{}/addon.zip", listener.local_addr().unwrap());@@ -344,6 +363,13 @@ mod tests {let status = if from > 0 { "206 Partial Content" } else { "200 OK" };let _ = write!(stream, "HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", rest.len());let _ = stream.write_all(&rest[..rest.len().min(chunk)]);+ let _ = stream.flush();+ if !stall.is_zero() && rest.len() > chunk {+ std::thread::spawn(move || {+ std::thread::sleep(stall);+ drop(stream);+ });+ }}});(url, hits)@@ -373,6 +399,19 @@ mod tests {let _ = std::fs::remove_file(&to);}+ /// The report itself (issue 1042): the request's time runs out with part of the body there ("timeout: receive body"). The next+ /// request brings the rest.+ #[test]+ fn a_download_whose_request_runs_out_of_time_goes_on_too() {+ let body: Vec<u8> = (0..400_000u32).map(|i| (i.wrapping_mul(40_503) >> 8) as u8).collect();+ let (url, hits) = stalling_server(body.clone(), 150_000, true, std::time::Duration::from_secs(4));+ let to = scratch_file("slow.zip");+ let sha = download_within(std::time::Duration::from_millis(700), &url, &to, body.len() as u64, &|_| {}, &|| false).unwrap();+ assert_eq!(sha, sha_of(&body));+ assert_eq!(hits.load(std::sync::atomic::Ordering::Relaxed), 3);+ let _ = std::fs::remove_file(&to);+ }+/// A server that brings nothing is given up after a few requests, with an error.#[test]fn a_download_that_brings_nothing_is_given_up() {