Writing tests for RPC-based code?

256 views
Skip to first unread message

Jens Alfke

unread,
Feb 21, 2022, 12:47:16 PM2/21/22
to Cap'n Proto
Does anyone have advice on writing C++ automated tests for RPC-based code? (FWIW, I use the Catch2 unit-test framework, but I don’t restrict it to true ‘unit’ tests.) Say I’ve got an interface file, a class `Client` built atop classes generated from that interface, and a class `Server` that accepts TCP connections from a `Client`. I want to write a test that creates a Client and Server, connects them to each other, and then invokes some operations on the Client and asserts the correct results.

The first step of course is creating a connection. It looks like the simplest supported mechanism is to open a POSIX pipe and then create kj AsyncStreams on its two file descriptors. That means I’ll need an alternative Client constructor that takes a file descriptor or stream instead of a hostname/port…

I’d love to take a look at anyone’s test code that does stuff like this.

—Jens

Kenton Varda

unread,
Feb 28, 2022, 3:12:46 PM2/28/22
to Jens Alfke, Cap'n Proto
Hi Jens,

Sorry for the slow reply.

Usually when I write tests, I don't actually use the RPC system, but rather have the client call the server directly running in the same thread. Remember that you can implicitly turn an `Own<MyInterface::Server>` into a `MyInterface::Client`, and then call it like a client:

    MyInterface::Client client = kj::heap<MyServerImpl>();

In theory, whether or not you have an actual RPC connection in between should be transparent.

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/CED5FAAE-EC6B-47E7-BDE3-58F3C1DD001F%40mooseyard.com.
Reply all
Reply to author
Forward
0 new messages