|
5 | 5 | import json |
6 | 6 | import os |
7 | 7 | import platform |
| 8 | +import re |
8 | 9 | import signal |
9 | 10 | import subprocess |
10 | 11 | import sys |
|
16 | 17 |
|
17 | 18 | import pytest |
18 | 19 |
|
19 | | -import matplotlib as mpl |
20 | 20 | from matplotlib import _c_internal_utils |
21 | 21 | from matplotlib.backend_tools import ToolToggleBase |
22 | 22 | from matplotlib.testing import subprocess_run_helper as _run_helper, is_ci_environment |
@@ -46,7 +46,7 @@ def wait_for(self, terminator): |
46 | 46 | f'Subprocess died before emitting expected {terminator!r}') |
47 | 47 | buf += c |
48 | 48 | if buf.endswith(terminator): |
49 | | - return |
| 49 | + return buf |
50 | 50 |
|
51 | 51 |
|
52 | 52 | # Minimal smoke-testing of the backends for which the dependencies are |
@@ -160,7 +160,6 @@ def _test_interactive_impl(): |
160 | 160 | from matplotlib.backend_bases import KeyEvent, FigureCanvasBase |
161 | 161 | mpl.rcParams.update({ |
162 | 162 | "webagg.open_in_browser": False, |
163 | | - "webagg.port_retries": 1, |
164 | 163 | }) |
165 | 164 |
|
166 | 165 | mpl.rcParams.update(json.loads(sys.argv[1])) |
@@ -484,32 +483,33 @@ def test_cross_Qt_imports(host, mpl): |
484 | 483 | @pytest.mark.skipif(sys.platform == "win32", reason="Cannot send SIGINT on Windows.") |
485 | 484 | def test_webagg(): |
486 | 485 | 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() |
513 | 513 |
|
514 | 514 |
|
515 | 515 | def _lazy_headless(): |
|
0 commit comments