Cotton code
112 lines · 4.9 KB · RustRaw
//! The read-pool starvation of 16 Oct 2026 (readpool), reproduced on a//! GENERATED household — the committed synthetic generator's `huge` plan//! (`cotton_tools::synthetic`, a million rows: messages, photos, documents,//! media) seeded into a phone's own copy — so a build shows whether Home still//! falls to it. Not a guard: a proof run by hand, in release://!//! ```text//! cargo test --release -p cotton-ffi --lib read_pool_household_tests -- --ignored --nocapture//! ```//!//! HomeSync asked `records.list {table:"copy"}` on a timer and a slow ask was//! joined by the next, until every read connection of the file was held;//! Home's `read.home` waited 30 s and was refused, and the fan fell to Me and//! Tools. This starts the copy list every half second whether or not the last//! has answered, asks Home and the catalogue beside them every five seconds,//! and prints one line per answer. Every row is synthetic.use std::sync::Arc;use std::time::{Duration, Instant};use serde_json::{json, Map, Value};use crate::shell::client::phone_tests::attached;use crate::shell::client::ClientController;const FOR: Duration = Duration::from_secs(45);const COPY_EVERY: Duration = Duration::from_millis(500);const HOME_EVERY: Duration = Duration::from_secs(5);fn said(what: &str, began: Instant, asked: Instant, answer: Result<Value, String>) {    let outcome = match answer {        Ok(_) => "ok".to_string(),        Err(error) => format!("REFUSED {}", error.chars().take(160).collect::<String>()),    };    println!(        "{:>7.1} s  {what:<24} took {:>7.2} s  {outcome}",        asked.duration_since(began).as_secs_f64(),        asked.elapsed().as_secs_f64()    );}fn home(client: &ClientController) -> Result<Value, String> {    let entry = crate::phone::reads::read_for("read.home").expect("Home's read");    crate::phone::answer_read(client, entry, &Map::new())}fn copies(client: &ClientController) -> Result<Value, String> {    crate::phone::records::call(client, "records.list", &json!({ "table": "copy", "page": 0, "limit": 50 }))        .map_err(|error| format!("{}: {}", error.code, error.message))}#[test]#[ignore = "a proof over a generated million-row household; run by hand with --release --ignored"]fn home_beside_stacked_copy_lists_on_a_generated_household() {    let (_dir, client) = attached();    let path = client.layout().replica(crate::fixture::WORKSPACE);    let owner = client.with_file(|file| Ok(file.owner_id.to_string())).expect("the copy names its owner");    client.detach();    let started = Instant::now();    {        let db = cotton_store::driver::Driver::open(&path).expect("the copy opens");        // A phone fixture's copy has no calendar to file events into, and        // the seeder's event shapes are written whatever the plan says: one        // calendar first, as the app's first open makes.        db.exec(&format!(            "INSERT OR IGNORE INTO cotton_collections (id, workspace_id, kind, name, is_default, created_at) \             VALUES ('cal-proof', '{}', 'calendar', 'Synthetic', 1, '2026-01-01T00:00:00Z')",            crate::fixture::WORKSPACE        ))        .expect("the copy takes a calendar");        let plan = cotton_tools::synthetic::Plan::named("huge").expect("the huge plan");        let counts = cotton_tools::synthetic::seed_household(&db, &owner, &plan, 7).expect("the household seeds");        println!("seeded {} rows in {:.0?}", counts.total(), started.elapsed());    }    println!("file {:.2} GB", std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0) as f64 / 1e9);    let client: Arc<ClientController> = client;    let began = Instant::now();    let until = began + FOR;    std::thread::scope(|scope| {        scope.spawn(|| {            while Instant::now() < until {                scope.spawn(|| {                    let asked = Instant::now();                    said("records.list copy", began, asked, copies(&client));                });                std::thread::sleep(COPY_EVERY);            }        });        std::thread::sleep(Duration::from_secs(2));        while Instant::now() < until {            scope.spawn(|| {                let asked = Instant::now();                said("read.home", began, asked, home(&client));            });            scope.spawn(|| {                let asked = Instant::now();                said(                    "definitions.catalogue",                    began,                    asked,                    crate::phone::catalogue::call(&client, &json!({})).map_err(|error| error.message),                );            });            std::thread::sleep(HOME_EVERY);        }    });    // The copy's open, step by step, as This phone's diagnostics list it — the    // numbers a switch waits on when a short read goes through the open.    for line in crate::shell::diagnostics::open_lines() {        println!("open: {line}");    }}