invoke with hand-written code and tests

This commit is contained in:
NikVolf 2016-03-29 01:40:30 +03:00
parent cd6a09c0df
commit 15ecbaf59c
2 changed files with 44 additions and 0 deletions

View File

@ -33,4 +33,16 @@ mod tests {
assert_eq!(10, *service.commits.read().unwrap()); assert_eq!(10, *service.commits.read().unwrap());
} }
#[test]
fn call_service_proxy() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![0, 0, 0, 10];
let service_proxy = ServiceProxy::new(socket);
let result = service_proxy.commit(5);
assert_eq!(vec![0, 0, 0, 0, 0, 5], service_proxy.socket().borrow().write_buffer.clone());
assert_eq!(10, result);
}
} }

View File

@ -15,6 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::RwLock; use std::sync::RwLock;
use std::ops::*;
pub struct Service { pub struct Service {
pub commits: RwLock<usize>, pub commits: RwLock<usize>,
@ -35,6 +36,37 @@ impl Service {
} }
} }
impl<S: ::ipc::IpcSocket> ServiceProxy<S> {
pub fn commit(&self, f: u32) -> u32 {
#[derive(Serialize)]
struct Request<'a> {
f: &'a u32,
}
let payload = Request{ f: &f, };
let mut socket_ref = self.socket.borrow_mut();
let mut socket = socket_ref.deref_mut();
let serialized_payload = ::bincode::serde::serialize(&payload, ::bincode::SizeLimit::Infinite).unwrap();
::ipc::invoke(0, &Some(serialized_payload), &mut socket);
while !socket.ready().load(::std::sync::atomic::Ordering::Relaxed) { }
::bincode::serde::deserialize_from::<_, u32>(&mut socket, ::bincode::SizeLimit::Infinite).unwrap()
}
pub fn new(socket: S) -> ServiceProxy<S> {
ServiceProxy {
socket: ::std::cell::RefCell::new(socket),
phantom: ::std::marker::PhantomData,
}
}
#[cfg(test)]
pub fn socket(&self) -> &::std::cell::RefCell<S> {
&self.socket
}
}
impl Service { impl Service {
pub fn new() -> Service { pub fn new() -> Service {
Service { Service {