Add Server to TestServer (#383)

* Add Server to TestServer

* Version bump

---------

Co-authored-by: James Bell <jamesbell@microsoft.com>
This commit is contained in:
jamescarterbell 2024-07-16 16:40:02 -04:00 committed by GitHub
parent d39611d246
commit 7c6235cdc4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 16 deletions

View file

@ -1,5 +1,9 @@
# Changes # Changes
## [2.3.0] - 2024-07-16
* Add Server to TestServer
## [2.2.0] - 2024-07-16 ## [2.2.0] - 2024-07-16
* Expose TetServer::stop() method * Expose TetServer::stop() method

View file

@ -1,6 +1,6 @@
[package] [package]
name = "ntex-server" name = "ntex-server"
version = "2.2.0" version = "2.3.0"
authors = ["ntex contributors <team@ntex.rs>"] authors = ["ntex contributors <team@ntex.rs>"]
description = "Server for ntex framework" description = "Server for ntex framework"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]

View file

@ -1,12 +1,12 @@
//! Test server //! Test server
use std::{io, net, sync::mpsc, thread}; use std::{io, net, thread};
use ntex_net::{tcp_connect, Io}; use ntex_net::{tcp_connect, Io};
use ntex_rt::System; use ntex_rt::System;
use ntex_service::ServiceFactory; use ntex_service::ServiceFactory;
use socket2::{Domain, SockAddr, Socket, Type}; use socket2::{Domain, SockAddr, Socket, Type};
use super::ServerBuilder; use super::{Server, ServerBuilder};
/// Start test server /// Start test server
/// ///
@ -44,29 +44,34 @@ where
F: Fn() -> R + Send + Clone + 'static, F: Fn() -> R + Send + Clone + 'static,
R: ServiceFactory<Io> + 'static, R: ServiceFactory<Io> + 'static,
{ {
let (tx, rx) = mpsc::channel(); let (tx, rx) = oneshot::channel();
// run server in separate thread // run server in separate thread
thread::spawn(move || { thread::spawn(move || {
let sys = System::new("ntex-test-server"); let sys = System::new("ntex-test-server");
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 system = sys.system();
tx.send((sys.system(), local_addr)).unwrap(); sys.run(move || {
sys.run(|| { let server = ServerBuilder::new()
ServerBuilder::new()
.listen("test", tcp, move |_| factory())? .listen("test", tcp, move |_| factory())?
.set_tag("test", "TEST-SERVER") .set_tag("test", "TEST-SERVER")
.workers(1) .workers(1)
.disable_signals() .disable_signals()
.run(); .run();
tx.send((system, local_addr, server))
.expect("Failed to send Server to TestServer");
Ok(()) Ok(())
}) })
}); });
let (system, addr) = rx.recv().unwrap(); let (system, addr, server) = rx.recv().unwrap();
TestServer { addr, system } TestServer {
addr,
server,
system,
}
} }
/// Start new server with server builder /// Start new server with server builder
@ -74,22 +79,24 @@ pub fn build_test_server<F>(factory: F) -> TestServer
where where
F: FnOnce(ServerBuilder) -> ServerBuilder + Send + 'static, F: FnOnce(ServerBuilder) -> ServerBuilder + Send + 'static,
{ {
let (tx, rx) = mpsc::channel(); let (tx, rx) = oneshot::channel();
// run server in separate thread // run server in separate thread
thread::spawn(move || { thread::spawn(move || {
let sys = System::new("ntex-test-server"); let sys = System::new("ntex-test-server");
let system = sys.system();
tx.send(sys.system()).unwrap();
sys.run(|| { sys.run(|| {
factory(super::build()).workers(1).disable_signals().run(); let server = factory(super::build()).workers(1).disable_signals().run();
tx.send((system, server))
.expect("Failed to send Server to TestServer");
Ok(()) Ok(())
}) })
}); });
let system = rx.recv().unwrap(); let (system, server) = rx.recv().unwrap();
TestServer { TestServer {
system, system,
server,
addr: "127.0.0.1:0".parse().unwrap(), addr: "127.0.0.1:0".parse().unwrap(),
} }
} }
@ -99,6 +106,7 @@ where
pub struct TestServer { pub struct TestServer {
addr: net::SocketAddr, addr: net::SocketAddr,
system: System, system: System,
server: Server,
} }
impl TestServer { impl TestServer {
@ -117,7 +125,7 @@ impl TestServer {
tcp_connect(self.addr).await tcp_connect(self.addr).await
} }
/// Stop http server /// Stop http server by stopping the runtime.
pub fn stop(&self) { pub fn stop(&self) {
self.system.stop(); self.system.stop();
} }
@ -131,6 +139,11 @@ impl TestServer {
let tcp = net::TcpListener::from(socket); let tcp = net::TcpListener::from(socket);
tcp.local_addr().unwrap() tcp.local_addr().unwrap()
} }
/// Get access to the running Server
pub fn server(&self) -> Server {
self.server.clone()
}
} }
impl Drop for TestServer { impl Drop for TestServer {