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

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

Loading…
Cancel
Save