Files
Kigi-CLI/crates/codegen/kigi-test-support/src/counting_server.rs
T
ZacharyZhang-NY a02b555e66 docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment
guidelines: delete redundant restatements, decorative banners, change
narration, and end-of-line comments; keep and tighten the crucial ones
(invariants, bug rationale, SAFETY blocks, ported-source attribution).

No functional code changed. Every edit is proven comment-only against the
prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a
separate doctest-fence check. Where removing a comment made rustfmt or clippy
want to re-lay-out adjacent code, the minimal triggering comment is restored so
code tokens stay byte-identical.

Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy
--workspace --all-targets (0 warnings).

Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for
these guidelines (flags banners, end-of-line comments, change narration, and
commented-out code).
2026-07-23 16:55:39 -04:00

68 lines
3.0 KiB
Rust

//! Minimal keep-alive HTTP/1.1 server for wire-level tests that assert TCP
//! connection reuse (e.g. shared-client pooling): it counts accepted
//! connections and records each request's header block.
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
/// Returns the base URL, the accepted-connection count, and the header blocks.
pub async fn spawn_counting_server() -> (String, Arc<AtomicUsize>, Arc<Mutex<Vec<String>>>) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let base_url = format!("http://{}/v1", listener.local_addr().unwrap());
let accepts = Arc::new(AtomicUsize::new(0));
let heads: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
let (accepts_l, heads_l) = (Arc::clone(&accepts), Arc::clone(&heads));
tokio::spawn(async move {
loop {
let Ok((mut sock, _)) = listener.accept().await else {
return;
};
accepts_l.fetch_add(1, Ordering::SeqCst);
let heads = Arc::clone(&heads_l);
tokio::spawn(async move {
let mut buf: Vec<u8> = Vec::new();
loop {
// Read one full request: header block, then content-length body bytes.
let head_end = loop {
if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
break i + 4;
}
let mut chunk = [0u8; 4096];
match sock.read(&mut chunk).await {
Ok(0) | Err(_) => return,
Ok(n) => buf.extend_from_slice(&chunk[..n]),
}
};
let head = String::from_utf8_lossy(&buf[..head_end]).to_string();
let body_len: usize = head
.lines()
.find_map(|l| {
l.to_ascii_lowercase()
.strip_prefix("content-length:")
.and_then(|v| v.trim().parse().ok())
})
.unwrap_or(0);
while buf.len() < head_end + body_len {
let mut chunk = [0u8; 4096];
match sock.read(&mut chunk).await {
Ok(0) | Err(_) => return,
Ok(n) => buf.extend_from_slice(&chunk[..n]),
}
}
buf.drain(..head_end + body_len);
heads.lock().unwrap().push(head);
let resp =
b"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: 2\r\n\r\n{}";
if sock.write_all(resp).await.is_err() {
return;
}
}
});
}
});
(base_url, accepts, heads)
}