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

View File

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