Simplify fifo test by giving the question as a string instead of an array

main
Laurent Pelecq 2 years ago
parent aff43c6147
commit 28c3533628

@ -22,7 +22,7 @@ use server::Server;
/// The communication is an array of (["question", ...], "response")
#[cfg(not(feature = "metal-io"))]
fn test_client<F>(
communication: &'static [(&'static [&'static str], &'static str)],
communication: &'static [(&'static str, &'static str)],
process: F,
) -> io::Result<()>
where
@ -49,8 +49,8 @@ where
}
#[cfg(not(feature = "metal-io"))]
const SET_CLIENT_COMMUNICATION: (&[&str], &str) = (
&["SET self CLIENT_NAME test:test:main\r\n"],
const SET_CLIENT_COMMUNICATION: (&str, &str) = (
"SET self CLIENT_NAME test:test:main\r\n",
"208 OK CLIENT NAME SET\r\n",
);
@ -60,7 +60,7 @@ fn connect_and_quit() -> io::Result<()> {
test_client(
&[
SET_CLIENT_COMMUNICATION,
(&["QUIT\r\n"], "231 HAPPY HACKING\r\n"),
("QUIT\r\n", "231 HAPPY HACKING\r\n"),
],
|client| {
client.quit().unwrap().check_status(OK_BYE).unwrap();
@ -75,9 +75,9 @@ fn say_one_line() -> io::Result<()> {
test_client(
&[
SET_CLIENT_COMMUNICATION,
(&["SPEAK\r\n"], "230 OK RECEIVING DATA\r\n"),
("SPEAK\r\n", "230 OK RECEIVING DATA\r\n"),
(
&["Hello, world\r\n", ".\r\n"],
"Hello, world\r\n.\r\n",
"225-21\r\n225 OK MESSAGE QUEUED\r\n",
),
],
@ -105,7 +105,7 @@ macro_rules! test_setter {
#[cfg(not(feature = "metal-io"))]
fn $setter() -> io::Result<()> {
test_client(
&[SET_CLIENT_COMMUNICATION, (&[$question], $answer)],
&[SET_CLIENT_COMMUNICATION, ($question, $answer)],
|client| {
client.$setter($($arg)*).unwrap().check_status($code).unwrap();
Ok(())
@ -121,7 +121,7 @@ macro_rules! test_getter {
#[cfg(not(feature = "metal-io"))]
fn $getter() -> io::Result<()> {
test_client(
&[SET_CLIENT_COMMUNICATION, (&[$question], $answer)],
&[SET_CLIENT_COMMUNICATION, ($question, $answer)],
|client| {
let value = client.$getter().unwrap().$receive(251).unwrap();
assert_eq!($value, value);
@ -141,7 +141,7 @@ macro_rules! test_list {
#[cfg(not(feature = "metal-io"))]
fn $getter() -> io::Result<()> {
test_client(
&[SET_CLIENT_COMMUNICATION, (&[$question], $answer)],
&[SET_CLIENT_COMMUNICATION, ($question, $answer)],
|client| {
let values = client.$getter().unwrap().receive_lines($code).unwrap();
assert_eq!($values, values.as_slice());
@ -167,7 +167,7 @@ fn set_debug() -> io::Result<()> {
&[
SET_CLIENT_COMMUNICATION,
(
&["SET all DEBUG on\r\n"],
"SET all DEBUG on\r\n",
"262-/run/user/100/speech-dispatcher/log/debug\r\n262 OK DEBUGGING SET\r\n",
),
],
@ -340,7 +340,7 @@ fn list_synthesis_voices() -> io::Result<()> {
&[
SET_CLIENT_COMMUNICATION,
(
&["LIST SYNTHESIS_VOICES\r\n"],
"LIST SYNTHESIS_VOICES\r\n",
"249-Amharic\tam\tnone\r\n249-Greek+Auntie\tel\tAuntie\r\n249-Vietnamese (Southern)+shelby\tvi-VN-X-SOUTH\tshelby\r\n249 OK VOICE LIST SENT\r\n"
),
],
@ -365,9 +365,9 @@ fn receive_notification() -> io::Result<()> {
test_client(
&[
SET_CLIENT_COMMUNICATION,
(&["SPEAK\r\n"], "230 OK RECEIVING DATA\r\n"),
("SPEAK\r\n", "230 OK RECEIVING DATA\r\n"),
(
&["Hello, world\r\n", ".\r\n"],
"Hello, world\r\n.\r\n",
"225-21\r\n225 OK MESSAGE QUEUED\r\n701-21\r\n701-test\r\n701 BEGIN\r\n",
),
],

@ -15,7 +15,7 @@ use std::os::unix::net::UnixListener;
/// Server on a named socket.
pub struct Server {
listener: UnixListener,
communication: Vec<(&'static [&'static str], &'static str)>,
communication: Vec<(&'static str, &'static str)>,
}
impl Server {
@ -25,7 +25,7 @@ impl Server {
/// the server will receive and the second item is the answer.
pub fn new<P>(
socket_path: &P,
communication: &[(&'static [&'static str], &'static str)],
communication: &[(&'static str, &'static str)],
) -> io::Result<Self>
where
P: AsRef<Path>,
@ -37,12 +37,20 @@ impl Server {
})
}
fn split_lines(lines: &str) -> Vec<String> {
lines
.trim_end()
.split("\r\n")
.map(|s| format!("{}\r\n", s))
.collect::<Vec<String>>()
}
pub 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);
for (questions, answer) in self.communication.iter() {
for question in questions.iter() {
for question in Server::split_lines(questions).iter() {
let mut line = String::new();
input.read_line(&mut line)?;
if line != *question {
@ -65,7 +73,7 @@ impl Server {
pub fn run<P>(
socket_path: P,
communication: &'static [(&'static [&'static str], &'static str)],
communication: &'static [(&'static str, &'static str)],
) -> thread::JoinHandle<io::Result<()>>
where
P: AsRef<Path>,
@ -78,3 +86,16 @@ impl Server {
})
}
}
#[cfg(test)]
mod test {
use super::Server;
#[test]
fn test_split_lines() {
const ONE_LINE: &str = "one line\r\n";
let one_line = Server::split_lines(ONE_LINE);
assert_eq!(&[ONE_LINE], one_line.as_slice());
}
}

Loading…
Cancel
Save