Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tilespill_part4.rs
tilespill_part4.rs105 lines · 4.7 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests_extra { |
| 3 | use super::tests::{drain, every_layer, feature, tmp}; |
| 4 | use super::*; |
| 5 | use tilecodec::mamaps::body::{GEOM_LINE, WINDING_OUTER}; |
| 6 | |
| 7 | /// Byte-identity, at the level the format can state it: what a reader yields does not depend on |
| 8 | /// how much it buffers. This is what pins "the window size is not observable". |
| 9 | #[test] |
| 10 | fn a_chunk_reads_the_same_however_the_window_is_sized() { |
| 11 | let spill = ChunkSpill::create(tmp("windows")).expect("create"); |
| 12 | let mut map: BTreeMap<(u64, u8), ChunkEntry> = every_layer().into_iter().collect(); |
| 13 | // One entry far larger than the smallest window, so at least one read has to be the |
| 14 | // oversize path rather than the buffered one. |
| 15 | let mut big = ChunkEntry::new(4); |
| 16 | big.layer.features = vec![feature(1, 1, GEOM_LINE, 0, 0, 1)]; |
| 17 | big.layer.parts = |
| 18 | vec![Part { coord_start: 0, point_count: 40_000, winding: WINDING_OUTER }]; |
| 19 | // The coordinates themselves are arbitrary; only the point count matters here. Kept |
| 20 | // inside `i16`'s range rather than cast from the index, which wraps to `i16::MIN` at |
| 21 | // 32 768 and then overflows on the negation. |
| 22 | big.layer.coords = (0..40_000) |
| 23 | .map(|i| { |
| 24 | let v = (i % 30_000) as i16; |
| 25 | (v, -v) |
| 26 | }) |
| 27 | .collect(); |
| 28 | map.insert((6, 4), big); |
| 29 | let at = spill.write_chunk(map).expect("write"); |
| 30 | |
| 31 | let want = drain(&spill, &at, 1 << 20); |
| 32 | for window in [1usize, 24, 25, 64, 1024, 1 << 14, 1 << 16, 1 << 22] { |
| 33 | assert_eq!(drain(&spill, &at, window), want, "window {window} yielded something else"); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn the_scratch_file_is_removed_on_drop() { |
| 39 | let path = tmp("dropped"); |
| 40 | { |
| 41 | let spill = ChunkSpill::create(&path).expect("create"); |
| 42 | spill.write_chunk(every_layer().into_iter().collect()).expect("write"); |
| 43 | assert!(path.exists()); |
| 44 | } |
| 45 | assert!(!path.exists(), "the scratch file outlived its spill"); |
| 46 | } |
| 47 | |
| 48 | /// The error path too: a spill dropped while a write is failing must not strand the file. |
| 49 | #[test] |
| 50 | fn the_scratch_file_is_removed_when_a_chunk_is_refused() { |
| 51 | let path = tmp("refused"); |
| 52 | { |
| 53 | let spill = ChunkSpill::create(&path).expect("create"); |
| 54 | // Keyed on one layer, carrying another. |
| 55 | let mut map: BTreeMap<(u64, u8), ChunkEntry> = BTreeMap::new(); |
| 56 | map.insert((1, 2), ChunkEntry::new(3)); |
| 57 | assert!(spill.write_chunk(map).is_err(), "a mislabelled entry was accepted"); |
| 58 | } |
| 59 | assert!(!path.exists(), "the scratch file outlived a failed write"); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn an_empty_chunk_is_zero_bytes_and_reads_as_nothing() { |
| 64 | let spill = ChunkSpill::create(tmp("empty")).expect("create"); |
| 65 | let at = spill.write_chunk(BTreeMap::new()).expect("write"); |
| 66 | assert_eq!(at, ChunkRef { at: 0, len: 0, entries: 0 }); |
| 67 | assert!(drain(&spill, &at, 1 << 16).is_empty()); |
| 68 | spill.check_books().expect("the books balance"); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn the_books_catch_a_chunk_that_was_never_read() { |
| 73 | let spill = ChunkSpill::create(tmp("unread")).expect("create"); |
| 74 | spill.write_chunk(every_layer().into_iter().collect()).expect("write"); |
| 75 | assert!(spill.check_books().is_err(), "an undrained spill balanced"); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn the_read_window_is_a_budget_divided_by_the_stream_count() { |
| 80 | // `window_for` rather than `read_window_bytes`, so a test elsewhere holding the override |
| 81 | // cannot make this one assert something else. |
| 82 | assert_eq!(window_for(0), MAX_WINDOW, "no streams clamps up, not to zero"); |
| 83 | assert_eq!(window_for(1), MAX_WINDOW); |
| 84 | // A north-america z14: the budget still binds, well clear of both clamps. |
| 85 | let na = window_for(5_700); |
| 86 | assert_eq!(na, READ_BUDGET / 5_700); |
| 87 | assert!((MIN_WINDOW..=MAX_WINDOW).contains(&na)); |
| 88 | // A planet z14: the floor takes over, so the total is `streams * MIN_WINDOW`. |
| 89 | assert_eq!(window_for(27_000), MIN_WINDOW); |
| 90 | assert_eq!(window_for(10_000_000), MIN_WINDOW); |
| 91 | for streams in [1usize, 2, 100, 5_700, 27_000, 10_000_000] { |
| 92 | let window = window_for(streams); |
| 93 | assert!( |
| 94 | (MIN_WINDOW..=MAX_WINDOW).contains(&window), |
| 95 | "{streams} streams gave a {window}-byte window" |
| 96 | ); |
| 97 | } |
| 98 | // The planet ceiling: streams x floor must stay a small fraction of a |
| 99 | // constrained box's commit headroom (~50 GB here), not half of it. |
| 100 | assert!( |
| 101 | 27_000u64 * MIN_WINDOW as u64 <= 512 << 20, |
| 102 | "a planet z14's merge cursors must fit in ~512 MB" |
| 103 | ); |
| 104 | } |
| 105 | } |