diff --git a/src/publication/sparql.rs b/src/publication/sparql.rs index 9e4a5507..02ddeae4 100644 --- a/src/publication/sparql.rs +++ b/src/publication/sparql.rs @@ -142,11 +142,24 @@ impl Read for LimitedRead { Ok(n) } } +/// Converts a transport failure into an I/O error that names every cause. +/// +/// `reqwest` reports connect failures, timeouts and dropped connections all as +/// "error sending request"; the distinguishing cause is only in the source +/// chain, so the chain is rendered rather than discarded. pub(crate) fn http_error(error: reqwest::Error) -> io::Error { - io::Error::other(format!( - "SPARQL HTTP request failed: {}", - error.without_url() - )) + let error = error.without_url(); + let mut message = format!("SPARQL HTTP request failed: {error}"); + let mut cause = std::error::Error::source(&error); + while let Some(inner) = cause { + message.push_str(": "); + message.push_str(&inner.to_string()); + cause = inner.source(); + } + if error.is_timeout() { + message.push_str(" (timed out)"); + } + io::Error::other(message) } pub(crate) fn status(status: reqwest::StatusCode) -> io::Result<()> { if status.is_success() { diff --git a/tests/sparql_support/mod.rs b/tests/sparql_support/mod.rs index 5676739c..8d1ed4f5 100644 --- a/tests/sparql_support/mod.rs +++ b/tests/sparql_support/mod.rs @@ -67,7 +67,22 @@ impl Server { let state = shared.clone(); let base = origin.clone(); thread::spawn(move || { - if let Err(error) = serve(stream, &base, state) + let started = std::time::Instant::now(); + let outcome = serve(stream, &base, state); + let elapsed = started.elapsed(); + if elapsed > std::time::Duration::from_millis(250) || + outcome.is_err() + { + let _ = std::io::Write::write_all( + &mut std::io::stderr(), + format!( + "DIAG mock request {elapsed:?} {:?}\n", + outcome.as_ref().err() + ) + .as_bytes(), + ); + } + if let Err(error) = outcome && !error.downcast_ref::().is_some_and(|e| matches!(e.kind(), std::io::ErrorKind::BrokenPipe | std::io::ErrorKind::ConnectionReset)) { panic!("{error:#}"); } }); }, @@ -165,7 +180,24 @@ fn serve( if delay > 0 { thread::sleep(std::time::Duration::from_millis(delay)); } + let waited = std::time::Instant::now(); let mut state = state.lock().unwrap(); + let wait = waited.elapsed(); + if wait > std::time::Duration::from_millis(100) { + let _ = std::io::Write::write_all( + &mut std::io::stderr(), + format!("DIAG mock lock wait {wait:?} {method} {path}\n") + .as_bytes(), + ); + } + let handling = std::time::Instant::now(); + let _guard = Timing( + handling, + format!( + "{method} {path} quads={}", + state.store.len().unwrap_or(0) + ), + ); state.authenticated |= headers .to_ascii_lowercase() .contains("authorization: bearer grapharch-test-only"); @@ -338,3 +370,16 @@ fn query_json(store: &Store, query: &str) -> Result> { }, } } + +struct Timing(std::time::Instant, String); +impl Drop for Timing { + fn drop(&mut self) { + let e = self.0.elapsed(); + if e > std::time::Duration::from_millis(100) { + let _ = std::io::Write::write_all( + &mut std::io::stderr(), + format!("DIAG mock handled {e:?} {}\n", self.1).as_bytes(), + ); + } + } +}