Avoid race-prone allocate_tcp_port for some Tubs when possible
create_tub on POSIX can pre-allocate a port safely instead.
This commit is contained in:
parent
91c655298a
commit
277cd16594
|
@ -314,18 +314,36 @@ class Server(unittest.TestCase):
|
||||||
|
|
||||||
NICKNAME = u"n\u00EDickname-%s" # LATIN SMALL LETTER I WITH ACUTE
|
NICKNAME = u"n\u00EDickname-%s" # LATIN SMALL LETTER I WITH ACUTE
|
||||||
|
|
||||||
|
def foolscapEndpointForPortNumber(portnum):
|
||||||
|
if portnum is None:
|
||||||
|
from twisted.internet import reactor
|
||||||
|
from twisted.internet.interfaces import IReactorSocket
|
||||||
|
if IReactorSocket.providedBy(reactor):
|
||||||
|
from socket import socket, AF_INET
|
||||||
|
from twisted.internet.endpoints import AdoptedStreamServerEndpoint
|
||||||
|
s = socket()
|
||||||
|
s.setblocking(False)
|
||||||
|
s.bind(('', 0))
|
||||||
|
portnum = s.getsockname()[1]
|
||||||
|
s.listen(3)
|
||||||
|
return (portnum, AdoptedStreamServerEndpoint(reactor, s.fileno(), AF_INET))
|
||||||
|
else:
|
||||||
|
# Get a random port number and fall through.
|
||||||
|
portnum = iputil.allocate_tcp_port()
|
||||||
|
return (portnum, "tcp:%d" % (portnum,))
|
||||||
|
|
||||||
|
|
||||||
class SystemTestMixin(ServiceMixin, pollmixin.PollMixin):
|
class SystemTestMixin(ServiceMixin, pollmixin.PollMixin):
|
||||||
|
|
||||||
def create_tub(self, portnum=None):
|
def create_tub(self, portnum=None):
|
||||||
|
portnum, endpoint = foolscapEndpointForPortNumber(portnum)
|
||||||
tubfile = os.path.join(self.basedir, "tub.pem")
|
tubfile = os.path.join(self.basedir, "tub.pem")
|
||||||
self.central_tub = tub = Tub(certFile=tubfile)
|
self.central_tub = tub = Tub(certFile=tubfile)
|
||||||
#tub.setOption("logLocalFailures", True)
|
#tub.setOption("logLocalFailures", True)
|
||||||
#tub.setOption("logRemoteFailures", True)
|
#tub.setOption("logRemoteFailures", True)
|
||||||
tub.setOption("expose-remote-exception-types", False)
|
tub.setOption("expose-remote-exception-types", False)
|
||||||
tub.setServiceParent(self.parent)
|
tub.setServiceParent(self.parent)
|
||||||
if portnum is None:
|
tub.listenOn(endpoint)
|
||||||
portnum = iputil.allocate_tcp_port()
|
|
||||||
tub.listenOn("tcp:%d" % portnum)
|
|
||||||
self.central_portnum = portnum
|
self.central_portnum = portnum
|
||||||
tub.setLocation("localhost:%d" % self.central_portnum)
|
tub.setLocation("localhost:%d" % self.central_portnum)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue