aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / tests / courier_model_fixture.rs
| 1 | //! Loopback-only model fixture for ck_courier_udp_test.sh. No external runtime. |
| 2 | use std::io::{self, Read, Write}; |
| 3 | use std::net::{TcpListener, TcpStream, UdpSocket}; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | fn request(stream: &mut TcpStream) -> io::Result<String> { |
| 7 | stream.set_read_timeout(Some(Duration::from_secs(10)))?; |
| 8 | let mut bytes = Vec::new(); |
| 9 | let mut one = [0u8; 1]; |
| 10 | while !bytes.ends_with(b"\r\n\r\n") { |
| 11 | stream.read_exact(&mut one)?; |
| 12 | bytes.push(one[0]); |
| 13 | if bytes.len() > 8192 { |
| 14 | return Err(io::Error::other("oversized request header")); |
| 15 | } |
| 16 | } |
| 17 | let head = String::from_utf8_lossy(&bytes); |
| 18 | let length = head |
| 19 | .lines() |
| 20 | .filter_map(|line| line.split_once(':')) |
| 21 | .find(|(name, _)| name.eq_ignore_ascii_case("content-length")) |
| 22 | .and_then(|(_, value)| value.trim().parse::<usize>().ok()) |
| 23 | .filter(|&length| length <= 65536) |
| 24 | .ok_or_else(|| io::Error::other("missing or oversized request body"))?; |
| 25 | let mut body = vec![0; length]; |
| 26 | stream.read_exact(&mut body)?; |
| 27 | String::from_utf8(body).map_err(io::Error::other) |
| 28 | } |
| 29 | |
| 30 | fn main() -> io::Result<()> { |
| 31 | let args: Vec<String> = std::env::args().collect(); |
| 32 | if args.get(1).map(String::as_str) == Some("--free-udp-port") { |
| 33 | let socket = UdpSocket::bind("127.0.0.1:0")?; |
| 34 | println!("{}", socket.local_addr()?.port()); |
| 35 | return Ok(()); |
| 36 | } |
| 37 | let busy: usize = args[2].parse().expect("busy request count"); |
| 38 | let delay: u64 = args[3].parse().expect("delay in seconds"); |
| 39 | // Bind before publishing the port so the shell never races server startup. |
| 40 | let listener = TcpListener::bind("127.0.0.1:0")?; |
| 41 | std::fs::write(&args[1], listener.local_addr()?.port().to_string())?; |
| 42 | for (seen, stream) in listener.incoming().enumerate() { |
| 43 | let mut stream = stream?; |
| 44 | stream.set_write_timeout(Some(Duration::from_secs(10)))?; |
| 45 | let body = request(&mut stream)?; |
| 46 | let system = body |
| 47 | .strip_prefix("{\"messages\":[{\"role\":\"system\",\"content\":") |
| 48 | .and_then(|rest| rest.split_once("},{\"role\":\"user\"")) |
| 49 | .map(|(system, _)| system) |
| 50 | .expect("courier must send its system message first"); |
| 51 | assert!(system.starts_with('"') && system.contains("Allen")); |
| 52 | if seen < busy { |
| 53 | stream.write_all(b"HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")?; |
| 54 | continue; |
| 55 | } |
| 56 | std::thread::sleep(Duration::from_secs(delay)); |
| 57 | stream.write_all( |
| 58 | b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n", |
| 59 | )?; |
| 60 | for word in "I am here. The rain is soft tonight and I like listening to it with you. Tell me more about your day when you are ready.".split(' ') { |
| 61 | // Fixture words contain no JSON escapes. |
| 62 | write!(stream, "data: {{\"choices\":[{{\"delta\":{{\"content\":\"{word} \"}}}}]}}\n\n")?; |
| 63 | stream.flush()?; |
| 64 | std::thread::sleep(Duration::from_millis(30)); |
| 65 | } |
| 66 | stream.write_all(b"data: [DONE]\n\n")?; |
| 67 | } |
| 68 | Ok(()) |
| 69 | } |