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

update server tests

This commit is contained in:
Nikolay Kim 2019-03-11 14:33:19 -07:00
parent 5567fb41d2
commit 9a9b3e9ca9
3 changed files with 42 additions and 14 deletions

View File

@ -35,7 +35,7 @@ script:
cargo test --features="ssl,tls,rust-tls" -- --nocapture cargo test --features="ssl,tls,rust-tls" -- --nocapture
cd actix-codec && cargo test && cd .. cd actix-codec && cargo test && cd ..
cd actix-service && cargo test && cd .. cd actix-service && cargo test && cd ..
cd actix-server && cargo test --features="ssl,tls,rust-tls" -- --nocapture && cd .. cd actix-server && cargo test --all-features -- --nocapture && cd ..
cd actix-rt && cargo test && cd .. cd actix-rt && cargo test && cd ..
cd actix-connector && cargo test && cd .. cd actix-connector && cargo test && cd ..
cd actix-utils && cargo test && cd .. cd actix-utils && cargo test && cd ..

View File

@ -64,5 +64,6 @@ webpki = { version = "0.19", optional = true }
webpki-roots = { version = "0.16", optional = true } webpki-roots = { version = "0.16", optional = true }
[dev-dependencies] [dev-dependencies]
env_logger = "0.6" bytes = "0.4"
actix-codec = "0.1.0" actix-codec = "0.1.0"
env_logger = "0.6"

View File

@ -1,9 +1,14 @@
use std::io::Read;
use std::sync::mpsc; use std::sync::mpsc;
use std::{net, thread, time}; use std::{net, thread, time};
use actix_server::{Server, ServerConfig}; use actix_codec::{BytesCodec, Framed};
use actix_server::{Io, Server, ServerConfig};
use actix_service::{fn_cfg_factory, fn_service, IntoService}; use actix_service::{fn_cfg_factory, fn_service, IntoService};
use bytes::Bytes;
use futures::{Future, Sink};
use net2::TcpBuilder; use net2::TcpBuilder;
use tokio_tcp::TcpStream;
fn unused_addr() -> net::SocketAddr { fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap(); let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
@ -37,16 +42,20 @@ fn test_bind() {
#[test] #[test]
fn test_bind_no_config() { fn test_bind_no_config() {
let addr = unused_addr(); let addr = unused_addr();
let (tx, rx) = mpsc::channel();
thread::spawn(move || { thread::spawn(move || {
Server::build() let sys = actix_rt::System::new("test");
let srv = Server::build()
.bind("test", addr, move || fn_service(|_| Ok::<_, ()>(()))) .bind("test", addr, move || fn_service(|_| Ok::<_, ()>(())))
.unwrap() .unwrap()
.run() .start();
let _ = tx.send((srv, actix_rt::System::current()));
let _ = sys.run();
}); });
let (_, sys) = rx.recv().unwrap();
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok()); assert!(net::TcpStream::connect(addr).is_ok());
let _ = sys.stop();
} }
#[test] #[test]
@ -80,11 +89,18 @@ fn test_start() {
let sys = actix_rt::System::new("test"); let sys = actix_rt::System::new("test");
let srv = Server::build() let srv = Server::build()
.backlog(1) .backlog(100)
.bind("test", addr, move || { .bind("test", addr, move || {
fn_cfg_factory(move |cfg: &ServerConfig| { fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr); assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service()) Ok::<_, ()>(
(|io: Io<TcpStream>| {
Framed::new(io.into_parts().0, BytesCodec)
.send(Bytes::from_static(b"test"))
.then(|_| Ok::<_, ()>(()))
})
.into_service(),
)
}) })
}) })
.unwrap() .unwrap()
@ -94,15 +110,20 @@ fn test_start() {
let _ = sys.run(); let _ = sys.run();
}); });
let (srv, sys) = rx.recv().unwrap(); let (srv, sys) = rx.recv().unwrap();
thread::sleep(time::Duration::from_millis(200));
assert!(net::TcpStream::connect(addr).is_ok()); let mut buf = [0u8; 4];
let mut conn = net::TcpStream::connect(addr).unwrap();
let _ = conn.read_exact(&mut buf);
assert_eq!(buf, b"test"[..]);
// pause // pause
let _ = srv.pause(); let _ = srv.pause();
thread::sleep(time::Duration::from_millis(1000)); thread::sleep(time::Duration::from_millis(200));
assert!(net::TcpStream::connect_timeout(&addr, time::Duration::from_millis(100)).is_ok()); let mut conn = net::TcpStream::connect(addr).unwrap();
assert!(net::TcpStream::connect_timeout(&addr, time::Duration::from_millis(100)).is_err()); conn.set_read_timeout(Some(time::Duration::from_millis(100)))
.unwrap();
let res = conn.read_exact(&mut buf);
assert!(res.is_err());
// resume // resume
let _ = srv.resume(); let _ = srv.resume();
@ -111,10 +132,16 @@ fn test_start() {
assert!(net::TcpStream::connect(addr).is_ok()); assert!(net::TcpStream::connect(addr).is_ok());
assert!(net::TcpStream::connect(addr).is_ok()); assert!(net::TcpStream::connect(addr).is_ok());
let mut buf = [0u8; 4];
let mut conn = net::TcpStream::connect(addr).unwrap();
let _ = conn.read_exact(&mut buf);
assert_eq!(buf, b"test"[..]);
// stop // stop
let _ = srv.stop(false); let _ = srv.stop(false);
thread::sleep(time::Duration::from_millis(100)); thread::sleep(time::Duration::from_millis(100));
assert!(net::TcpStream::connect(addr).is_err()); assert!(net::TcpStream::connect(addr).is_err());
thread::sleep(time::Duration::from_millis(100));
let _ = sys.stop(); let _ = sys.stop();
} }