1#![deny(rust_2018_idioms, warnings)]
3#![allow(clippy::type_complexity, clippy::needless_doctest_main)]
4
5use std::sync::mpsc;
6use std::{net, thread};
7
8use actix_rt::{net::TcpStream, System};
9use actix_server::{Server, ServerBuilder, ServiceFactory};
10use socket2::{Domain, Protocol, Socket, Type};
11
12#[cfg(not(test))] pub use actix_macros::test;
14
15pub struct TestServer;
39
40pub struct TestServerRuntime {
42 addr: net::SocketAddr,
43 host: String,
44 port: u16,
45 system: System,
46}
47
48impl TestServer {
49 pub fn start<F>(mut factory: F) -> TestServerRuntime
51 where
52 F: FnMut(ServerBuilder) -> ServerBuilder + Send + 'static,
53 {
54 let (tx, rx) = mpsc::channel();
55
56 thread::spawn(move || {
58 let sys = System::new("actix-test-server");
59 factory(Server::build())
60 .workers(1)
61 .disable_signals()
62 .start();
63
64 tx.send(System::current()).unwrap();
65 sys.run()
66 });
67 let system = rx.recv().unwrap();
68
69 TestServerRuntime {
70 system,
71 addr: "127.0.0.1:0".parse().unwrap(),
72 host: "127.0.0.1".to_string(),
73 port: 0,
74 }
75 }
76
77 pub fn with<F: ServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
79 let (tx, rx) = mpsc::channel();
80
81 thread::spawn(move || {
83 let sys = System::new("actix-test-server");
84 let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
85 let local_addr = tcp.local_addr().unwrap();
86
87 Server::build()
88 .listen("test", tcp, factory)?
89 .workers(1)
90 .disable_signals()
91 .start();
92
93 tx.send((System::current(), local_addr)).unwrap();
94 sys.run()
95 });
96
97 let (system, addr) = rx.recv().unwrap();
98
99 let host = format!("{}", addr.ip());
100 let port = addr.port();
101
102 TestServerRuntime {
103 system,
104 addr,
105 host,
106 port,
107 }
108 }
109
110 pub fn unused_addr() -> net::SocketAddr {
112 let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
113 let socket =
114 Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp())).unwrap();
115 socket.bind(&addr.into()).unwrap();
116 socket.set_reuse_address(true).unwrap();
117 let tcp = socket.into_tcp_listener();
118 tcp.local_addr().unwrap()
119 }
120}
121
122impl TestServerRuntime {
123 pub fn host(&self) -> &str {
125 &self.host
126 }
127
128 pub fn port(&self) -> u16 {
130 self.port
131 }
132
133 pub fn addr(&self) -> net::SocketAddr {
135 self.addr
136 }
137
138 fn stop(&mut self) {
140 self.system.stop();
141 }
142
143 pub fn connect(&self) -> std::io::Result<TcpStream> {
145 TcpStream::from_std(net::TcpStream::connect(self.addr)?)
146 }
147}
148
149impl Drop for TestServerRuntime {
150 fn drop(&mut self) {
151 self.stop()
152 }
153}