11import importlib
22import os
3- from subprocess import Popen
3+ import signal
4+ import subprocess
45import sys
6+ import time
7+ import urllib .request
58
69import pytest
710
11+ import matplotlib as mpl
12+
813
914# Minimal smoke-testing of the backends for which the dependencies are
1015# PyPI-installable on Travis. They are not available for all tested Python
@@ -17,6 +22,7 @@ def _get_testable_interactive_backends():
1722 (["PyQt5" ], "qt5agg" ),
1823 (["cairocffi" , "PyQt5" ], "qt5cairo" ),
1924 (["tkinter" ], "tkagg" ),
25+ (["wx" ], "wx" ),
2026 (["wx" ], "wxagg" )]:
2127 reason = None
2228 if not os .environ .get ("DISPLAY" ):
@@ -30,20 +36,47 @@ def _get_testable_interactive_backends():
3036
3137_test_script = """\
3238 import sys
33- from matplotlib import pyplot as plt
39+ from matplotlib import pyplot as plt, rcParams
40+ rcParams.update({
41+ "webagg.open_in_browser": False,
42+ "webagg.port_retries": 1,
43+ })
3444
3545fig = plt.figure()
3646ax = fig.add_subplot(111)
37- ax.plot([1,2,3 ], [1,3,1 ])
47+ ax.plot([1, 2 ], [2, 3 ])
3848fig.canvas.mpl_connect("draw_event", lambda event: sys.exit())
3949plt.show()
4050"""
51+ _test_timeout = 10 # Empirically, 1s is not enough on Travis.
4152
4253
4354@pytest .mark .parametrize ("backend" , _get_testable_interactive_backends ())
4455@pytest .mark .flaky (reruns = 3 )
45- def test_backend (backend ):
46- proc = Popen ([sys .executable , "-c" , _test_script ],
47- env = {** os .environ , "MPLBACKEND" : backend })
48- # Empirically, 1s is not enough on Travis.
49- assert proc .wait (timeout = 10 ) == 0
56+ def test_interactive_backend (backend ):
57+ subprocess .run ([sys .executable , "-c" , _test_script ],
58+ env = {** os .environ , "MPLBACKEND" : backend },
59+ check = True , # Throw on failure.
60+ timeout = _test_timeout )
61+
62+
63+ @pytest .mark .skipif (os .name == "nt" , reason = "Cannot send SIGINT on Windows." )
64+ def test_webagg ():
65+ pytest .importorskip ("tornado" )
66+ proc = subprocess .Popen ([sys .executable , "-c" , _test_script ],
67+ env = {** os .environ , "MPLBACKEND" : "webagg" })
68+ url = "http://{}:{}" .format (
69+ mpl .rcParams ["webagg.address" ], mpl .rcParams ["webagg.port" ])
70+ timeout = time .perf_counter () + _test_timeout
71+ while True :
72+ try :
73+ conn = urllib .request .urlopen (url )
74+ break
75+ except urllib .error .URLError :
76+ if time .perf_counter () > timeout :
77+ pytest .fail ("Failed to connect to the webagg server." )
78+ else :
79+ continue
80+ conn .close ()
81+ proc .send_signal (signal .SIGINT )
82+ assert proc .wait (timeout = _test_timeout ) == 0
0 commit comments