Are there any 'good' ways to cause a thread waiting on a recvfrom() call to become unblocked and return with an error?
The motivation for this is to write unit tests for a system which includes a unit that reads UDP datagrams. One of the branches handles errors on the recvfrom call itself. The code isn't required to distinguish between different types of errors, it just has to set a flag.
I've thought of closing the socket from another thread, or do a shutdown on it, to cause recvfrom to return with an error, but this seems a bit heavy handed. I've seen mention elsewhere that sending an over-sized packet would do it, and so set up an experiment where a 16K buffer was sent to a recvfrom waiting for just 4K, but that didn't result in an error. The recvfrom just return 4096, to indicate it had gotten that many bytes.
If the class you're testing is well isolated from the rest of your application, perhaps you could wholesale #include
the .c source file from your unit test. Then you can use a macro to #define recvfrom(...)
to your own private function solely within the test.
Note that this will expose your private implementation to the unit test; you should still test against the public interface and pretend you can't see the guts. It's pretty hacky, but it at least contains all the ugliness to your unit test, without disrupting the deliverable code.