forked from seanmonstar/reqwest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial import of PR seanmonstar#1623
- Loading branch information
Showing
8 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#![cfg(not(target_arch = "wasm32"))] | ||
use std::convert::Infallible; | ||
use std::future::Future; | ||
use std::path::PathBuf; | ||
use std::sync::mpsc as std_mpsc; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use tokio::sync::oneshot; | ||
|
||
pub use http::Response; | ||
use hyperlocal::UnixServerExt; | ||
use tokio::runtime; | ||
|
||
pub struct Server { | ||
panic_rx: std_mpsc::Receiver<()>, | ||
shutdown_tx: Option<oneshot::Sender<()>>, | ||
} | ||
|
||
impl Drop for Server { | ||
fn drop(&mut self) { | ||
if let Some(tx) = self.shutdown_tx.take() { | ||
let _ = tx.send(()); | ||
} | ||
|
||
if !::std::thread::panicking() { | ||
self.panic_rx | ||
.recv_timeout(Duration::from_secs(3)) | ||
.expect("test server should not panic"); | ||
} | ||
} | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn http<F, Fut>(socket: PathBuf, func: F) -> Server | ||
where | ||
F: Fn(http::Request<hyper::Body>) -> Fut + Clone + Send + 'static, | ||
Fut: Future<Output = http::Response<hyper::Body>> + Send + 'static, | ||
{ | ||
//Spawn new runtime in thread to prevent reactor execution context conflict | ||
thread::spawn(move || { | ||
let rt = runtime::Builder::new_current_thread() | ||
.enable_all() | ||
.build() | ||
.expect("new rt"); | ||
let srv = rt.block_on(async move { | ||
hyper::Server::bind_unix(socket).unwrap().serve(hyper::service::make_service_fn( | ||
move |_| { | ||
let func = func.clone(); | ||
async move { | ||
Ok::<_, Infallible>(hyper::service::service_fn(move |req| { | ||
let fut = func(req); | ||
async move { Ok::<_, Infallible>(fut.await) } | ||
})) | ||
} | ||
}, | ||
)) | ||
}); | ||
|
||
let (shutdown_tx, shutdown_rx) = oneshot::channel(); | ||
let srv = srv.with_graceful_shutdown(async move { | ||
let _ = shutdown_rx.await; | ||
}); | ||
|
||
let (panic_tx, panic_rx) = std_mpsc::channel(); | ||
let tname = format!( | ||
"test({})-support-server", | ||
thread::current().name().unwrap_or("<unknown>") | ||
); | ||
thread::Builder::new() | ||
.name(tname) | ||
.spawn(move || { | ||
rt.block_on(srv).unwrap(); | ||
let _ = panic_tx.send(()); | ||
}) | ||
.expect("thread spawn"); | ||
|
||
Server { | ||
panic_rx, | ||
shutdown_tx: Some(shutdown_tx), | ||
} | ||
}) | ||
.join() | ||
.unwrap() | ||
} |