Skip to content

Commit db3cb43

Browse files
authored
Fix test failures caused by port 8787 already in use (#9296)
1 parent 6e4ca76 commit db3cb43

5 files changed

Lines changed: 59 additions & 32 deletions

File tree

distributed/cli/tests/test_dask_scheduler.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import asyncio
44
import os
5-
import re
65
import shutil
76
import signal
87
import subprocess
@@ -26,16 +25,11 @@
2625
assert_can_connect_from_everywhere_4_6,
2726
assert_can_connect_locally_4,
2827
gen_test,
28+
get_dashboard_port,
2929
popen,
3030
)
3131

3232

33-
def _get_dashboard_port(client: Client) -> int:
34-
match = re.search(r":(\d+)\/status", client.dashboard_link)
35-
assert match
36-
return int(match.group(1))
37-
38-
3933
def test_defaults(loop, requires_default_ports):
4034
with popen([sys.executable, "-m", "dask", "scheduler"]):
4135

@@ -45,7 +39,7 @@ async def f():
4539

4640
with Client(f"127.0.0.1:{Scheduler.default_port}", loop=loop) as c:
4741
c.sync(f)
48-
assert _get_dashboard_port(c) == 8787
42+
assert get_dashboard_port(c) == 8787
4943

5044

5145
@pytest.mark.slow
@@ -75,9 +69,24 @@ async def f():
7569
def test_no_dashboard(loop, requires_default_ports):
7670
requests = pytest.importorskip("requests")
7771

78-
with popen([sys.executable, "-m", "dask", "scheduler", "--no-dashboard"]):
79-
with Client(f"127.0.0.1:{Scheduler.default_port}", loop=loop):
80-
response = requests.get("http://127.0.0.1:8787/status/")
72+
port = open_port()
73+
74+
with popen(
75+
[
76+
sys.executable,
77+
"-m",
78+
"dask",
79+
"scheduler",
80+
"--host",
81+
f"127.0.0.1:{port}",
82+
"--dashboard-address",
83+
"127.0.0.1:0",
84+
"--no-dashboard",
85+
]
86+
):
87+
with Client(f"127.0.0.1:{port}", loop=loop) as c:
88+
dashboard_port = get_dashboard_port(c)
89+
response = requests.get(f"http://127.0.0.1:{dashboard_port}/status/")
8190
assert response.status_code == 404
8291

8392

@@ -91,7 +100,7 @@ def test_dashboard(loop):
91100
[sys.executable, "-m", "dask", "scheduler", "--host", f"127.0.0.1:{port}"],
92101
):
93102
with Client(f"127.0.0.1:{port}", loop=loop) as c:
94-
dashboard_port = _get_dashboard_port(c)
103+
dashboard_port = get_dashboard_port(c)
95104

96105
names = ["localhost", "127.0.0.1", get_ip()]
97106
start = time()
@@ -172,10 +181,8 @@ def test_dashboard_allowlist(loop):
172181
pytest.importorskip("bokeh")
173182
requests = pytest.importorskip("requests")
174183

175-
with pytest.raises(requests.ConnectionError):
176-
requests.get("http://localhost:8787/status/").ok
177-
178184
port = open_port()
185+
179186
with popen(
180187
[
181188
sys.executable,
@@ -184,15 +191,15 @@ def test_dashboard_allowlist(loop):
184191
"scheduler",
185192
f"--port={port}",
186193
]
187-
) as proc:
188-
with Client(f"127.0.0.1:{port}", loop=loop) as c:
194+
):
195+
with Client(f"127.0.0.1:{port}", loop=loop):
189196
pass
190197

191198
start = time()
192199
while True:
193200
try:
194-
for name in ["127.0.0.2", "127.0.0.3"]:
195-
response = requests.get("http://%s:8787/status/" % name)
201+
for ip in ["127.0.0.2", "127.0.0.3"]:
202+
response = requests.get(f"http://{ip}:8787/status/")
196203
assert response.ok
197204
break
198205
except Exception as f:
@@ -343,8 +350,7 @@ def test_dashboard_port_zero(loop):
343350
],
344351
):
345352
with Client(f"tcp://127.0.0.1:{port}", loop=loop) as c:
346-
port = _get_dashboard_port(c)
347-
assert port > 0
353+
assert get_dashboard_port(c) > 0
348354

349355

