1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Fix test server listener thread leak (#655)

This commit is contained in:
Julian Tescher 2019-01-08 10:42:22 -08:00 committed by Nikolay Kim
parent 4f2e970732
commit e5cdd22720
2 changed files with 17 additions and 7 deletions

View File

@ -12,6 +12,8 @@
* StaticFiles decode special characters in request's path * StaticFiles decode special characters in request's path
* Fix test server listener leak #654
## [0.7.17] - 2018-12-25 ## [0.7.17] - 2018-12-25
### Added ### Added

View File

@ -5,7 +5,9 @@ use std::sync::mpsc;
use std::{net, thread}; use std::{net, thread};
use actix::{Actor, Addr, System}; use actix::{Actor, Addr, System};
use actix::actors::signal;
use actix_net::server::Server;
use cookie::Cookie; use cookie::Cookie;
use futures::Future; use futures::Future;
use http::header::HeaderName; use http::header::HeaderName;
@ -66,6 +68,7 @@ pub struct TestServer {
ssl: bool, ssl: bool,
conn: Addr<ClientConnector>, conn: Addr<ClientConnector>,
rt: Runtime, rt: Runtime,
backend: Addr<Server>,
} }
impl TestServer { impl TestServer {
@ -112,24 +115,25 @@ impl TestServer {
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap(); let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
let local_addr = tcp.local_addr().unwrap(); let local_addr = tcp.local_addr().unwrap();
let _ = HttpServer::new(factory) let srv = HttpServer::new(factory)
.disable_signals() .disable_signals()
.listen(tcp) .listen(tcp)
.keep_alive(5) .keep_alive(5)
.start(); .start();
tx.send((System::current(), local_addr, TestServer::get_conn())) tx.send((System::current(), local_addr, TestServer::get_conn(), srv))
.unwrap(); .unwrap();
sys.run(); sys.run();
}); });
let (system, addr, conn) = rx.recv().unwrap(); let (system, addr, conn, backend) = rx.recv().unwrap();
System::set_current(system); System::set_current(system);
TestServer { TestServer {
addr, addr,
conn, conn,
ssl: false, ssl: false,
rt: Runtime::new().unwrap(), rt: Runtime::new().unwrap(),
backend,
} }
} }
@ -197,6 +201,7 @@ impl TestServer {
/// Stop http server /// Stop http server
fn stop(&mut self) { fn stop(&mut self) {
let _ = self.backend.send(signal::Signal(signal::SignalType::Term)).wait();
System::current().stop(); System::current().stop();
} }
@ -333,8 +338,7 @@ where
.keep_alive(5) .keep_alive(5)
.disable_signals(); .disable_signals();
tx.send((System::current(), addr, TestServer::get_conn()))
.unwrap();
#[cfg(any(feature = "alpn", feature = "ssl"))] #[cfg(any(feature = "alpn", feature = "ssl"))]
{ {
@ -356,18 +360,22 @@ where
let tcp = net::TcpListener::bind(addr).unwrap(); let tcp = net::TcpListener::bind(addr).unwrap();
srv = srv.listen(tcp); srv = srv.listen(tcp);
} }
srv.start(); let backend = srv.start();
tx.send((System::current(), addr, TestServer::get_conn(), backend))
.unwrap();
sys.run(); sys.run();
}); });
let (system, addr, conn) = rx.recv().unwrap(); let (system, addr, conn, backend) = rx.recv().unwrap();
System::set_current(system); System::set_current(system);
TestServer { TestServer {
addr, addr,
conn, conn,
ssl: has_ssl, ssl: has_ssl,
rt: Runtime::new().unwrap(), rt: Runtime::new().unwrap(),
backend,
} }
} }
} }