Add ServerHandle to TestServerRuntime.

This commit is contained in:
fakeshadow 2021-04-06 05:56:39 +08:00
parent c5d6df83c7
commit 5f98d65717
2 changed files with 17 additions and 17 deletions

View File

@ -36,6 +36,7 @@ pub struct TestServerRuntime {
host: String, host: String,
port: u16, port: u16,
system: System, system: System,
handle: ServerHandle,
} }
impl TestServer { impl TestServer {
@ -53,18 +54,19 @@ impl TestServer {
.workers(1) .workers(1)
.disable_signals() .disable_signals()
.run(); .run();
tx.send(System::current()).unwrap(); tx.send((server.handle(), System::current())).unwrap();
server.await server.await
}) })
}); });
let system = rx.recv().unwrap(); let (handle, system) = rx.recv().unwrap();
TestServerRuntime { TestServerRuntime {
system,
addr: "127.0.0.1:0".parse().unwrap(), addr: "127.0.0.1:0".parse().unwrap(),
host: "127.0.0.1".to_string(), host: "127.0.0.1".to_string(),
port: 0, port: 0,
system,
handle,
} }
} }
@ -86,13 +88,14 @@ impl TestServer {
.disable_signals() .disable_signals()
.run(); .run();
tx.send((System::current(), local_addr)).unwrap(); tx.send((server.handle(), System::current(), local_addr))
.unwrap();
server.await server.await
}) })
}); });
let (system, addr) = rx.recv().unwrap(); let (handle, system, addr) = rx.recv().unwrap();
let host = format!("{}", addr.ip()); let host = format!("{}", addr.ip());
let port = addr.port(); let port = addr.port();
@ -102,6 +105,7 @@ impl TestServer {
host, host,
port, port,
system, system,
handle,
} }
} }
@ -134,6 +138,7 @@ impl TestServerRuntime {
/// Stop http server /// Stop http server
fn stop(&mut self) { fn stop(&mut self) {
let _ = self.handle.stop(false);
self.system.stop(); self.system.stop();
} }

View File

@ -22,28 +22,23 @@ fn test_bind() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let h = thread::spawn(move || { let h = thread::spawn(move || {
let system = actix_rt::System::new(); actix_rt::System::new().block_on(async {
system.block_on(async {
let server = ServerHandle::build() let server = ServerHandle::build()
.workers(1) .workers(1)
.disable_signals() .disable_signals()
.bind("test", addr, move || fn_service(|_| ok::<_, ()>(()))) .bind("test", addr, move || fn_service(|_| ok::<_, ()>(())))
.unwrap() .unwrap()
.run(); .run();
let _ = tx.send(actix_rt::System::current()); tx.send(server.handle()).unwrap();
let _ = actix_rt::spawn(async move { server.await
let _ = server.await; })
}); });
}); let handle = rx.recv().unwrap();
system.run()
});
let sys = rx.recv().unwrap();
thread::sleep(time::Duration::from_millis(500)); thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok()); assert!(net::TcpStream::connect(addr).is_ok());
sys.stop(); let _ = handle.stop(true);
let _ = h.join(); let _ = h.join().unwrap();
} }
#[test] #[test]