Skip to content

Commit dc99418

Browse files
fix: add missing close() calls and sync test files with main branch
1 parent 20dd3e2 commit dc99418

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

tests/test_cache.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_cache_basic():
3737
assert cache.get("none_key") is None
3838

3939
assert cache.number_of_items == 8
40+
cache.close()
4041

4142

4243
def test_cache_miss():
@@ -46,6 +47,7 @@ def test_cache_miss():
4647
cache.set("key1", "value1")
4748
assert cache.get("key1") == "value1"
4849
assert cache.get("key2") is CACHE_MISS
50+
cache.close()
4951

5052

5153
def test_cache_overwrite():
@@ -56,6 +58,7 @@ def test_cache_overwrite():
5658
cache.set("key", "value2")
5759
assert cache.get("key") == "value2"
5860
assert cache.number_of_items == 1
61+
cache.close()
5962

6063

6164
def test_cache_ttl():
@@ -88,6 +91,7 @@ def test_cache_ttl():
8891

8992
time.sleep(0.2)
9093
assert cache.get("key4") is CACHE_MISS
94+
cache.close()
9195

9296

9397
def test_cache_ttl_thread():
@@ -128,6 +132,7 @@ def custom_deserializer(value):
128132
# Should raise ValueError for non-string values
129133
with pytest.raises(ValueError, match="Failed to serialize"):
130134
cache.set("key2", 42)
135+
cache.close()
131136

132137

133138
def test_cache_serialization_error():
@@ -138,6 +143,7 @@ def failing_serializer(value):
138143

139144
with pytest.raises(ValueError, match="Failed to serialize value of type"):
140145
cache.set("key", "value")
146+
cache.close()
141147

142148

143149
def test_cache_deserialization_error():
@@ -151,6 +157,7 @@ def failing_deserializer(value):
151157

152158
with pytest.raises(ValueError, match="Failed to deserialize value"):
153159
cache.get("key")
160+
cache.close()
154161

155162

156163
def test_cache_complex_objects():
@@ -174,6 +181,7 @@ def test_cache_complex_objects():
174181
}
175182
cache.set("complex", complex_obj)
176183
assert cache.get("complex") == complex_obj
184+
cache.close()
177185

178186

179187
def test_cache_lru_behavior():
@@ -196,6 +204,7 @@ def test_cache_lru_behavior():
196204
assert cache.get("key2") is CACHE_MISS # Should be evicted
197205
assert cache.get("key3") == "value3" # Should still be there
198206
assert cache.get("key4") == "value4" # Should be there
207+
cache.close()
199208

200209

201210
def test_cache_lru_behavior_with_set_overwrite():
@@ -218,6 +227,7 @@ def test_cache_lru_behavior_with_set_overwrite():
218227
assert cache.get("key2") is CACHE_MISS # Should be evicted
219228
assert cache.get("key3") == "value3" # Should still be there
220229
assert cache.get("key4") == "value4" # Should be there
230+
cache.close()
221231

222232

223233
def test_cache_size_limit():
@@ -236,6 +246,7 @@ def test_cache_size_limit():
236246

237247
# Large value should be rejected (exceeds size_limit_in_bytes / 2)
238248
assert cache.get("large") is CACHE_MISS
249+
cache.close()
239250

240251

241252
def test_cache_delete():
@@ -256,6 +267,7 @@ def test_cache_delete():
256267
# Delete non-existent key should do nothing
257268
assert cache.delete("nonexistent") is False
258269
assert cache.number_of_items == 1
270+
cache.close()
259271

260272

261273
def test_cache_clean_expired():
@@ -277,6 +289,7 @@ def test_cache_clean_expired():
277289
assert cache.number_of_items == 1
278290
assert cache.get("key1") is CACHE_MISS
279291
assert cache.get("key2") == "value2"
292+
cache.close()
280293

281294

282295
def test_cache_close():
@@ -321,6 +334,7 @@ def test_cache_default_serializer_pickle():
321334
assert isinstance(retrieved, dict)
322335
retrieved_dict = cast(dict[str, Any], retrieved)
323336
assert retrieved_dict["level1"]["level2"]["level3"]["list"][3]["nested"] == "dict"
337+
cache.close()
324338

325339

326340
def test_cache_multiple_operations():
@@ -345,6 +359,7 @@ def test_cache_multiple_operations():
345359
# Verify unchanged values
346360
for i in range(25, 50):
347361
assert cache.get(f"key{i}") == f"value{i}"
362+
cache.close()
348363

349364

350365
def test_cache_empty_values():
@@ -364,6 +379,7 @@ def test_cache_empty_values():
364379

365380
cache.set("false", False)
366381
assert cache.get("false") is False
382+
cache.close()
367383

368384

369385
def test_cache_default_ttl_sentinel():
@@ -382,6 +398,7 @@ def test_cache_default_ttl_sentinel():
382398

383399
time.sleep(0.2)
384400
assert cache.get("key2") == "value2" # Should still be there
401+
cache.close()
385402

386403

387404
def test_cache_concurrent_get_set_delete_clear():

tests/test_storage.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def test_storage_expiration_manual():
147147
checked, deleted = storage._clean_expired()
148148
assert checked == 81
149149
assert deleted == 80
150+
storage.close()
150151

151152

152153
def test_storage_expiration_thread():
@@ -191,6 +192,7 @@ def test_storage_too_many_items():
191192
if i % 10 == 0:
192193
# These keys should be still in the storage (thanks to the LRU behavior)
193194
assert storage.get(f"key{i}") == f"v{i}".encode()
195+
storage.close()
194196

195197

196198
def test_storage_various():
@@ -210,6 +212,7 @@ def test_storage_various():
210212

211213
# Get an expired key should return CACHE_MISS
212214
assert storage.get("key1") is CACHE_MISS
215+
storage.close()
213216

214217

215218
def test_storage_delete_return_value():
@@ -254,6 +257,7 @@ def test_max_size_limit():
254257
assert storage.number_of_items > 10
255258
assert storage.size_in_bytes > 3500
256259
assert storage.size_in_bytes < 4096
260+
storage.close()
257261

258262

259263
def test_overwrite_existing_key_size_tracking():
@@ -491,6 +495,12 @@ def test_storage_invalid_parameters():
491495
with pytest.raises(ValueError, match="expiration_thread_delay must be positive"):
492496
Storage[bytes](expiration_thread_delay=-1.0)
493497

498+
with pytest.raises(
499+
ValueError,
500+
match="expiration_thread_max_checks_per_iteration cannot be negative",
501+
):
502+
Storage[bytes](expiration_thread_max_checks_per_iteration=-1)
503+
494504

495505
def test_overwrite_lru_item_size_tracking():
496506
"""Size tracking stays correct when set() overwrites a key that gets evicted as the LRU item.

0 commit comments

Comments
 (0)