Running ZeroMQ under unit test - address already in use
I'm trying to run two ZeroMQ REQ/REP endpoints under test using TCP protocol. The first test executes fine. The endpoints are then disposed (via a call to Dispose() on socket and context), however the second test (using the same TCP endpoint address) fails with an exception saying the 'Address is already in use'. Why is the socket not being disposed properly?
This sounds like a concurrency bug to me - as if the network stack or ZeroMQ doesn't clean up it's resources quick enough for next unit-test to allocate them again.
What happens if you put a short sleep between the tests? (I don't like such solutions, but for debugging...)
A simple potential solution would be to use different ports for different tests.
I've been getting this in unit tests also. To resolve I put a 10 msec sleep after the socket is closed (I'm reusing the context and doing inproc sockets).
Without the sleep I get the already in use problem. ZMQ is running the sockets on a separate thread, so assume that even with inproc, you need a context switch before reusing the address to allow the socket thread to do its close down.
I'm surprised that if you are correctly shutting down the context that the error persists, as you would expect that to wait for the socket thread to complete, ensuring the the addresses are released. You may need to check the libzmq sources to understand what is going on in detail.