crates/korp-map-wire/tests/descriptors.rs
//! **The descriptors replies in flight hold**, measured in a test binary of its own (no
//! other test opens sockets meanwhile). Every read of a layer file opened the file
//! again, so a few hundred replies in flight passed a soft limit of 1024 descriptors and
//! the next accept took the process down. Now a file is opened once and read by
//! position.
mod common;
use common::*;
use korp_map_wire::{BBox, FileSource, Kind, MapQuery, MapSource};
/// Descriptors this process holds now.
fn open_descriptors() -> usize {
std::fs::read_dir("/proc/self/fd").map_or(0, Iterator::count)
}
fn query(kind: Kind) -> MapQuery {
MapQuery {
region: REGION.into(),
bbox: BBox {
min_lon: 10.0,
min_lat: 55.0,
max_lon: 20.0,
max_lat: 65.0,
},
lod: 14,
kind,
max: None,
}
}
#[test]
fn replies_in_flight_open_no_descriptor() {
let tmp = tempfile::tempdir().unwrap();
let dir = write_region(tmp.path());
let source = FileSource::open(&[(REGION.to_string(), dir)])
.unwrap()
.with_batch_rows(4);
// Warm: one query of every kind, read whole.
for kind in Kind::ALL {
let (_, _, batches) = source.query(&query(kind)).unwrap().into_parts();
for b in batches {
b.unwrap();
}
}
let before = open_descriptors();
// 200 replies planned and not yet read.
let held: Vec<_> = (0..200)
.map(|i| {
source
.query(&query(Kind::ALL[i % Kind::ALL.len()]))
.unwrap()
})
.collect();
let during = open_descriptors();
let mut rows = 0;
for r in held {
let (_, _, batches) = r.into_parts();
for b in batches {
rows += b.unwrap().num_rows();
}
}
assert!(rows > 0);
assert!(
during <= before + 4,
"200 replies in flight hold {} more descriptors",
during - before
);
}