Skip to content

Commit 92c564b

Browse files
bjesterclaude
andcommitted
Use explicit ports for test live servers
A sandbox can deny binding to port 0, which LiveServerTestCase uses by default and get_free_tcp_port used to find a free port. get_free_tcp_port moves to the test helpers and falls back to a fixed list of ports when port 0 is denied. A new FreePortLiveServerTestCase picks its port with it in setUpClass, so each class gets a port that is free when its server starts. The client test cases now extend it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 72ce97d commit 92c564b

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎tests/testapp/tests/helpers.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import json
6+
import socket
67
import uuid
78

89
import factory
@@ -35,6 +36,35 @@
3536
from .compat import EnvironmentVarGuard
3637

3738

39+
def get_free_tcp_port():
40+
# sandboxing can cause 0 to fail (which is to try for any open port), so fallback to a hardcoded
41+
# list afterward
42+
ports = [0, 3000, 8000, 8001, 8080, 8081, 8888, 10001, 10002, 10003]
43+
for p in ports:
44+
try:
45+
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46+
tcp.bind(("127.0.0.1", p))
47+
_, port = tcp.getsockname()
48+
tcp.close()
49+
return port
50+
except OSError:
51+
pass
52+
53+
raise RuntimeError("Could not find open port")
54+
55+
56+
class FreePortLiveServerTestCase(LiveServerTestCase):
57+
"""
58+
LiveServerTestCase defaults to port 0, which sandboxing can deny, so pick an explicit port
59+
for each test class instead
60+
"""
61+
62+
@classmethod
63+
def setUpClass(cls):
64+
cls.port = get_free_tcp_port()
65+
super(FreePortLiveServerTestCase, cls).setUpClass()
66+
67+
3868
class FacilityModelFactory(factory.django.DjangoModelFactory):
3969
class Meta:
4070
model = Facility
@@ -325,7 +355,7 @@ def create_buffer_and_store_dummy_data(transfer_session_id):
325355
return data
326356

327357

328-
class BaseClientTestCase(LiveServerTestCase):
358+
class BaseClientTestCase(FreePortLiveServerTestCase):
329359
profile = "facilitydata"
330360

331361
def setUp(self):

‎tests/testapp/tests/integration/test_syncsession.py‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
import json
33
import os
4-
import socket
54
import subprocess
65
import sys
76
import time
@@ -29,6 +28,7 @@
2928
from morango.sync.controller import MorangoProfileController
3029

3130
from ..compat import EnvironmentVarGuard
31+
from ..helpers import get_free_tcp_port
3232

3333
SECOND_TEST_DATABASE = "default2"
3434
SECOND_SYSTEM_ID = "default2"
@@ -45,14 +45,6 @@ def second_environment():
4545
assert instance1.id != instance2.id
4646

4747

48-
def get_free_tcp_port():
49-
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
50-
tcp.bind(("", 0))
51-
addr, port = tcp.getsockname()
52-
tcp.close()
53-
return port
54-
55-
5648
class LiveServer:
5749
def __init__(self):
5850
self.env = os.environ.copy()

‎tests/testapp/tests/sync/test_syncsession.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import uuid
33

44
import mock
5-
from django.test.testcases import LiveServerTestCase
65
from django.test.utils import override_settings
76
from requests.exceptions import ChunkedEncodingError
87
from requests.exceptions import HTTPError
@@ -35,6 +34,7 @@
3534

3635
from ..helpers import BaseClientTestCase
3736
from ..helpers import BaseTransferClientTestCase
37+
from ..helpers import FreePortLiveServerTestCase
3838

3939

4040
def mock_patch_decorator(func):
@@ -52,7 +52,7 @@ def wrapper(*args, **kwargs):
5252
return wrapper
5353

5454

55-
class NetworkSyncConnectionTestCase(LiveServerTestCase):
55+
class NetworkSyncConnectionTestCase(FreePortLiveServerTestCase):
5656
def setUp(self):
5757
super(NetworkSyncConnectionTestCase, self).setUp()
5858
self.profile = "facilitydata"

0 commit comments

Comments
 (0)