@@ -626,29 +626,47 @@ def _impl_test_interactive_timers():
626
626
# We only want singleshot if we specify that ourselves, otherwise we want
627
627
# a repeating timer
628
628
import os
629
+ import sys
629
630
from unittest .mock import Mock
630
631
import matplotlib .pyplot as plt
631
632
# increase pause duration on CI to let things spin up
632
633
# particularly relevant for gtk3cairo
633
634
pause_time = 2 if os .getenv ("CI" ) else 0.5
635
+ expected_100ms_calls = int (pause_time / 0.1 )
634
636
fig = plt .figure ()
635
637
plt .pause (pause_time )
636
638
timer = fig .canvas .new_timer (0.1 )
637
639
mock = Mock ()
638
640
timer .add_callback (mock )
639
641
timer .start ()
640
642
plt .pause (pause_time )
641
- timer .stop ()
642
- assert mock .call_count > 1
643
+ # NOTE: The timer is as fast as possible, but this is different between backends
644
+ # so we can't assert on the exact number but it should be faster than 100ms
645
+ assert mock .call_count > expected_100ms_calls , \
646
+ f"Expected more than { expected_100ms_calls } calls, got { mock .call_count } "
647
+
648
+ # Test updating the interval updates a running timer
649
+ timer .interval = 100
650
+ mock .call_count = 0
651
+ plt .pause (pause_time )
652
+ # GTK4 on macos runners produces about 3x as many calls as expected
653
+ # It works locally and on Linux though, so only skip when running on CI
654
+ if not (os .getenv ("CI" )
655
+ and "gtk4" in os .getenv ("MPLBACKEND" )
656
+ and sys .platform == "darwin" ):
657
+ # Could be off due to when the timers actually get fired (especially on CI)
658
+ assert 1 < mock .call_count <= expected_100ms_calls + 1 , \
659
+ f"Expected less than { expected_100ms_calls + 1 } calls, " \
660
+ "got {mock.call_count}"
643
661
644
662
# Now turn it into a single shot timer and verify only one gets triggered
645
663
mock .call_count = 0
646
664
timer .single_shot = True
647
- timer .start ()
648
665
plt .pause (pause_time )
649
666
assert mock .call_count == 1
650
667
651
- # Make sure we can start the timer a second time
668
+ # Make sure we can start the timer after stopping
669
+ timer .stop ()
652
670
timer .start ()
653
671
plt .pause (pause_time )
654
672
assert mock .call_count == 2
0 commit comments