capnp-rpc-rust
capnp-rpc-rust copied to clipboard
Unit testing seems to be difficult
I've just gotten started using this crate and I'm struggling with something rather simple. Here's a snippet of my code, which is supposed to build a greeting message:
impl Server for Proxy {
fn hello(&mut self,
params: HelloParams<>,
mut results: HelloResults<>)
-> ::capnp::capability::Promise<(), ::capnp::Error> {
results
.get()
.set_greetings(&format!("Hello {}", pry!(params.get()).get_name().unwrap()));
::capnp::capability::Promise::ok(())
}
}
I would like to write a unit test for this, but no matter which approach I try it seems too difficult to immitate capnp's internals. I'd be happy about any pointers on how to approach this, or if someone could point out any mistakes I've already made ;-)
It's possible to turn your Proxy
object into a Client
and then call it directly, without needing to construct any RpcSystem
. There are some examples of this in the test suite: https://github.com/capnproto/capnp-rpc-rust/blob/69859603b6c14fcec1c46458649602fc12bc6982/test/test.rs#L823-L834
Does that work for you?
Thanks @dwrensha. That snippet allowed me to construct this simple testcase, although I haven't yet grasped what is happening internally.
#[test]
fn proxy_server_hello() {
let client =
::proxy_capnp::proxy::ToClient::new(Proxy {}).from_server::<::capnp_rpc::Server>();
let mut request = client.hello_request();
request.get().set_name("Proxy");
let response = request.send().promise.wait().unwrap();
assert_eq!(
response.get().unwrap().get_greetings().unwrap(),
"Hello Proxy"
);
}
Is there a document where the mechanics of client/server is described? If not, since it's not self-explanatory I'm happy to help build some beginner friendly docs.
I think the important thing to remember is: a Server
is something you implement, and a Client
is something you call.
The main documentation we have for this stuff right now is in the README.md at the root of the capnp-rpc-rust repo. That could certainly be expanded upon.