Fixed #28212 -- Allowed customizing the port that LiveServerTestCase uses.

Forwardport of 877d7b71ae from stable/1.11.x
This commit is contained in:
Robert Rollins 2017-05-22 10:16:56 -07:00 committed by Tim Graham
parent a30482a4b6
commit b6d4b6e544
5 changed files with 42 additions and 7 deletions

View file

@ -147,6 +147,24 @@ class LiveServerPort(LiveServerBase):
if hasattr(TestCase, 'server_thread'):
TestCase.server_thread.terminate()
def test_specified_port_bind(self):
"""LiveServerTestCase.port customizes the server's port."""
TestCase = type(str('TestCase'), (LiveServerBase,), {})
# Find an open port and tell TestCase to use it.
s = socket.socket()
s.bind(('', 0))
TestCase.port = s.getsockname()[1]
s.close()
TestCase.setUpClass()
try:
self.assertEqual(
TestCase.port, TestCase.server_thread.port,
'Did not use specified port for LiveServerTestCase thread: %s' % TestCase.port
)
finally:
if hasattr(TestCase, 'server_thread'):
TestCase.server_thread.terminate()
class LiverServerThreadedTests(LiveServerBase):
"""If LiverServerTestCase isn't threaded, these tests will hang."""