|
| 1 | +# Copyright ScyllaDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Unit tests for the deserializer caches in deserializers.pyx. |
| 17 | +
|
| 18 | +Validates cache hit/miss behaviour, bounded eviction, the |
| 19 | +clear_deserializer_caches() API (needed after runtime Des* overrides), |
| 20 | +and the get_deserializer_cache_sizes() diagnostic helper. |
| 21 | +""" |
| 22 | + |
| 23 | +import unittest |
| 24 | + |
| 25 | +from tests.unit.cython.utils import cythontest |
| 26 | + |
| 27 | +try: |
| 28 | + from cassandra.deserializers import ( |
| 29 | + clear_deserializer_caches, |
| 30 | + find_deserializer, |
| 31 | + get_deserializer_cache_sizes, |
| 32 | + make_deserializers, |
| 33 | + ) |
| 34 | + |
| 35 | + _HAS_DESERIALIZERS = True |
| 36 | +except ImportError: |
| 37 | + _HAS_DESERIALIZERS = False |
| 38 | + |
| 39 | +from cassandra import cqltypes |
| 40 | + |
| 41 | + |
| 42 | +# --------------------------------------------------------------------------- |
| 43 | +# Tests |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | + |
| 46 | + |
| 47 | +class DeserializerCacheTest(unittest.TestCase): |
| 48 | + """Tests for find_deserializer / make_deserializers caching.""" |
| 49 | + |
| 50 | + def setUp(self): |
| 51 | + if _HAS_DESERIALIZERS: |
| 52 | + clear_deserializer_caches() |
| 53 | + |
| 54 | + def tearDown(self): |
| 55 | + if _HAS_DESERIALIZERS: |
| 56 | + clear_deserializer_caches() |
| 57 | + |
| 58 | + # -- find_deserializer cache ------------------------------------------- |
| 59 | + |
| 60 | + @cythontest |
| 61 | + def test_find_cache_hit_same_object(self): |
| 62 | + """Repeated calls for the same cqltype return the same instance.""" |
| 63 | + d1 = find_deserializer(cqltypes.Int32Type) |
| 64 | + d2 = find_deserializer(cqltypes.Int32Type) |
| 65 | + self.assertIs(d1, d2) |
| 66 | + |
| 67 | + @cythontest |
| 68 | + def test_find_cache_miss_different_types(self): |
| 69 | + """Different cqltypes produce different deserializer instances.""" |
| 70 | + d_int = find_deserializer(cqltypes.Int32Type) |
| 71 | + d_utf = find_deserializer(cqltypes.UTF8Type) |
| 72 | + self.assertIsNot(d_int, d_utf) |
| 73 | + |
| 74 | + @cythontest |
| 75 | + def test_find_returns_correct_deserializer_class(self): |
| 76 | + """The returned deserializer class name matches the cqltype.""" |
| 77 | + d = find_deserializer(cqltypes.Int32Type) |
| 78 | + self.assertEqual(type(d).__name__, "DesInt32Type") |
| 79 | + |
| 80 | + # -- make_deserializers cache ------------------------------------------ |
| 81 | + |
| 82 | + @cythontest |
| 83 | + def test_make_cache_hit_same_object(self): |
| 84 | + """Repeated calls with the same type list return the same array.""" |
| 85 | + types = [cqltypes.Int32Type, cqltypes.UTF8Type] |
| 86 | + r1 = make_deserializers(types) |
| 87 | + r2 = make_deserializers(types) |
| 88 | + self.assertIs(r1, r2) |
| 89 | + |
| 90 | + @cythontest |
| 91 | + def test_make_cache_correct_length(self): |
| 92 | + """Returned array has the right number of entries.""" |
| 93 | + types = [cqltypes.Int32Type, cqltypes.UTF8Type, cqltypes.BooleanType] |
| 94 | + result = make_deserializers(types) |
| 95 | + self.assertEqual(len(result), 3) |
| 96 | + |
| 97 | + # -- clear_deserializer_caches ----------------------------------------- |
| 98 | + |
| 99 | + @cythontest |
| 100 | + def test_clear_invalidates_find_cache(self): |
| 101 | + """After clearing, find_deserializer returns a new instance.""" |
| 102 | + d1 = find_deserializer(cqltypes.Int32Type) |
| 103 | + clear_deserializer_caches() |
| 104 | + d2 = find_deserializer(cqltypes.Int32Type) |
| 105 | + # New instance, but same deserializer class |
| 106 | + self.assertIsNot(d1, d2) |
| 107 | + self.assertEqual(type(d1).__name__, type(d2).__name__) |
| 108 | + |
| 109 | + @cythontest |
| 110 | + def test_clear_invalidates_make_cache(self): |
| 111 | + """After clearing, make_deserializers returns a new array.""" |
| 112 | + types = [cqltypes.Int32Type, cqltypes.UTF8Type] |
| 113 | + r1 = make_deserializers(types) |
| 114 | + clear_deserializer_caches() |
| 115 | + r2 = make_deserializers(types) |
| 116 | + self.assertIsNot(r1, r2) |
| 117 | + |
| 118 | + # -- get_deserializer_cache_sizes -------------------------------------- |
| 119 | + |
| 120 | + @cythontest |
| 121 | + def test_cache_sizes_empty_after_clear(self): |
| 122 | + """Sizes are (0, 0) immediately after clearing.""" |
| 123 | + find_size, make_size = get_deserializer_cache_sizes() |
| 124 | + self.assertEqual(find_size, 0) |
| 125 | + self.assertEqual(make_size, 0) |
| 126 | + |
| 127 | + @cythontest |
| 128 | + def test_cache_sizes_increment(self): |
| 129 | + """Sizes reflect the number of cached entries.""" |
| 130 | + find_deserializer(cqltypes.Int32Type) |
| 131 | + find_deserializer(cqltypes.UTF8Type) |
| 132 | + make_deserializers([cqltypes.Int32Type, cqltypes.UTF8Type]) |
| 133 | + |
| 134 | + find_size, make_size = get_deserializer_cache_sizes() |
| 135 | + self.assertEqual(find_size, 2) |
| 136 | + self.assertEqual(make_size, 1) |
| 137 | + |
| 138 | + # -- bounded eviction -------------------------------------------------- |
| 139 | + |
| 140 | + @cythontest |
| 141 | + def test_find_cache_bounded_size(self): |
| 142 | + """find_deserializer cache should not exceed 256 entries.""" |
| 143 | + # Create 300 distinct cqltype objects via apply_parameters. |
| 144 | + # Each ListType.apply_parameters() call creates a fresh class. |
| 145 | + inner_types = [ |
| 146 | + cqltypes.Int32Type, |
| 147 | + cqltypes.UTF8Type, |
| 148 | + cqltypes.BooleanType, |
| 149 | + cqltypes.DoubleType, |
| 150 | + cqltypes.LongType, |
| 151 | + ] |
| 152 | + distinct_types = [] |
| 153 | + for i in range(300): |
| 154 | + # Create ListType(inner) — each apply_parameters returns a new |
| 155 | + # class object, so these are all distinct cache keys. |
| 156 | + inner = inner_types[i % len(inner_types)] |
| 157 | + ct = cqltypes.ListType.apply_parameters([inner]) |
| 158 | + distinct_types.append(ct) |
| 159 | + |
| 160 | + for ct in distinct_types: |
| 161 | + find_deserializer(ct) |
| 162 | + |
| 163 | + find_size, _ = get_deserializer_cache_sizes() |
| 164 | + self.assertLessEqual( |
| 165 | + find_size, |
| 166 | + 256, |
| 167 | + "find_deserializer cache should be bounded to 256, got %d" % find_size, |
| 168 | + ) |
| 169 | + |
| 170 | + @cythontest |
| 171 | + def test_make_cache_bounded_size(self): |
| 172 | + """make_deserializers cache should not exceed 256 entries.""" |
| 173 | + inner_types = [ |
| 174 | + cqltypes.Int32Type, |
| 175 | + cqltypes.UTF8Type, |
| 176 | + cqltypes.BooleanType, |
| 177 | + cqltypes.DoubleType, |
| 178 | + cqltypes.LongType, |
| 179 | + ] |
| 180 | + for i in range(300): |
| 181 | + inner = inner_types[i % len(inner_types)] |
| 182 | + ct = cqltypes.ListType.apply_parameters([inner]) |
| 183 | + make_deserializers([ct]) |
| 184 | + |
| 185 | + _, make_size = get_deserializer_cache_sizes() |
| 186 | + self.assertLessEqual( |
| 187 | + make_size, |
| 188 | + 256, |
| 189 | + "make_deserializers cache should be bounded to 256, got %d" % make_size, |
| 190 | + ) |
0 commit comments