[zeromq-dev] Check for empty socket (unit testing)?

Chuck Remes lists at chuckremes.com
Tue Apr 10 18:01:24 CEST 2012

On Apr 10, 2012, at 9:42 AM, <Jonathan.Meran at schneider-electric.com> <Jonathan.Meran at schneider-electric.com> wrote:

> Hello,
> I am experimenting with ZeroMQ to switch what used to be a synchronous flow of message passing to asynchronous. The unit test for this scenario would send a message, and then assert that the receiver got the message. The unit test code was something like this:
> SendMessage(“Message”);
> CPPUNIT_ASSERT(Receiver.GetMsgCount() == 1);
> Now that the flow is asynchronous using ZeroMQ, I need something that my test code can call to wait for the messages to reach the Receiver. Traditionally, I would use some type of wait loop that would run until the queue has been emptied by the receiving thread. Since I don’t have access to the ZeroMQ internal “queue” I can’t think of an obvious way to synchronize the unit test to wait until the Receiver receives all its messages.
> I’m sure this is a fairly common test scenario and there is an obvious answer that just hasn’t struck me yet. Any help would be much appreciated J

You'll want to make a helper method to perform this (common) action.

Essentially, you need to poll the receiving socket and run your assertion after the poll has returned. Take a look at the man page for zmq_poll(). Note that you can pass a timeout value to zmq_poll() so that if no event is received for the registered socket, you won't hang forever. 

I hope this helps.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120410/1215d1ee/attachment.htm>

More information about the zeromq-dev mailing list