[issue8576] test_support.find_unused_port can cause socket conflicts on Windows

Jean-Paul Calderone report at bugs.python.org
Tue May 4 18:18:19 CEST 2010


Jean-Paul Calderone <exarkun at twistedmatrix.com> added the comment:

> One of the tests in test_socket is checking that an attempt to connect to a port with no server running gives socket.error. For that, we need a port that's guaranteed to have no server present.

A good way to do this is to create a socket, bind it to an address (any address - ie ('', 0)), ask it what its address is, and use that as the destination of the connect attempt.  Since you didn't listen() on it, it's not a server, and it won't accept any connections.

Doing it this way is again more reliable, since if you try to discover an unused address with find_unused_port, a real server might end up re-using that address by the time your test gets around to trying to connect to it.

> I think that one of the tests in test_httplib checks the source_address attribute to make sure the port it contains is the one you connected on - again, you need a specific port and it can't be in use.

If you're talking about SourceAddressTest.testHTTPConnectionSourceAddress, I see what you mean.  This is basically an integration test between httplib and the socket module.  No actual socket is required here.  I would implement this test using a fake socket object.  This would allow the test assertion to be strengthened (it currently doesn't assert anything about the ip part of the address, it only checks the port), and still avoids running into problems binding a *real* local address.

> Arguably these tests are of limited value and could simply be deleted

I definitely would not argue this.  These tests seem to cover real functionality which isn't directly covered elsewhere.  They shouldn't be deleted, but fixed to avoid the potential races they're subject to now.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8576>
_______________________________________


More information about the Python-bugs-list mailing list