Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing seems to be difficult #27

Open
steveej opened this issue Jan 19, 2018 · 3 comments
Open

Unit testing seems to be difficult #27

steveej opened this issue Jan 19, 2018 · 3 comments

Comments

@steveej
Copy link

steveej commented Jan 19, 2018

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 ;-)

@dwrensha
Copy link
Member

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:

capnp-rpc-rust/test/test.rs

Lines 823 to 834 in 6985960

fn local_client_return_cap() {
let server = ::impls::Bootstrap;
let client = ::test_capnp::bootstrap::ToClient::new(server).from_server::<::capnp_rpc::Server>();
let response = client.test_interface_request().send().promise.wait().unwrap();
let client1 = response.get().unwrap().get_cap().unwrap();
let mut request = client1.foo_request();
request.get().set_i(123);
request.get().set_j(true);
let response1 = request.send().promise.wait().unwrap();
assert_eq!(response1.get().unwrap().get_x().unwrap(), "foo");
}

Does that work for you?

@steveej
Copy link
Author

steveej commented Jan 21, 2018

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.

@dwrensha
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants