From 7817e27e93b7563fcb4b5fa9ce850ea13b68eedb Mon Sep 17 00:00:00 2001 From: Tim Bruijnzeels Date: Mon, 3 Dec 2018 11:31:46 +0100 Subject: [PATCH] Using reqwest instead of hyper, because synchronous requests are all we need here, both in testing and for the publication client --- Cargo.toml | 2 +- src/bin/pubd.rs | 14 ++------ src/lib.rs | 1 - src/pubd/http.rs | 74 ++++++++------------------------------- tests/integration_test.rs | 72 ++++--------------------------------- 5 files changed, 24 insertions(+), 139 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 957f1b4f..fc737b39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,13 @@ bcder = "^0.1" bytes = "^0.4" chrono = { version = "^0.4", features = ["serde"] } clap = "^2.32" -hyper = "0.12" futures = "0.1" failure = "^0.1" lazy_static = "^1.1" openssl = "^0.10" pretty = "0.5.2" rand = "^0.5" +reqwest = "^0.9" rpki = { version = "^0.2", features = ["softkeys"] } serde = { version = "^1.0", features = ["rc"] } serde_derive = "^1.0" diff --git a/src/bin/pubd.rs b/src/bin/pubd.rs index 738da9dd..98a86e2c 100644 --- a/src/bin/pubd.rs +++ b/src/bin/pubd.rs @@ -2,9 +2,8 @@ extern crate actix; extern crate rpubd; #[macro_use] extern crate lazy_static; +extern crate core; -use std::thread; -use actix::System; use rpubd::pubd::config::Config; use rpubd::pubd::http::PubServerApp; @@ -21,14 +20,5 @@ lazy_static! { } fn main() { - // Start the server - thread::spawn(||{ - System::run(move || { - PubServerApp::serve(&CONFIG) - }) - }); - - loop { - // wait forever - } + PubServerApp::run(&CONFIG) } diff --git a/src/lib.rs b/src/lib.rs index d0276270..1b2fba07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,6 @@ extern crate serde_json; extern crate tokio; extern crate toml; extern crate xml; -extern crate hyper; pub mod file; pub mod provisioning; diff --git a/src/pubd/http.rs b/src/pubd/http.rs index 71836fb1..0fb176c4 100644 --- a/src/pubd/http.rs +++ b/src/pubd/http.rs @@ -69,15 +69,28 @@ impl PubServerApp { Arc::new(RwLock::new(pub_server)) } - pub fn serve(config: &Config) { + /// Used to start the server with an existing executor (e.g. in tests) + pub fn start(config: &Config) { let ps = PubServerApp::create_server(config); server::new(move || PubServerApp::new(ps.clone())) .bind(config.socket_addr()) .expect(&format!("Cannot bind to: {}", config.socket_addr())) + .shutdown_timeout(0) .start(); } + /// Used to run the server in blocking mode, from the main method. + pub fn run(config: &Config) { + let ps = PubServerApp::create_server(config); + + server::new(move || PubServerApp::new(ps.clone())) + .bind(config.socket_addr()) + .expect(&format!("Cannot bind to: {}", config.socket_addr())) + .shutdown_timeout(0) + .run(); + } + /// 404 handler fn p404(_req: &HttpRequest) -> HttpResponse { HttpResponse::build(StatusCode::NOT_FOUND) @@ -179,61 +192,4 @@ pub enum Error { //------------ Tests --------------------------------------------------------- -#[cfg(test)] -mod tests { - use super::*; - use test; - use pubc::client::PubClient; - use rpki::oob::exchange::PublisherRequest; - use std::path::PathBuf; - use std::fs::File; - use std::io::Write; - use std::thread; - use actix::System; - use std::time; - - fn save_pr(base_dir: &PathBuf, file_name: &str, pr: &PublisherRequest) { - let mut full_name = base_dir.clone(); - full_name.push(PathBuf::from - (file_name)); - let mut f = File::create(full_name).unwrap(); - let xml = pr.encode_vec(); - f.write(xml.as_ref()).unwrap(); - } - - #[test] - fn start() { - test::test_with_tmp_dir(|d| { - - // Set up a test PubServer Config with a client in it. - let server_conf = { - // Use a data dir for the storage - let data_dir = test::create_sub_dir(&d); - let xml_dir = test::create_sub_dir(&d); - - // Set up a client - let client_dir = test::create_sub_dir(&d); - let mut client = PubClient::new(&client_dir).unwrap(); - client.init("client".to_string()).unwrap(); - let pr = client.publisher_request().unwrap(); - - // Add the client's PublisherRequest to the server dir. - save_pr(&xml_dir, "client.xml", &pr); - Config::test(&data_dir, &xml_dir) - }; - - // Start the server - thread::spawn(||{ - System::run(move || { - PubServerApp::serve(&server_conf) - }) - }); - - // Wait for server to boot.. - thread::sleep(time::Duration::from_secs(10)); - - - }); - } - -} \ No newline at end of file +// Tested in tests/integration_test.rs \ No newline at end of file diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 428940bb..5f368620 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -1,24 +1,21 @@ +extern crate actix; extern crate futures; -extern crate hyper; +extern crate reqwest; extern crate rpki; extern crate rpubd; extern crate serde_json; extern crate tokio; -extern crate actix; use std::fs::File; use std::io::Write; use std::path::PathBuf; use std::str; use std::{thread, time}; -use hyper::Client; use rpki::oob::exchange::PublisherRequest; use rpubd::test; use rpubd::pubc::client::PubClient; use rpubd::pubd::config::Config; use rpubd::provisioning::publisher::Publisher; -use tokio::prelude::*; -use tokio::runtime::Runtime; use rpubd::pubd::http::PubServerApp; use actix::System; @@ -55,73 +52,16 @@ fn testing() { // Start the server thread::spawn(||{ System::run(move || { - PubServerApp::serve(&server_conf) + PubServerApp::start(&server_conf); }) }); - - let mut rt = Runtime::new().unwrap(); // XXX TODO: Find a better way to know the server is ready! thread::sleep(time::Duration::from_millis(150)); - // XXX TODO: Use a helper to create the futures to check the - // XXX TODO: responses.. the compiler insists this crosses threads - // XXX TODO: and isn't safe.. -// fn test(url: &str, test: F) -> impl Future -// where F: Fn(&str)-> () + Send { -// let url = url.parse().unwrap(); -// let client = Client::new(); -// -// client -// .get(url) -// .and_then(|res| { -// res.into_body().concat2() -// }) -// .and_then(|body| { -// let s = str::from_utf8(&body).unwrap(); -// test(s); -// Ok(()) -// }) -// .map_err(|e| { -// println!("{}", e); -// }) -// }; -// -// let fut = test( -// "http://localhost:3000/publishers", -// |s| { -// let pl: Vec = serde_json::from_str(s).unwrap(); -// assert_eq!(2, pl.len()); -// } -// ); - - // XXX TODO: Extract some function, or macro?!, see above.. - // XXX TODO: for now doing this the verbose way for each test. - - let url = "http://localhost:3000/publishers".parse().unwrap(); - let client = Client::new(); - - let fut = client - .get(url) - .and_then(|res| { - res.into_body().concat2() - }) - .and_then(|body| { - let pl: Vec = serde_json::from_str( - str::from_utf8(&body).unwrap() - ).unwrap(); - assert_eq!(1, pl.len()); - Ok(()) - }) - .map_err(|e| { - println!("{}", e); - }); - - rt.block_on(fut).unwrap(); - - - - rt.shutdown_now(); + let mut res = reqwest::get("http://localhost:3000/publishers").unwrap(); + let pl: Vec = serde_json::from_str(&res.text().unwrap()).unwrap(); + assert_eq!(1, pl.len()); }); }