Skip to content

Commit 4753428

Browse files
sylwiaszunejkodkropachev
authored andcommitted
test_libevreactor: Restore preparer after cleanup
test_watchers_are_finished calls libev__cleanup(), which stops the shared libev loop preparer. If a timer test runs later, the stopped preparer means timers are never scheduled and the test can hang. Restore _global_loop._shutdown and restart _global_loop._preparer after the cleanup path runs. Put the restoration in a finally block so the shared loop is left usable even if the post-cleanup watcher assertions fail.
1 parent ca42a54 commit 4753428

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

tests/unit/io/test_libevreactor.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,33 @@ def test_watchers_are_finished(self):
6969
@test_category connection
7070
"""
7171
from cassandra.io.libevreactor import _global_loop
72-
with patch.object(_global_loop, "_thread"),\
73-
patch.object(_global_loop, "notify"):
74-
75-
self.make_connection()
76-
77-
# We have to make a copy because the connections shouldn't
78-
# be alive when we verify them
79-
live_connections = set(_global_loop._live_conns)
80-
81-
# This simulates the process ending without cluster.shutdown()
82-
# being called, then with atexit _cleanup for libevreactor would
83-
# be called
84-
libev__cleanup(_global_loop)
85-
for conn in live_connections:
86-
assert conn._write_watcher.stop.mock_calls
87-
assert conn._read_watcher.stop.mock_calls
88-
89-
_global_loop._shutdown = False
72+
reactor_needs_restore = False
73+
try:
74+
with patch.object(_global_loop, "_thread"),\
75+
patch.object(_global_loop, "notify"):
76+
77+
self.make_connection()
78+
79+
# We have to make a copy because the connections shouldn't
80+
# be alive when we verify them
81+
live_connections = set(_global_loop._live_conns)
82+
83+
# This simulates the process ending without cluster.shutdown()
84+
# being called, then with atexit _cleanup for libevreactor would
85+
# be called
86+
reactor_needs_restore = True
87+
libev__cleanup(_global_loop)
88+
for conn in live_connections:
89+
assert conn._write_watcher.stop.mock_calls
90+
assert conn._read_watcher.stop.mock_calls
91+
92+
finally:
93+
if reactor_needs_restore:
94+
_global_loop._shutdown = False
95+
# _cleanup stopped the prepare watcher; restart it so the shared
96+
# singleton loop is left in a working state for subsequent tests
97+
# (otherwise timers would never be scheduled and tests would hang).
98+
_global_loop._preparer.start()
9099

91100

92101
class LibevTimerPatcher(unittest.TestCase):

0 commit comments

Comments
 (0)