Skip to content

Commit c578d8c

Browse files
Address timeout issues directly
1 parent 4b693ad commit c578d8c

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import threading
2-
import time
1+
import os
2+
import subprocess
3+
import sys
34
import tkinter
45

56
import numpy as np
@@ -56,25 +57,44 @@ def legitmate_quit():
5657

5758

5859
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
60+
@pytest.mark.flaky(reruns=3)
5961
def test_figuremanager_cleans_own_mainloop():
60-
root = tkinter.Tk()
61-
plt.plot([1, 2, 3], [1, 2, 5])
62-
can_detect_mainloop = False
63-
thread_died_before_quit = True
64-
65-
def target():
66-
nonlocal can_detect_mainloop
67-
nonlocal thread_died_before_quit
68-
from matplotlib.cbook import _get_running_interactive_framework
69-
70-
time.sleep(0.1) # should poll for mainloop being up
71-
can_detect_mainloop = 'tk' == _get_running_interactive_framework()
72-
plt.close()
73-
time.sleep(0.1) # should poll for mainloop going down
74-
root.quit()
75-
thread_died_before_quit = False
76-
77-
threading.Thread(target=target, daemon=True).start()
78-
plt.show(block=True)
79-
assert can_detect_mainloop
80-
assert thread_died_before_quit
62+
script = '''
63+
import tkinter
64+
import time
65+
import matplotlib.pyplot as plt
66+
import threading
67+
from matplotlib.cbook import _get_running_interactive_framework
68+
69+
root = tkinter.Tk()
70+
plt.plot([1, 2, 3], [1, 2, 5])
71+
72+
def target():
73+
while not 'tk' == _get_running_interactive_framework():
74+
time.sleep(.01)
75+
plt.close()
76+
if show_finished_event.wait():
77+
print('success')
78+
79+
show_finished_event = threading.Event()
80+
thread = threading.Thread(target=target, daemon=True)
81+
thread.start()
82+
plt.show(block=True) # testing if this function hangs
83+
show_finished_event.set()
84+
thread.join()
85+
86+
'''
87+
try:
88+
proc = subprocess.run(
89+
[sys.executable, "-c", script],
90+
env={**os.environ, "MPLBACKEND": "TkAgg", "SOURCE_DATE_EPOCH": "0"},
91+
timeout=10,
92+
stdout=subprocess.PIPE,
93+
universal_newlines=True,
94+
check=True
95+
)
96+
except subprocess.TimeoutExpired:
97+
pytest.fail("Most likely plot.show(block=True) hung")
98+
except subprocess.CalledProcessError:
99+
pytest.fail("Subprocess failed to test intended behavior")
100+
assert proc.stdout.count("success") == 1

0 commit comments

Comments
 (0)