Unit testing UDP socket handling code
        Posted  
        
            by JustJeff
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by JustJeff
        
        
        
        Published on 2010-05-22T02:00:38Z
        Indexed on 
            2010/05/22
            2:10 UTC
        
        
        Read the original article
        Hit count: 355
        
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.
© Stack Overflow or respective owner