|
| 1 | +""" |
| 2 | +Unit tests verifying that RLock -> Lock conversion is safe. |
| 3 | +
|
| 4 | +Tests that the lock objects are of the correct type and that basic |
| 5 | +operations (connect, metadata, pool) still work correctly. |
| 6 | +""" |
| 7 | +import threading |
| 8 | +import unittest |
| 9 | +from unittest.mock import Mock |
| 10 | + |
| 11 | +from cassandra.metadata import Metadata, TokenMap |
| 12 | +from cassandra.pool import Host |
| 13 | + |
| 14 | + |
| 15 | +class TestLockTypes(unittest.TestCase): |
| 16 | + """Verify each converted lock is a plain Lock, not RLock.""" |
| 17 | + |
| 18 | + def _assert_is_lock_not_rlock(self, lock_obj): |
| 19 | + """Assert the given object is a plain Lock, not an RLock.""" |
| 20 | + # In CPython, Lock() creates _thread.lock, RLock() creates _thread.RLock |
| 21 | + lock_type_name = type(lock_obj).__name__ |
| 22 | + self.assertNotIn('RLock', lock_type_name, |
| 23 | + f"Expected plain Lock but got {type(lock_obj)}") |
| 24 | + |
| 25 | + def test_metadata_hosts_lock_is_plain_lock(self): |
| 26 | + """Metadata._hosts_lock should be a plain Lock.""" |
| 27 | + m = Metadata() |
| 28 | + self._assert_is_lock_not_rlock(m._hosts_lock) |
| 29 | + |
| 30 | + def test_metadata_rebuild_lock_is_plain_lock(self): |
| 31 | + """TokenMap._rebuild_lock should be a plain Lock.""" |
| 32 | + tm = TokenMap( |
| 33 | + token_class=Mock(), |
| 34 | + token_to_host_owner={}, |
| 35 | + all_tokens=[], |
| 36 | + metadata=Mock() |
| 37 | + ) |
| 38 | + self._assert_is_lock_not_rlock(tm._rebuild_lock) |
| 39 | + |
| 40 | + def test_host_lock_is_plain_lock(self): |
| 41 | + """Host.lock should be a plain Lock.""" |
| 42 | + import uuid |
| 43 | + h = Host( |
| 44 | + endpoint=Mock(), |
| 45 | + conviction_policy_factory=Mock(), |
| 46 | + host_id=uuid.uuid4() |
| 47 | + ) |
| 48 | + self._assert_is_lock_not_rlock(h.lock) |
| 49 | + |
| 50 | + def test_cqlengine_connection_lock_is_plain_lock(self): |
| 51 | + """CQLEngine Connection.lazy_connect_lock should be a plain Lock.""" |
| 52 | + from cassandra.cqlengine.connection import Connection as CQLConn |
| 53 | + c = CQLConn.__new__(CQLConn) |
| 54 | + c.lazy_connect_lock = threading.Lock() |
| 55 | + self._assert_is_lock_not_rlock(c.lazy_connect_lock) |
| 56 | + |
| 57 | + |
| 58 | +class TestMetadataOperationsWithLock(unittest.TestCase): |
| 59 | + """Verify metadata operations work correctly with plain Lock.""" |
| 60 | + |
| 61 | + def test_add_and_get_host(self): |
| 62 | + """add_or_return_host + get_host should work with plain Lock.""" |
| 63 | + import uuid |
| 64 | + m = Metadata() |
| 65 | + endpoint = Mock() |
| 66 | + host = Host(endpoint=endpoint, conviction_policy_factory=Mock(), |
| 67 | + host_id=uuid.uuid4()) |
| 68 | + returned, new = m.add_or_return_host(host) |
| 69 | + self.assertTrue(new) |
| 70 | + self.assertIs(returned, host) |
| 71 | + |
| 72 | + # Second add should return same host |
| 73 | + returned2, new2 = m.add_or_return_host(host) |
| 74 | + self.assertFalse(new2) |
| 75 | + self.assertIs(returned2, host) |
| 76 | + |
| 77 | + def test_update_host_sequential_lock(self): |
| 78 | + """update_host acquires lock twice sequentially — must not deadlock.""" |
| 79 | + import uuid |
| 80 | + m = Metadata() |
| 81 | + old_endpoint = Mock() |
| 82 | + new_endpoint = Mock() |
| 83 | + host = Host(endpoint=new_endpoint, conviction_policy_factory=Mock(), |
| 84 | + host_id=uuid.uuid4()) |
| 85 | + # update_host calls add_or_return_host (acquires lock, releases), |
| 86 | + # then acquires lock again for endpoint update. |
| 87 | + # With plain Lock, this must NOT deadlock. |
| 88 | + m.update_host(host, old_endpoint) |
| 89 | + # Host should be retrievable by host_id |
| 90 | + result = m.get_host_by_host_id(host.host_id) |
| 91 | + self.assertIs(result, host) |
| 92 | + |
| 93 | + def test_remove_host(self): |
| 94 | + """remove_host should work with plain Lock.""" |
| 95 | + import uuid |
| 96 | + m = Metadata() |
| 97 | + endpoint = Mock() |
| 98 | + host = Host(endpoint=endpoint, conviction_policy_factory=Mock(), |
| 99 | + host_id=uuid.uuid4()) |
| 100 | + m.add_or_return_host(host) |
| 101 | + removed = m.remove_host(host) |
| 102 | + self.assertTrue(removed) |
| 103 | + |
| 104 | + def test_all_hosts(self): |
| 105 | + """all_hosts should work under plain Lock.""" |
| 106 | + import uuid |
| 107 | + m = Metadata() |
| 108 | + hosts = [] |
| 109 | + for _ in range(3): |
| 110 | + h = Host(endpoint=Mock(), conviction_policy_factory=Mock(), |
| 111 | + host_id=uuid.uuid4()) |
| 112 | + m.add_or_return_host(h) |
| 113 | + hosts.append(h) |
| 114 | + all_h = m.all_hosts() |
| 115 | + self.assertEqual(len(all_h), 3) |
| 116 | + |
| 117 | + |
| 118 | +class TestHostLockOperations(unittest.TestCase): |
| 119 | + """Verify Host lock operations work with plain Lock.""" |
| 120 | + |
| 121 | + def test_get_and_set_reconnection_handler(self): |
| 122 | + """get_and_set_reconnection_handler should work with plain Lock.""" |
| 123 | + import uuid |
| 124 | + h = Host(endpoint=Mock(), conviction_policy_factory=Mock(), |
| 125 | + host_id=uuid.uuid4()) |
| 126 | + handler = Mock() |
| 127 | + old = h.get_and_set_reconnection_handler(handler) |
| 128 | + self.assertIsNone(old) |
| 129 | + old2 = h.get_and_set_reconnection_handler(Mock()) |
| 130 | + self.assertIs(old2, handler) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + unittest.main() |
0 commit comments