extern crate xmlrpc;
use xmlrpc::{Fault, Request, Value};
use std::net::TcpStream;
use std::process::{Child, Command};
use std::thread::sleep;
use std::time::{Duration, Instant};
const PORT: u16 = 8000;
const URL: &'static str = "http://127.0.0.1:8000";
struct Reap(Child);
impl Drop for Reap {
fn drop(&mut self) {
self.0.kill().expect("process already died");
}
}
fn setup() -> Result<Reap, ()> {
let start = Instant::now();
let mut child = match Command::new("python3")
.arg("-m")
.arg("xmlrpc.server")
.spawn()
{
Ok(child) => child,
Err(e) => {
eprintln!(
"could not start python XML-RPC server, ignoring python test ({})",
e
);
return Err(());
}
};
let mut iteration = 0;
loop {
match child.try_wait().unwrap() {
None => {} Some(status) => panic!("python process unexpectedly died: {}", status),
}
match TcpStream::connect(("127.0.0.1", PORT)) {
Ok(_) => {
println!(
"connected to server after {:?} (iteration {})",
Instant::now() - start,
iteration
);
return Ok(Reap(child));
}
Err(_) => {} }
sleep(Duration::from_millis(50));
iteration += 1;
}
}
fn run_tests() {
let pow = Request::new("pow").arg(2).arg(8).call_url(URL).unwrap();
assert_eq!(pow.as_i64(), Some(2i64.pow(8)));
let err = Request::new("pow")
.arg(2)
.arg(2)
.arg("BLA")
.call_url(URL)
.unwrap_err();
err.fault().expect("returned error was not a fault");
let result = Request::new_multicall(&[
Request::new("pow").arg(2).arg(4),
Request::new("add").arg(2).arg(4),
Request::new("doesn't exist"),
])
.call_url(URL)
.unwrap();
let results = result.as_array().unwrap();
assert_eq!(results[0], Value::Array(vec![Value::Int(16)]));
assert_eq!(results[1], Value::Array(vec![Value::Int(6)]));
Fault::from_value(&results[2]).expect("expected fault as third result");
}
fn main() {
let mut reaper = match setup() {
Ok(reap) => reap,
Err(()) => return,
};
match reaper.0.try_wait().unwrap() {
None => {} Some(status) => {
panic!("python process unexpectedly exited: {}", status);
}
}
run_tests();
match reaper.0.try_wait().unwrap() {
None => {} Some(status) => {
panic!("python process unexpectedly exited: {}", status);
}
}
}