Rename test server to UnixServer

main
Laurent Pelecq 2 years ago
parent 4619b2c57f
commit a827aa06a6

@ -17,9 +17,6 @@ use ssip_client::*;
#[cfg(feature = "async-mio")]
mod server;
#[cfg(feature = "async-mio")]
use server::UnixServer;
#[cfg(feature = "async-mio")]
struct State<'a, 'b> {
pub done: bool,
@ -91,7 +88,7 @@ fn basic_async_communication() -> ClientResult<()> {
let socket_dir = tempfile::tempdir()?;
let socket_path = socket_dir.path().join("basic_async_communication.socket");
assert!(!socket_path.exists());
let handle = UnixServer::run(&socket_path, &COMMUNICATION);
let handle = server::run_unix(&socket_path, &COMMUNICATION)?;
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(128);
let mut client = QueuedClient::new(fifo::Builder::new().path(&socket_path).build()?);

@ -14,14 +14,11 @@ use std::{io, os::unix::net::UnixStream};
#[cfg(not(feature = "async-mio"))]
mod server;
#[cfg(not(feature = "async-mio"))]
use server::UnixServer;
/// Create a server and run the client
///
/// The communication is an array of (["question", ...], "response")
#[cfg(not(feature = "async-mio"))]
fn test_client<F>(
fn test_unix_client<F>(
communication: &'static [(&'static str, &'static str)],
process: F,
) -> ClientResult<()>
@ -32,7 +29,7 @@ where
let socket_path = socket_dir.path().join("test_client.socket");
assert!(!socket_path.exists());
let mut process_wrapper = std::panic::AssertUnwindSafe(process);
let handle = UnixServer::run(&socket_path, communication);
let handle = server::run_unix(&socket_path, communication)?;
let mut client = ssip_client::fifo::Builder::new()
.path(&socket_path)
.build()?;
@ -54,7 +51,7 @@ const SET_CLIENT_COMMUNICATION: (&str, &str) = (
#[test]
#[cfg(not(feature = "async-mio"))]
fn connect_and_quit() -> ClientResult<()> {
test_client(
test_unix_client(
&[
SET_CLIENT_COMMUNICATION,
("QUIT\r\n", "231 HAPPY HACKING\r\n"),
@ -69,7 +66,7 @@ fn connect_and_quit() -> ClientResult<()> {
#[test]
#[cfg(not(feature = "async-mio"))]
fn say_one_line() -> ClientResult<()> {
test_client(
test_unix_client(
&[
SET_CLIENT_COMMUNICATION,
("SPEAK\r\n", "230 OK RECEIVING DATA\r\n"),
@ -101,7 +98,7 @@ macro_rules! test_setter {
#[test]
#[cfg(not(feature = "async-mio"))]
fn $setter() -> ClientResult<()> {
test_client(
test_unix_client(
&[SET_CLIENT_COMMUNICATION, ($question, $answer)],
|client| {
client.$setter($($arg)*).unwrap().check_status($code).unwrap();
@ -117,7 +114,7 @@ macro_rules! test_getter {
#[test]
#[cfg(not(feature = "async-mio"))]
fn $getter() -> ClientResult<()> {
test_client(
test_unix_client(
&[SET_CLIENT_COMMUNICATION, ($question, $answer)],
|client| {
let value = client.$getter $get_args.unwrap().$receive $recv_arg.unwrap();
@ -140,7 +137,7 @@ macro_rules! test_list {
#[test]
#[cfg(not(feature = "async-mio"))]
fn $getter() -> ClientResult<()> {
test_client(
test_unix_client(
&[SET_CLIENT_COMMUNICATION, ($question, $answer)],
|client| {
let values = client.$getter().unwrap().receive_lines($code).unwrap();
@ -163,7 +160,7 @@ test_setter!(
#[test]
#[cfg(not(feature = "async-mio"))]
fn set_debug() -> ClientResult<()> {
test_client(
test_unix_client(
&[
SET_CLIENT_COMMUNICATION,
(
@ -339,7 +336,7 @@ test_list!(
#[test]
#[cfg(not(feature = "async-mio"))]
fn list_synthesis_voices() -> ClientResult<()> {
test_client(
test_unix_client(
&[
SET_CLIENT_COMMUNICATION,
(
@ -365,7 +362,7 @@ fn list_synthesis_voices() -> ClientResult<()> {
#[test]
#[cfg(not(feature = "async-mio"))]
fn receive_notification() -> ClientResult<()> {
test_client(
test_unix_client(
&[
SET_CLIENT_COMMUNICATION,
("SPEAK\r\n", "230 OK RECEIVING DATA\r\n"),
@ -402,7 +399,7 @@ fn receive_notification() -> ClientResult<()> {
#[test]
#[cfg(not(feature = "async-mio"))]
fn history_clients_list() -> ClientResult<()> {
test_client(
test_unix_client(
&[
SET_CLIENT_COMMUNICATION,
(

@ -12,6 +12,11 @@ use std::thread;
use std::os::unix::net::UnixListener;
/// Server traits
pub trait Server {
fn serve(&mut self) -> io::Result<()>;
}
/// Server on a named socket.
pub struct UnixServer {
listener: UnixListener,
@ -44,8 +49,10 @@ impl UnixServer {
.map(|s| format!("{}\r\n", s))
.collect::<Vec<String>>()
}
}
pub fn serve(&mut self) -> io::Result<()> {
impl Server for UnixServer {
fn serve(&mut self) -> io::Result<()> {
let (stream, _) = self.listener.accept()?;
let mut input = BufReader::new(stream.try_clone()?);
let mut output = BufWriter::new(stream);
@ -65,21 +72,27 @@ impl UnixServer {
}
Ok(())
}
}
pub fn run<P>(
socket_path: P,
communication: &'static [(&'static str, &'static str)],
) -> thread::JoinHandle<io::Result<()>>
where
P: AsRef<Path>,
{
let server_path = socket_path.as_ref().to_path_buf();
let mut server = Self::new(&server_path, communication).unwrap();
thread::spawn(move || -> io::Result<()> {
server.serve()?;
Ok(())
})
}
/// Run the server in a thread
pub fn run_server(mut server: Box<dyn Server + Send>) -> thread::JoinHandle<io::Result<()>> {
thread::spawn(move || -> io::Result<()> {
server.serve()?;
Ok(())
})
}
pub fn run_unix<P>(
socket_path: P,
communication: &'static [(&'static str, &'static str)],
) -> io::Result<thread::JoinHandle<io::Result<()>>>
where
P: AsRef<Path>,
{
Ok(run_server(Box::new(UnixServer::new(
&socket_path,
communication,
)?)))
}
#[cfg(test)]

Loading…
Cancel
Save