Skip to content

Commit e337ef2

Browse files
author
bjmb
committed
Moved test_watchers_are_finished to unit tests
1 parent be535e7 commit e337ef2

2 files changed

Lines changed: 38 additions & 47 deletions

File tree

tests/integration/standard/test_connection.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
try:
3939
from cassandra.io.libevreactor import LibevConnection
40-
from cassandra.io.libevreactor import _cleanup as libev__cleanup
4140
except ImportError:
4241
LibevConnection = None
4342

@@ -400,44 +399,3 @@ def setUp(self):
400399
raise unittest.SkipTest(
401400
'libev does not appear to be installed properly')
402401
ConnectionTests.setUp(self)
403-
404-
def test_watchers_are_finished(self):
405-
"""
406-
Test for asserting that watchers are closed in LibevConnection
407-
408-
It will open a connection to the Cluster and then abruptly clean it simulating,
409-
a process termination without calling cluster.shutdown(), which would trigger
410-
LibevConnection._libevloop._cleanup. Then it will check the watchers have been closed
411-
Finally it will restore the LibevConnection reactor so it doesn't affect
412-
the rest of the tests
413-
414-
@since 3.10
415-
@jira_ticket PYTHON-747
416-
@expected_result the watchers are closed
417-
418-
@test_category connection
419-
"""
420-
421-
# conn._write_watcher and conn._read_watcher will be closed
422-
# when the request is finished so it may not be _cleanup the
423-
# one who ends up cleaning them everytime.
424-
for _ in range(10):
425-
cluster = Cluster(connection_class=LibevConnection)
426-
session = cluster.connect(wait_for_all_pools=True)
427-
428-
session.execute_async("SELECT * FROM system.local LIMIT 1")
429-
# We have to make a copy because the connections shouldn't
430-
# be alive when we verify them
431-
live_connections = set(LibevConnection._libevloop._live_conns)
432-
433-
# This simulates the process ending without cluster.shutdown()
434-
# being called, then with atexit _cleanup for libevreactor would
435-
# be called
436-
libev__cleanup(weakref.ref(LibevConnection._libevloop))
437-
438-
for conn in live_connections:
439-
for watcher in (conn._write_watcher, conn._read_watcher):
440-
self.assertTrue(watcher is None or not watcher.is_active())
441-
442-
cluster.shutdown()
443-
LibevConnection._libevloop = None

tests/unit/io/test_libevreactor.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,24 @@
2020
import math
2121
from mock import patch, Mock
2222
import os
23+
import weakref
2324
import six
2425
from six import BytesIO
2526
from socket import error as socket_error
26-
import sys
27-
import time
2827

28+
from cassandra.io.libevreactor import _cleanup as libev__cleanup
2929
from cassandra.connection import (HEADER_DIRECTION_TO_CLIENT,
3030
ConnectionException, ProtocolError)
3131

3232
from cassandra.protocol import (write_stringmultimap, write_int, write_string,
3333
SupportedMessage, ReadyMessage, ServerError)
3434
from cassandra.marshal import uint8_pack, uint32_pack, int32_pack
35-
from tests.unit.io.utils import TimerCallback
36-
from tests.unit.io.utils import submit_and_wait_for_completion
35+
3736
from tests import is_monkey_patched
3837

3938

4039
try:
41-
from cassandra.io.libevreactor import LibevConnection
40+
from cassandra.io.libevreactor import LibevConnection, LibevLoop
4241
except ImportError:
4342
LibevConnection = None # noqa
4443

@@ -296,3 +295,37 @@ def test_partial_message_read(self, *args):
296295

297296
self.assertTrue(c.connected_event.is_set())
298297
self.assertFalse(c.is_defunct)
298+
299+
def test_watchers_are_finished(self, *args):
300+
"""
301+
Test for asserting that watchers are closed in LibevConnection
302+
303+
This test simulates a process termination without calling cluster.shutdown(), which would trigger
304+
LibevConnection._libevloop._cleanup. It will check the watchers have been closed
305+
Finally it will restore the LibevConnection reactor so it doesn't affect
306+
the rest of the tests
307+
308+
@since 3.10
309+
@jira_ticket PYTHON-747
310+
@expected_result the watchers are closed
311+
312+
@test_category connection
313+
"""
314+
with patch.object(LibevConnection._libevloop, "_thread"), \
315+
patch.object(LibevConnection._libevloop, "notify"):
316+
317+
self.make_connection()
318+
319+
# We have to make a copy because the connections shouldn't
320+
# be alive when we verify them
321+
live_connections = set(LibevConnection._libevloop._live_conns)
322+
323+
# This simulates the process ending without cluster.shutdown()
324+
# being called, then with atexit _cleanup for libevreactor would
325+
# be called
326+
libev__cleanup(weakref.ref(LibevConnection._libevloop))
327+
for conn in live_connections:
328+
for watcher in (conn._write_watcher, conn._read_watcher):
329+
self.assertTrue(watcher.stop.mock_calls)
330+
331+
LibevConnection._libevloop._shutdown = False

0 commit comments

Comments
 (0)