1010
1111import os
1212import tempfile
13+ import time
1314
1415import numpy as np
1516
1617
18+ def _wait_for_file (path , timeout = 5 ):
19+ """Poll until `path` exists or `timeout` seconds elapse."""
20+ deadline = time .monotonic () + timeout
21+ while not os .path .exists (path ) and time .monotonic () < deadline :
22+ time .sleep (0.05 )
23+
24+
1725def test_detachable_plot_with_detach ():
1826 """Plot.plot with detach=True should complete without PicklingError."""
1927 from batchflow .plotter .plot import Plot
@@ -22,11 +30,9 @@ def test_detachable_plot_with_detach():
2230 with tempfile .TemporaryDirectory () as tmpdir :
2331 savepath = os .path .join (tmpdir , "test_detach.png" )
2432 p = Plot (data = data , mode = "image" , show = False , detach = True , savepath = savepath )
25- # detach=True runs in a daemon thread — give it a moment to finish
26- import time
27- time .sleep (1 )
28- # The plot object should have been created without error
33+ _wait_for_file (savepath )
2934 assert p is not None
35+ assert os .path .exists (savepath )
3036
3137
3238def test_plot_save_with_detach ():
@@ -41,10 +47,7 @@ def test_plot_save_with_detach():
4147
4248 savepath2 = os .path .join (tmpdir , "test_save_detach2.png" )
4349 p .save (savepath = savepath2 , detach = True )
44- import time
45- deadline = time .monotonic () + 5
46- while not os .path .exists (savepath2 ) and time .monotonic () < deadline :
47- time .sleep (0.05 )
50+ _wait_for_file (savepath2 )
4851 assert os .path .exists (savepath2 )
4952
5053
0 commit comments