Skip to content
Merged
14 changes: 13 additions & 1 deletion crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5934,6 +5934,11 @@ pub(crate) fn add_cog_with(s: &mut Session, p: &Value, url: &str, strict: bool,
// (which follow the time slider) and AlphaEarth's quantised embeddings (which raster styles
// don't dequantise) are tile layers.
let (file, mut look) = crate::render::rustgis_tiles::cog::split(url);
// A header not read first (a plugin's command in the app, a data cube's file) whose connection
// dropped is read again by what draws the layer now, as a probed one is (#2886).
if !probed {
crate::render::rustgis_tiles::cog::forget_dropped(file);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
}
// A key given as `null` (a script's unused one) asks for nothing.
let given = |k: &str| p.get(k).filter(|v| !v.is_null());
// A data cube's file stays a tile layer, its project opened again too: its address says so.
Expand Down Expand Up @@ -5961,7 +5966,14 @@ pub(crate) fn add_cog_with(s: &mut Session, p: &Value, url: &str, strict: bool,
let dated = s.map().tiles.get(index).ok_or("added GeoTIFF tile is missing")?.url.clone();
// Its header read now (natively) as an undated address's is before it is added.
#[cfg(not(target_arch = "wasm32"))]
let probe = if s.leave_downloads { Ok(false) } else { crate::render::rustgis_tiles::cog::probe_blocking(&dated) };
// Not probed off the frame: the file shown (not the template) is read again by the map's
// draw if its connection dropped (#2886).
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
let probe = if s.leave_downloads {
crate::render::rustgis_tiles::cog::forget_dropped(&dated);
Ok(false)
} else {
crate::render::rustgis_tiles::cog::probe_blocking(&dated)
};
#[cfg(target_arch = "wasm32")]
let probe: Result<bool, String> = Ok(false);
(dated, probe == Ok(true), probe.err())
Expand Down
68 changes: 68 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31586,6 +31586,74 @@ fn a_stac_data_cube_from_a_plugin_command_in_the_app() {
);
}

/// A server that drops every connection once its request is in, and a signal for each request.
#[cfg(not(target_arch = "wasm32"))]
fn serve_dropping_all() -> (String, std::sync::mpsc::Receiver<()>) {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
for stream in listener.incoming() {
let Ok(mut stream) = stream else { return };
let mut buf = [0u8; 4096];
let _ = std::io::Read::read(&mut stream, &mut buf);
// Sent before the connection drops, so a read that has failed has been counted.
let _ = tx.send(());
}
});
(base, rx)
}

/// Whether a header whose connection dropped (#2886) is asked for again once `add` runs: the
/// requests made before it are let go, and one is waited for after it.
#[cfg(not(target_arch = "wasm32"))]
fn asked_again_after(asked: &std::sync::mpsc::Receiver<()>, add: impl FnOnce()) -> bool {
while asked.try_recv().is_ok() {}
add();
asked.recv_timeout(std::time::Duration::from_secs(3)).is_ok()
}

/// A cube's file whose header's connection dropped each time (#2886) is asked for again when the
/// app adds it again (a plugin's command, off the frame), as one the command line adds is.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_cube_file_whose_header_dropped_is_read_again_when_added_off_the_frame() {
let (base, asked) = serve_dropping_all();
let url = format!("{base}/dropped-cube.tif");
let e = crate::render::rustgis_tiles::cog::info_blocking(&url).err().unwrap_or_default();
assert!(e.contains("the connection dropped"), "{e}");
let mut s = Session::new(Project::default());
s.leave_downloads = true;
let again = asked_again_after(&asked, || {
s.run("layer.addUrl", &json!({"url": url, "cube": true})).unwrap();
});
assert!(again, "the header wasn't asked for again");
}

/// A dated address's file whose header's connection dropped (#2886) is asked for again when the
/// app adds the address again off the frame: the file of the date shown, not the template.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_dated_file_whose_header_dropped_is_read_again_when_added_off_the_frame() {
let (base, asked) = serve_dropping_all();
let files: Vec<_> = (2020..=2021).map(|year| format!("{base}/{year}.tif")).collect();
let reads: Vec<_> = files
.iter()
.cloned()
.map(|file| std::thread::spawn(move || crate::render::rustgis_tiles::cog::info_blocking(&file).err().unwrap_or_default()))
.collect();
for read in reads {
let e = read.join().unwrap();
assert!(e.contains("the connection dropped"), "{e}");
}
let mut s = Session::new(Project::default());
s.leave_downloads = true;
let again = asked_again_after(&asked, || {
s.run("layer.addUrl", &json!({"url": format!("{base}/{{date:YYYY}}.tif"), "start": 2020, "end": 2021})).unwrap();
});
assert!(again, "the shown date's header wasn't asked for again");
}

/// An entry that gives no range, as JAXA Earth's don't, is added with one from a sample of its
/// files (`"range": "auto"`): the same for every date. Its credit goes on the tile layers it
/// added, and its collection's licence comes back with it.
Expand Down
Loading
Loading