use super::*; #[derive(serde::Deserialize)] #[serde(deny_unknown_fields)] struct Config { module: Module, } #[derive(serde::Deserialize)] #[serde(deny_unknown_fields)] struct Module { main: Domain, } #[derive(serde::Deserialize)] #[serde(deny_unknown_fields)] struct Domain { main: Settings, } #[derive(serde::Deserialize)] #[serde(deny_unknown_fields)] struct Settings { port: u16, } pub(super) fn run() { let mut args = std::env::args().skip(1); let mode = args.next().unwrap(); let count: i64 = args.next().unwrap().parse().unwrap(); if mode == "server" { tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap() .block_on(async { let owner = Owner::default(); let listener = tokio::net::TcpListener::bind(("127.0.0.1", u16::try_from(count).unwrap())) .await .unwrap(); server::serve(listener, CancellationToken::new(), owner.clone()) .await .unwrap(); assert!(owner.take_faults().is_empty()); }); return; } let config: Config = serde_json::from_slice(&std::fs::read(args.next().unwrap()).unwrap()).unwrap(); let port = config.module.main.main.port; tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap() .block_on(async { if mode == "http" { let address = SocketAddr::from(([127, 0, 0, 1], port)); let mut total = 0i64; for value in 0..count { let body = Bytes::from(value.to_string()); let request = Request::builder() .method("POST") .uri("/echo") .header("Host", "localhost") .body(Full::new(body.clone())) .unwrap(); let response = single::send(address, request).await.unwrap(); assert_eq!(response.status(), 200); assert_eq!(response.into_body(), body); total = total.checked_add(1).unwrap(); } println!("{total}"); return; } let pool = Pool::new("127.0.0.1".into(), port, 32).await.unwrap(); let uri: Uri = format!("http://127.0.0.1:{port}/echo").parse().unwrap(); let result = AssertUnwindSafe(async { let mut total = 0i64; for value in 0..count { let body = Bytes::from(value.to_string()); let request = Request::builder() .method("POST") .uri(uri.clone()) .body(Full::new(body.clone())) .unwrap(); if mode == "http_pool" { let response = pool.request(request).await.unwrap(); assert_eq!(response.status(), 200); assert_eq!(response.into_body(), body); } else { assert_eq!(mode, "http_stream"); let (parts, download) = pool.open(request).await.unwrap(); assert_eq!(parts.status, 200); let _close = download.close_on_drop(); let mut offset = 0; while let Some(chunk) = download.next().await.unwrap() { let end = offset + chunk.len(); assert_eq!(chunk, body.slice(offset..end)); offset = end; } assert_eq!(offset, body.len()); } total = total.checked_add(1).unwrap(); } total }) .catch_unwind() .await; pool.close().await; assert!(pool.owner.tasks.is_empty()); assert!(pool.owner.take_faults().is_empty()); assert_eq!(pool.physical.available_permits(), 32); match result { Ok(total) => println!("{total}"), Err(fault) => std::panic::resume_unwind(fault), } }); }