2019-09-17 16:04:20 +06:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::{net, thread};
|
|
|
|
|
2019-12-02 11:49:42 +06:00
|
|
|
use actix_rt::{net::TcpStream, System};
|
2019-09-17 16:04:20 +06:00
|
|
|
|
2020-12-30 05:20:24 +08:00
|
|
|
use crate::{Server, ServerBuilder, ServiceFactory};
|
2019-09-17 16:04:20 +06:00
|
|
|
|
|
|
|
/// The `TestServer` type.
|
|
|
|
///
|
|
|
|
/// `TestServer` is very simple test server that simplify process of writing
|
|
|
|
/// integration tests for actix-net applications.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2021-01-09 14:13:16 +00:00
|
|
|
/// ```
|
2019-12-08 19:05:05 +06:00
|
|
|
/// use actix_service::fn_service;
|
2020-12-30 05:20:24 +08:00
|
|
|
/// use actix_server::TestServer;
|
2019-09-17 16:04:20 +06:00
|
|
|
///
|
2019-12-05 16:40:24 +06:00
|
|
|
/// #[actix_rt::main]
|
|
|
|
/// async fn main() {
|
2019-12-08 19:05:05 +06:00
|
|
|
/// let srv = TestServer::with(|| fn_service(
|
2019-11-14 18:38:24 +06:00
|
|
|
/// |sock| async move {
|
2019-09-17 16:04:20 +06:00
|
|
|
/// println!("New connection: {:?}", sock);
|
|
|
|
/// Ok::<_, ()>(())
|
|
|
|
/// }
|
|
|
|
/// ));
|
|
|
|
///
|
|
|
|
/// println!("SOCKET: {:?}", srv.connect());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub struct TestServer;
|
|
|
|
|
2020-09-19 22:12:41 +08:00
|
|
|
/// Test server runtime
|
2019-09-17 16:04:20 +06:00
|
|
|
pub struct TestServerRuntime {
|
|
|
|
addr: net::SocketAddr,
|
|
|
|
host: String,
|
|
|
|
port: u16,
|
|
|
|
system: System,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestServer {
|
|
|
|
/// Start new server with server builder
|
2019-12-02 22:30:09 +06:00
|
|
|
pub fn start<F>(mut factory: F) -> TestServerRuntime
|
2019-09-17 16:04:20 +06:00
|
|
|
where
|
|
|
|
F: FnMut(ServerBuilder) -> ServerBuilder + Send + 'static,
|
|
|
|
{
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
// run server in separate thread
|
|
|
|
thread::spawn(move || {
|
2021-01-31 03:34:07 +00:00
|
|
|
let sys = System::new();
|
2021-01-09 14:13:16 +00:00
|
|
|
factory(Server::build()).workers(1).disable_signals().run();
|
2019-09-17 16:04:20 +06:00
|
|
|
|
|
|
|
tx.send(System::current()).unwrap();
|
|
|
|
sys.run()
|
|
|
|
});
|
|
|
|
let system = rx.recv().unwrap();
|
|
|
|
|
|
|
|
TestServerRuntime {
|
|
|
|
system,
|
|
|
|
addr: "127.0.0.1:0".parse().unwrap(),
|
|
|
|
host: "127.0.0.1".to_string(),
|
|
|
|
port: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start new test server with application factory
|
2019-11-14 18:38:24 +06:00
|
|
|
pub fn with<F: ServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
|
2019-09-17 16:04:20 +06:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
// run server in separate thread
|
|
|
|
thread::spawn(move || {
|
2021-01-31 03:34:07 +00:00
|
|
|
let sys = System::new();
|
2019-09-17 16:04:20 +06:00
|
|
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
let local_addr = tcp.local_addr().unwrap();
|
|
|
|
|
2020-12-27 07:26:02 +08:00
|
|
|
sys.block_on(async {
|
|
|
|
Server::build()
|
|
|
|
.listen("test", tcp, factory)
|
|
|
|
.unwrap()
|
|
|
|
.workers(1)
|
|
|
|
.disable_signals()
|
2021-01-09 14:13:16 +00:00
|
|
|
.run();
|
2020-12-29 07:44:53 +08:00
|
|
|
tx.send((System::current(), local_addr)).unwrap();
|
2020-12-27 07:26:02 +08:00
|
|
|
});
|
2019-09-17 16:04:20 +06:00
|
|
|
sys.run()
|
|
|
|
});
|
|
|
|
|
|
|
|
let (system, addr) = rx.recv().unwrap();
|
|
|
|
|
|
|
|
let host = format!("{}", addr.ip());
|
|
|
|
let port = addr.port();
|
|
|
|
|
|
|
|
TestServerRuntime {
|
|
|
|
addr,
|
|
|
|
host,
|
|
|
|
port,
|
2021-03-26 17:20:17 -07:00
|
|
|
system,
|
2019-09-17 16:04:20 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:12:41 +08:00
|
|
|
/// Get first available unused local address
|
2019-09-17 16:04:20 +06:00
|
|
|
pub fn unused_addr() -> net::SocketAddr {
|
|
|
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
2020-12-30 05:20:24 +08:00
|
|
|
let socket = mio::net::TcpSocket::new_v4().unwrap();
|
|
|
|
socket.bind(addr).unwrap();
|
|
|
|
socket.set_reuseaddr(true).unwrap();
|
|
|
|
let tcp = socket.listen(1024).unwrap();
|
2019-09-17 16:04:20 +06:00
|
|
|
tcp.local_addr().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestServerRuntime {
|
|
|
|
/// Test server host
|
|
|
|
pub fn host(&self) -> &str {
|
|
|
|
&self.host
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test server port
|
|
|
|
pub fn port(&self) -> u16 {
|
|
|
|
self.port
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get test server address
|
|
|
|
pub fn addr(&self) -> net::SocketAddr {
|
|
|
|
self.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop http server
|
|
|
|
fn stop(&mut self) {
|
|
|
|
self.system.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect to server, return tokio TcpStream
|
|
|
|
pub fn connect(&self) -> std::io::Result<TcpStream> {
|
2019-12-05 16:40:24 +06:00
|
|
|
TcpStream::from_std(net::TcpStream::connect(self.addr)?)
|
2019-09-17 16:04:20 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestServerRuntime {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.stop()
|
|
|
|
}
|
|
|
|
}
|