22
33import asyncio
44import os
5- import re
65import shutil
76import signal
87import subprocess
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-
3933def 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():
7569def 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
350356PRELOAD_TEXT = """
0 commit comments