350356
PRELOAD_TEXT = """

distributed/comm/tests/test_ws.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from distributed.comm.registry import backends, get_backend
1616
from distributed.comm.tests.test_comms import check_tls_extra
1717
from distributed.security import Security
18+
from distributed.utils import open_port
1819
from distributed.utils_test import (
1920
gen_cluster,
2021
gen_test,
@@ -105,9 +106,12 @@ async def test_expect_scheduler_ssl_when_sharing_server(tmp_path):
105106
"distributed.scheduler.dashboard.tls.key": key_path,
106107
"distributed.scheduler.dashboard.tls.cert": cert_path,
107108
}
109+
port = open_port()
108110
with dask.config.set(c):
109111
with pytest.raises(RuntimeError):
110-
async with Scheduler(protocol="ws://", dashboard=True, port=8787):
112+
async with Scheduler(
113+
protocol="ws://", port=port, dashboard_address=f":{port}"
114+
):
111115
pass
112116

113117

distributed/diagnostics/tests/test_memray.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
def test_all_workers(tmp_path, loop):
1414
n_workers = 7
15-
with LocalCluster(n_workers=n_workers, loop=loop) as cluster:
15+
with LocalCluster(
16+
n_workers=n_workers, loop=loop, dashboard_address=":0"
17+
) as cluster:
1618
with Client(cluster, loop=loop) as client:
1719
with memray_workers(tmp_path):
1820
pass

distributed/tests/test_jupyter.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from distributed.compatibility import MACOS, WINDOWS
1515
from distributed.core import Status
1616
from distributed.utils import open_port
17-
from distributed.utils_test import gen_test, popen
17+
from distributed.utils_test import gen_test, get_dashboard_port, popen
1818

1919
pytest.importorskip("jupyter_server")
2020

@@ -47,22 +47,27 @@ def test_jupyter_cli(loop, requires_default_ports):
4747
requests = pytest.importorskip("requests")
4848

4949
port = open_port()
50+
5051
with popen(
5152
[
5253
sys.executable,
5354
"-m",
5455
"dask",
5556
"scheduler",
5657
"--jupyter",
57-
"--no-dashboard",
58+
"--dashboard-address",
59+
"127.0.0.1:0",
5860
"--host",
5961
f"127.0.0.1:{port}",
6062
],
6163
terminate_timeout=120,
6264
kill_timeout=60,
6365
):
64-
with Client(f"127.0.0.1:{port}", loop=loop):
65-
response = requests.get("http://127.0.0.1:8787/jupyter/api/status")
66+
with Client(f"127.0.0.1:{port}", loop=loop) as c:
67+
dashboard_port = get_dashboard_port(c)
68+
response = requests.get(
69+
f"http://127.0.0.1:{dashboard_port}/jupyter/api/status"
70+
)
6671
assert response.status_code == 200
6772

6873

@@ -129,7 +134,8 @@ def test_shutdowns_cleanly(requires_default_ports):
129134
"dask",
130135
"scheduler",
131136
"--jupyter",
132-
"--no-dashboard",
137+
"--dashboard-address",
138+
"127.0.0.1:0",
133139
"--host",
134140
f"127.0.0.1:{port}",
135141
],
@@ -140,13 +146,15 @@ def test_shutdowns_cleanly(requires_default_ports):
140146
)
141147

142148
# wait until scheduler is running
143-
with Client(f"127.0.0.1:{port}"):
144-
pass
149+
with Client(f"127.0.0.1:{port}") as c:
150+
dashboard_port = get_dashboard_port(c)
145151

146152
with requests.Session() as session:
147-
session.get("http://127.0.0.1:8787/jupyter/lab").raise_for_status()
153+
session.get(
154+
f"http://127.0.0.1:{dashboard_port}/jupyter/lab"
155+
).raise_for_status()
148156
session.post(
149-
"http://127.0.0.1:8787/jupyter/api/shutdown",
157+
f"http://127.0.0.1:{dashboard_port}/jupyter/api/shutdown",
150158
headers={"X-XSRFToken": session.cookies["_xsrf"]},
151159
).raise_for_status()
152160

distributed/utils_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import logging
1212
import multiprocessing
1313
import os
14+
import re
1415
import signal
1516
import socket
1617
import ssl
@@ -2578,3 +2579,9 @@ async def padded_time(before=0.05, after=0.05):
25782579
t = time()
25792580
await asyncio.sleep(after)
25802581
return t
2582+
2583+
2584+
def get_dashboard_port(client: Client) -> int:
2585+
match = re.search(r":(\d+)\/status", client.dashboard_link)
2586+
assert match
2587+
return int(match.group(1))

0 commit comments

Comments
 (0)