|
| 1 | +use http_body_util::StreamBody; |
| 2 | +use hyper::body::Bytes; |
| 3 | +use hyper::body::Frame; |
| 4 | +use hyper::server::conn::http1; |
| 5 | +use hyper::service::service_fn; |
| 6 | +use hyper::{Response, StatusCode}; |
| 7 | +use std::convert::Infallible; |
| 8 | +use std::time::Duration; |
| 9 | +use tokio::sync::mpsc; |
| 10 | +use tokio::time::timeout; |
| 11 | +use tracing::{error, info}; |
| 12 | + |
| 13 | +pub struct TestConfig { |
| 14 | + pub total_chunks: usize, |
| 15 | + pub chunk_size: usize, |
| 16 | + pub chunk_timeout: Duration, |
| 17 | +} |
| 18 | + |
| 19 | +impl TestConfig { |
| 20 | + pub fn with_timeout(chunk_timeout: Duration) -> Self { |
| 21 | + Self { |
| 22 | + total_chunks: 16, |
| 23 | + chunk_size: 64 * 1024, |
| 24 | + chunk_timeout, |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub struct Client { |
| 30 | + pub rx: mpsc::UnboundedReceiver<Vec<u8>>, |
| 31 | + pub tx: mpsc::UnboundedSender<Vec<u8>>, |
| 32 | +} |
| 33 | + |
| 34 | +pub async fn run<S>(server: S, mut client: Client, config: TestConfig) |
| 35 | +where |
| 36 | + S: hyper::rt::Read + hyper::rt::Write + Send + Unpin + 'static, |
| 37 | +{ |
| 38 | + let mut http_builder = http1::Builder::new(); |
| 39 | + http_builder.max_buf_size(config.chunk_size); |
| 40 | + |
| 41 | + let total_chunks = config.total_chunks; |
| 42 | + let chunk_size = config.chunk_size; |
| 43 | + |
| 44 | + let service = service_fn(move |_| { |
| 45 | + let total_chunks = total_chunks; |
| 46 | + let chunk_size = chunk_size; |
| 47 | + async move { |
| 48 | + info!( |
| 49 | + "Creating payload of {} chunks of {} KiB each ({} MiB total)...", |
| 50 | + total_chunks, |
| 51 | + chunk_size / 1024, |
| 52 | + total_chunks * chunk_size / (1024 * 1024) |
| 53 | + ); |
| 54 | + let bytes = Bytes::from(vec![0; chunk_size]); |
| 55 | + let data = vec![bytes.clone(); total_chunks]; |
| 56 | + let stream = futures_util::stream::iter( |
| 57 | + data.into_iter() |
| 58 | + .map(|b| Ok::<_, Infallible>(Frame::data(b))), |
| 59 | + ); |
| 60 | + let body = StreamBody::new(stream); |
| 61 | + info!("Server: Sending data response..."); |
| 62 | + Ok::<_, hyper::Error>( |
| 63 | + Response::builder() |
| 64 | + .status(StatusCode::OK) |
| 65 | + .header("content-type", "application/octet-stream") |
| 66 | + .header("content-length", (total_chunks * chunk_size).to_string()) |
| 67 | + .body(body) |
| 68 | + .unwrap(), |
| 69 | + ) |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + let server_task = tokio::spawn(async move { |
| 74 | + let conn = http_builder.serve_connection(Box::pin(server), service); |
| 75 | + let conn_result = conn.await; |
| 76 | + if let Err(e) = &conn_result { |
| 77 | + error!("Server connection error: {}", e); |
| 78 | + } |
| 79 | + conn_result |
| 80 | + }); |
| 81 | + |
| 82 | + let get_request = "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; |
| 83 | + client |
| 84 | + .tx |
| 85 | + .send(get_request.as_bytes().to_vec()) |
| 86 | + .map_err(|e| { |
| 87 | + Box::new(std::io::Error::new( |
| 88 | + std::io::ErrorKind::Other, |
| 89 | + format!("Failed to send request: {}", e), |
| 90 | + )) |
| 91 | + }) |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + info!("Client is reading response..."); |
| 95 | + let mut bytes_received = 0; |
| 96 | + let mut all_data = Vec::new(); |
| 97 | + loop { |
| 98 | + match timeout(config.chunk_timeout, client.rx.recv()).await { |
| 99 | + Ok(Some(chunk)) => { |
| 100 | + bytes_received += chunk.len(); |
| 101 | + all_data.extend_from_slice(&chunk); |
| 102 | + } |
| 103 | + Ok(None) => break, |
| 104 | + Err(_) => { |
| 105 | + panic!( |
| 106 | + "Chunk timeout: chunk took longer than {:?}", |
| 107 | + config.chunk_timeout |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Clean up |
| 114 | + let result = server_task.await.unwrap(); |
| 115 | + result.unwrap(); |
| 116 | + |
| 117 | + // Parse HTTP response to find body start |
| 118 | + // HTTP response format: "HTTP/1.1 200 OK\r\n...headers...\r\n\r\n<body>" |
| 119 | + let body_start = all_data |
| 120 | + .windows(4) |
| 121 | + .position(|w| w == b"\r\n\r\n") |
| 122 | + .map(|pos| pos + 4) |
| 123 | + .unwrap_or(0); |
| 124 | + |
| 125 | + let body_bytes = bytes_received - body_start; |
| 126 | + assert_eq!( |
| 127 | + body_bytes, |
| 128 | + config.total_chunks * config.chunk_size, |
| 129 | + "Expected {} body bytes, got {} (total received: {}, headers: {})", |
| 130 | + config.total_chunks * config.chunk_size, |
| 131 | + body_bytes, |
| 132 | + bytes_received, |
| 133 | + body_start |
| 134 | + ); |
| 135 | + info!(bytes_received, body_bytes, "Client done receiving bytes"); |
| 136 | +} |
0 commit comments