Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4890369

Browse files
committed
TST: Avoid hard-coded port in test_webagg
If the port's in use, then we want to allow retries and connect to whichever port it ends up on.
1 parent c8b3e18 commit 4890369

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import os
77
import platform
8+
import re
89
import signal
910
import subprocess
1011
import sys
@@ -16,7 +17,6 @@
1617

1718
import pytest
1819

19-
import matplotlib as mpl
2020
from matplotlib import _c_internal_utils
2121
from matplotlib.backend_tools import ToolToggleBase
2222
from matplotlib.testing import subprocess_run_helper as _run_helper, is_ci_environment
@@ -46,7 +46,7 @@ def wait_for(self, terminator):
4646
f'Subprocess died before emitting expected {terminator!r}')
4747
buf += c
4848
if buf.endswith(terminator):
49-
return
49+
return buf
5050

5151

5252
# Minimal smoke-testing of the backends for which the dependencies are
@@ -160,7 +160,6 @@ def _test_interactive_impl():
160160
from matplotlib.backend_bases import KeyEvent, FigureCanvasBase
161161
mpl.rcParams.update({
162162
"webagg.open_in_browser": False,
163-
"webagg.port_retries": 1,
164163
})
165164

166165
mpl.rcParams.update(json.loads(sys.argv[1]))
@@ -484,32 +483,33 @@ def test_cross_Qt_imports(host, mpl):
484483
@pytest.mark.skipif(sys.platform == "win32", reason="Cannot send SIGINT on Windows.")
485484
def test_webagg():
486485
pytest.importorskip("tornado")
487-
proc = subprocess.Popen(
488-
[sys.executable, "-c",
489-
inspect.getsource(_test_interactive_impl)
490-
+ "\n_test_interactive_impl()", "{}"],
491-
env={**os.environ, "MPLBACKEND": "webagg", "SOURCE_DATE_EPOCH": "0"})
492-
url = f'http://{mpl.rcParams["webagg.address"]}:{mpl.rcParams["webagg.port"]}'
493-
timeout = time.perf_counter() + _test_timeout
494-
try:
495-
while True:
496-
try:
497-
retcode = proc.poll()
498-
# check that the subprocess for the server is not dead
499-
assert retcode is None
500-
conn = urllib.request.urlopen(url)
501-
break
502-
except urllib.error.URLError:
503-
if time.perf_counter() > timeout:
504-
pytest.fail("Failed to connect to the webagg server.")
505-
else:
506-
continue
507-
conn.close()
508-
proc.send_signal(signal.SIGINT)
509-
assert proc.wait(timeout=_test_timeout) == 0
510-
finally:
511-
if proc.poll() is None:
512-
proc.kill()
486+
source = (inspect.getsource(_test_interactive_impl) +
487+
"\n_test_interactive_impl()")
488+
rc = '{"backend": "webagg"}'
489+
with _WaitForStringPopen([sys.executable, "-c", source, rc]) as proc:
490+
timeout = time.perf_counter() + _test_timeout
491+
try:
492+
buf = proc.wait_for('Press Ctrl+C')
493+
url = re.search(r'visit (https?:\/\/\S+)', buf).group(1)
494+
print(url)
495+
while True:
496+
try:
497+
retcode = proc.poll()
498+
# check that the subprocess for the server is not dead
499+
assert retcode is None
500+
conn = urllib.request.urlopen(url)
501+
break
502+
except urllib.error.URLError:
503+
if time.perf_counter() > timeout:
504+
pytest.fail("Failed to connect to the webagg server.")
505+
else:
506+
continue
507+
conn.close()
508+
proc.send_signal(signal.SIGINT)
509+
assert proc.wait(timeout=_test_timeout) == 0
510+
finally:
511+
if proc.poll() is None:
512+
proc.kill()
513513

514514

515515
def _lazy_headless():

0 commit comments

Comments
 (0)