Skip to content

Commit

Permalink
Fix test build. I don't know why this works and the other way didn't.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorendorff committed Jan 24, 2024
1 parent ba5c8d3 commit fca9663
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/twirp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ pin-project = "1.1"
# For the test-support feature
async-trait = { version = "0.1", optional = true }
tokio = { version = "1.33", features = [], optional = true }

[dev-dependencies]
tokio = { version = "1.33", features = ["rt", "macros"] }
54 changes: 28 additions & 26 deletions crates/twirp/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,54 @@ use async_trait::async_trait;
use http_body_util::BodyExt;
use hyper::body::Incoming;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::Request;
use hyper::service::{service_fn, Service};
use hyper::{Request, Response};
use hyper_util::rt::TokioIo;
use serde::de::DeserializeOwned;
use tokio::net::{TcpListener, TcpStream};
use tokio::net::TcpListener;
use tokio::task::JoinHandle;

use crate::{error, Body, Client, Result, Router, TwirpErrorResponse};
use crate::{error, Body, Client, GenericError, Result, Router, TwirpErrorResponse};

async fn test_server_handle_connection(io: TokioIo<TcpStream>, router: Arc<Router>) {
let service = service_fn(move |req: Request<Incoming>| {
let router = Arc::clone(&router);
async move {
let req = req.map(|body| Body::new(body));
crate::serve(router.clone(), req).await
}
});
if let Err(err) = http1::Builder::new().serve_connection(io, service).await {
eprintln!("Error serving connection: {err:?}");
}
}

async fn test_server_main(
tcp_listener: TcpListener,
router: Arc<Router>,
) -> Result<(), std::io::Error> {
async fn test_server_main<S>(tcp_listener: TcpListener, service: S) -> Result<(), std::io::Error>
where
S: Clone + Service<Request<Incoming>, Response = Response<Body>> + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<GenericError>,
{
loop {
let (stream, _) = tcp_listener.accept().await?;
let io = TokioIo::new(stream);
let router = Arc::clone(&router);
tokio::spawn(test_server_handle_connection(io, router));
let service = service.clone();
let task = async move {
if let Err(err) = http1::Builder::new().serve_connection(io, service).await {
eprintln!("test server: error serving connection: {err:#}");
}
};
tokio::spawn(task);
}
}

pub async fn run_test_server(port: u16) -> JoinHandle<Result<(), std::io::Error>> {
let router = test_api_router().await;
let router = test_api_router();
let service = service_fn(move |req: Request<Incoming>| {
let router = Arc::clone(&router);
async move {
let req = req.map(Body::new);
crate::serve(router, req).await
}
});

let addr: SocketAddr = ([127, 0, 0, 1], port).into();
let tcp_listener = TcpListener::bind(&addr).await.unwrap();
let server = test_server_main(tcp_listener, router);
let server = test_server_main(tcp_listener, service);
println!("Listening on {addr}");
let h = tokio::spawn(server);
tokio::time::sleep(Duration::from_millis(100)).await;
h
}

pub async fn test_api_router() -> Arc<Router> {
pub fn test_api_router() -> Arc<Router> {
let api = Arc::new(TestAPIServer {});
let mut router = Router::default();
// NB: This would be generated
Expand Down

0 comments on commit fca9663

Please sign in to comment.