Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/korp-map

crates/korp-map-wire/tests/under_load.rs

//! **The server under load**: a failed accept never ends it, and a map reply waits for
//! a work slot without holding up the rest. (The descriptors a reply holds are measured
//! alone, in `tests/descriptors.rs`.)

mod common;

use std::sync::Arc;

use common::*;
use korp_map_wire::{FileSource, serve};

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_failed_accept_does_not_end_the_server() {
    // `serve` returns `()`: it has no error to give up with. A connection after a burst
    // of closed ones is still answered.
    let tmp = tempfile::tempdir().unwrap();
    let dir = write_region(tmp.path());
    let source = Arc::new(FileSource::open(&[(REGION.to_string(), dir)]).unwrap());
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let cfg = korp_map_wire::ServerConfig {
        log: false,
        ..korp_map_wire::ServerConfig::default()
    };
    let task: tokio::task::JoinHandle<()> = tokio::spawn(serve(listener, source, cfg));
    for _ in 0..50 {
        drop(tokio::net::TcpStream::connect(addr).await.unwrap());
    }
    let r = get(&format!("http://{addr}/v1/health")).await;
    assert_eq!(r.status, 200);
    assert!(!task.is_finished());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_map_reply_waits_for_a_work_slot_and_the_rest_of_the_server_does_not() {
    let slots = Arc::new(tokio::sync::Semaphore::new(1));
    let cfg = korp_map_wire::ServerConfig {
        log: false,
        work_slots: slots.clone(),
        ..korp_map_wire::ServerConfig::default()
    };
    let s = TestServer::start(cfg).await;
    let url = s.url("/v1/map/areas?region=testland&lod=14&bbox=10,55,20,65");
    let held = slots.clone().acquire_owned().await.unwrap();
    let pending = tokio::spawn({
        let url = url.clone();
        async move { get(&url).await }
    });
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;
    assert!(!pending.is_finished(), "answered without a work slot");
    assert_eq!(get(&s.url("/v1/health")).await.status, 200);
    drop(held);
    let r = pending.await.unwrap();
    assert_eq!(r.status, 200);
    let all = futures::future::join_all((0..40).map(|_| get(&url))).await;
    for x in &all {
        assert_eq!(x.status, 200);
        assert_eq!(x.body, r.body);
    }
    assert!(
        r.header("content-length").is_some(),
        "a whole reply says its length"
    );
    let (_, batches) = decode(&r.body);
    assert!(batches.iter().map(|b| b.num_rows()).sum::<usize>() > 0);
}