Use better method to listen on random port.

This commit is contained in:
Itamar Turner-Trauring 2022-02-03 12:43:49 -05:00
parent 0fbf746e27
commit 1dfc0bde36
1 changed files with 25 additions and 14 deletions

View File

@ -25,9 +25,10 @@ else:
from random import Random from random import Random
from unittest import SkipTest from unittest import SkipTest
from twisted.internet.defer import inlineCallbacks, returnValue from twisted.internet.defer import inlineCallbacks, returnValue, succeed
from twisted.internet.task import Clock from twisted.internet.task import Clock
from twisted.internet import reactor from twisted.internet import reactor
from twisted.internet.endpoints import serverFromString
from twisted.web.server import Site from twisted.web.server import Site
from twisted.web.client import HTTPConnectionPool from twisted.web.client import HTTPConnectionPool
from hyperlink import DecodedURL from hyperlink import DecodedURL
@ -37,11 +38,10 @@ from foolscap.api import Referenceable, RemoteException
from allmydata.interfaces import IStorageServer # really, IStorageClient from allmydata.interfaces import IStorageServer # really, IStorageClient
from .common_system import SystemTestMixin from .common_system import SystemTestMixin
from .common import AsyncTestCase from .common import AsyncTestCase, SameProcessStreamEndpointAssigner
from allmydata.storage.server import StorageServer # not a IStorageServer!! from allmydata.storage.server import StorageServer # not a IStorageServer!!
from allmydata.storage.http_server import HTTPServer from allmydata.storage.http_server import HTTPServer
from allmydata.storage.http_client import StorageClient from allmydata.storage.http_client import StorageClient
from allmydata.util.iputil import allocate_tcp_port
from allmydata.storage_client import _HTTPStorageServer from allmydata.storage_client import _HTTPStorageServer
@ -1029,6 +1029,11 @@ class _SharedMixin(SystemTestMixin):
) )
AsyncTestCase.setUp(self) AsyncTestCase.setUp(self)
self._port_assigner = SameProcessStreamEndpointAssigner()
self._port_assigner.setUp()
self.addCleanup(self._port_assigner.tearDown)
self.basedir = "test_istorageserver/" + self.id() self.basedir = "test_istorageserver/" + self.id()
yield SystemTestMixin.setUp(self) yield SystemTestMixin.setUp(self)
yield self.set_up_nodes(1) yield self.set_up_nodes(1)
@ -1041,7 +1046,7 @@ class _SharedMixin(SystemTestMixin):
self._clock = Clock() self._clock = Clock()
self._clock.advance(123456) self._clock.advance(123456)
self.server._clock = self._clock self.server._clock = self._clock
self.storage_client = self._get_istorage_server() self.storage_client = yield self._get_istorage_server()
def fake_time(self): def fake_time(self):
"""Return the current fake, test-controlled, time.""" """Return the current fake, test-controlled, time."""
@ -1073,7 +1078,7 @@ class _FoolscapMixin(_SharedMixin):
def _get_istorage_server(self): def _get_istorage_server(self):
client = self._get_native_server().get_storage_server() client = self._get_native_server().get_storage_server()
self.assertTrue(IStorageServer.providedBy(client)) self.assertTrue(IStorageServer.providedBy(client))
return client return succeed(client)
@inlineCallbacks @inlineCallbacks
def disconnect(self): def disconnect(self):
@ -1094,22 +1099,28 @@ class _HTTPMixin(_SharedMixin):
self.skipTest("Not going to bother supporting Python 2") self.skipTest("Not going to bother supporting Python 2")
return _SharedMixin.setUp(self) return _SharedMixin.setUp(self)
@inlineCallbacks
def _get_istorage_server(self): def _get_istorage_server(self):
set_treq_pool(HTTPConnectionPool(reactor, persistent=False)) set_treq_pool(HTTPConnectionPool(reactor, persistent=False))
swissnum = b"1234" swissnum = b"1234"
self._http_storage_server = HTTPServer(self.server, swissnum) self._http_storage_server = HTTPServer(self.server, swissnum)
self._port_number = allocate_tcp_port()
self._listening_port = reactor.listenTCP( # Listen on randomly assigned port:
self._port_number, tcp_address, endpoint_string = self._port_assigner.assign(reactor)
Site(self._http_storage_server.get_resource()), _, host, port = tcp_address.split(":")
interface="127.0.0.1", port = int(port)
endpoint = serverFromString(reactor, endpoint_string)
self._listening_port = yield endpoint.listen(
Site(self._http_storage_server.get_resource())
) )
return _HTTPStorageServer.from_http_client( returnValue(
_HTTPStorageServer.from_http_client(
StorageClient( StorageClient(
DecodedURL.from_text("http://127.0.0.1:{}".format(self._port_number)), DecodedURL().replace(scheme="http", host=host, port=port),
swissnum, swissnum,
) )
) )
)
# Eventually should also: # Eventually should also:
# self.assertTrue(IStorageServer.providedBy(client)) # self.assertTrue(IStorageServer.providedBy(client))