-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_connection_pool.py
More file actions
640 lines (495 loc) · 21.9 KB
/
test_connection_pool.py
File metadata and controls
640 lines (495 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
"""
Comprehensive tests for ConnectionPool.
This test suite covers:
- Pool initialization and configuration
- Connection checkout and return
- Thread safety
- Health checks and connection aging
- Pool size limits and timeout handling
- Statistics tracking
- Error handling and cleanup
"""
import sqlite3
import threading
import time
from unittest.mock import patch
import pytest
from myspellchecker.core.config.validation_configs import ConnectionPoolConfig
from myspellchecker.providers.connection_pool import ConnectionPool, PooledConnection
@pytest.fixture
def test_db(tmp_path):
"""Create a temporary test database."""
database_path = tmp_path / "test.db"
conn = sqlite3.connect(str(database_path))
cursor = conn.cursor()
cursor.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")
cursor.execute("INSERT INTO test (value) VALUES ('test')")
conn.commit()
conn.close()
return database_path
class TestPooledConnection:
"""Tests for PooledConnection dataclass."""
def test_initialization(self):
"""Test PooledConnection initialization."""
conn = sqlite3.connect(":memory:")
pooled = PooledConnection(connection=conn)
assert pooled.connection == conn
assert pooled.use_count == 0
assert isinstance(pooled.created_at, float)
assert isinstance(pooled.last_used, float)
# Timestamps should be very close (within 1ms)
assert abs(pooled.created_at - pooled.last_used) < 0.001
def test_mark_used(self):
"""Test marking connection as used updates metadata."""
conn = sqlite3.connect(":memory:")
pooled = PooledConnection(connection=conn)
initial_last_used = pooled.last_used
initial_use_count = pooled.use_count
# Patch time.time where it's used (in connection_pool module) to advance the clock
with patch("myspellchecker.providers.connection_pool.time") as mock_time:
mock_time.time.return_value = initial_last_used + 1.0
pooled.mark_used()
assert pooled.use_count == initial_use_count + 1
assert pooled.last_used > initial_last_used
def test_multiple_uses(self):
"""Test connection can be marked as used multiple times."""
conn = sqlite3.connect(":memory:")
pooled = PooledConnection(connection=conn)
base_time = pooled.last_used
for i in range(5):
# Advance the clock deterministically instead of sleeping
with patch("myspellchecker.providers.connection_pool.time") as mock_time:
mock_time.time.return_value = base_time + (i + 1) * 1.0
pooled.mark_used()
assert pooled.use_count == i + 1
class TestConnectionPoolInitialization:
"""Tests for ConnectionPool initialization."""
def test_basic_initialization(self, test_db):
"""Test basic pool initialization."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
assert pool.database_path == test_db
assert pool.min_size == 2
assert pool.max_size == 5
assert pool.timeout == 5.0 # default
assert pool.max_connection_age == 3600.0 # default
assert not pool.check_same_thread
pool.close_all()
def test_custom_configuration(self, test_db):
"""Test pool with custom configuration."""
config = ConnectionPoolConfig(
min_size=3,
max_size=10,
timeout=10.0,
max_connection_age=7200.0,
check_same_thread=True,
)
pool = ConnectionPool(test_db, pool_config=config)
assert pool.min_size == 3
assert pool.max_size == 10
assert pool.timeout == 10.0
assert pool.max_connection_age == 7200.0
assert pool.check_same_thread
pool.close_all()
def test_invalid_min_size(self, test_db):
"""Test initialization fails with invalid min_size (via Pydantic)."""
with pytest.raises(ValueError):
ConnectionPoolConfig(min_size=-1, max_size=5)
def test_invalid_max_size(self, test_db):
"""Test initialization fails with invalid max_size (via Pydantic)."""
with pytest.raises(ValueError):
ConnectionPoolConfig(min_size=0, max_size=0)
def test_min_size_exceeds_max_size(self, test_db):
"""Test initialization fails when min_size > max_size (via Pydantic)."""
with pytest.raises(ValueError, match="max_size.*must be >= min_size"):
ConnectionPoolConfig(min_size=10, max_size=5)
def test_database_not_found(self, tmp_path):
"""Test initialization fails with non-existent database."""
non_existent = tmp_path / "nonexistent.db"
config = ConnectionPoolConfig(min_size=2, max_size=5)
with pytest.raises(FileNotFoundError, match="Database file not found"):
ConnectionPool(non_existent, pool_config=config)
def test_initial_connections_created(self, test_db):
"""Test min_size connections are created on initialization."""
config = ConnectionPoolConfig(min_size=3, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
stats = pool.get_stats()
assert stats["pool_size"] == 5 # max_size, not active_connections
assert stats["active_connections"] == 3 # min_size connections created
assert stats["available_connections"] == 3
pool.close_all()
class TestConnectionCheckout:
"""Tests for connection checkout and return."""
def test_basic_checkout(self, test_db):
"""Test basic connection checkout and return."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
with pool.checkout() as conn:
assert conn is not None
assert isinstance(conn, sqlite3.Connection)
# Connection should work
cursor = conn.cursor()
cursor.execute("SELECT 1")
result = cursor.fetchone()
assert result[0] == 1
pool.close_all()
def test_connection_returned_to_pool(self, test_db):
"""Test connection is returned to pool after use."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
initial_available = pool.get_stats()["available_connections"]
with pool.checkout():
# During checkout, available should decrease
stats_during = pool.get_stats()
assert stats_during["available_connections"] == initial_available - 1
# After return, available should be back to initial
stats_after = pool.get_stats()
assert stats_after["available_connections"] == initial_available
pool.close_all()
def test_multiple_checkouts(self, test_db):
"""Test multiple sequential checkouts."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
for i in range(10):
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT ?", (i,))
result = cursor.fetchone()
assert result[0] == i
pool.close_all()
def test_checkout_exception_returns_connection(self, test_db):
"""Test connection is returned even if exception occurs."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
initial_available = pool.get_stats()["available_connections"]
try:
with pool.checkout():
raise ValueError("Test exception")
except ValueError:
pass
# Connection should still be returned
stats = pool.get_stats()
assert stats["available_connections"] == initial_available
pool.close_all()
def test_checkout_updates_statistics(self, test_db):
"""Test checkout updates pool statistics."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
initial_checkouts = pool.get_stats()["total_checkouts"]
with pool.checkout():
pass
stats = pool.get_stats()
assert stats["total_checkouts"] == initial_checkouts + 1
assert stats["average_wait_time_ms"] >= 0
pool.close_all()
class TestPoolSizeLimits:
"""Tests for pool size limits and expansion."""
def test_pool_expands_when_needed(self, test_db):
"""Test pool creates new connections up to max_size."""
config = ConnectionPoolConfig(min_size=1, max_size=3)
pool = ConnectionPool(test_db, pool_config=config)
connections = []
try:
# Checkout all connections
for _ in range(3):
connections.append(pool.checkout())
connections[-1].__enter__()
stats = pool.get_stats()
assert stats["pool_size"] == 3 # max_size
assert stats["active_connections"] == 3
assert stats["available_connections"] == 0
finally:
# Return all connections
for ctx in connections:
ctx.__exit__(None, None, None)
pool.close_all()
def test_timeout_when_pool_exhausted(self, test_db):
"""Test timeout error when pool is exhausted."""
config = ConnectionPoolConfig(min_size=1, max_size=2, timeout=0.5)
pool = ConnectionPool(test_db, pool_config=config)
connections = []
try:
# Exhaust the pool
for _ in range(2):
connections.append(pool.checkout())
connections[-1].__enter__()
# Try to get another connection - should timeout
with pytest.raises(TimeoutError, match="Connection pool exhausted"):
with pool.checkout():
pass
finally:
for ctx in connections:
ctx.__exit__(None, None, None)
pool.close_all()
def test_peak_active_tracking(self, test_db):
"""Test peak active connections tracking."""
config = ConnectionPoolConfig(min_size=1, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
# Checkout 3 connections
connections = []
try:
for _ in range(3):
connections.append(pool.checkout())
connections[-1].__enter__()
stats = pool.get_stats()
assert stats["peak_active"] >= 3
finally:
for ctx in connections:
ctx.__exit__(None, None, None)
pool.close_all()
class TestHealthChecks:
"""Tests for connection health checks."""
def test_healthy_connection_reused(self, test_db):
"""Test healthy connections are reused."""
config = ConnectionPoolConfig(min_size=1, max_size=2)
pool = ConnectionPool(test_db, pool_config=config)
# First checkout
with pool.checkout() as conn1:
cursor = conn1.cursor()
cursor.execute("SELECT 1")
# Second checkout should get the same connection
with pool.checkout() as conn2:
cursor = conn2.cursor()
cursor.execute("SELECT 2")
pool.close_all()
def test_unhealthy_connection_recreated(self, test_db):
"""Test unhealthy connections are recreated."""
config = ConnectionPoolConfig(min_size=1, max_size=2)
pool = ConnectionPool(test_db, pool_config=config)
# Checkout and corrupt connection
with pool.checkout() as conn:
pass
# Manually close a connection to make it unhealthy
# Then check it's recreated on next checkout
pooled_conn = pool._pool.get()
pooled_conn.connection.close()
pool._pool.put(pooled_conn)
# Next checkout should recreate the connection
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT 1")
result = cursor.fetchone()
assert result[0] == 1
pool.close_all()
def test_aged_connection_recreated(self, test_db):
"""Test connections exceeding max age are recreated."""
config = ConnectionPoolConfig(min_size=1, max_size=2, max_connection_age=0.1)
pool = ConnectionPool(test_db, pool_config=config)
# Checkout connection to exercise it
with pool.checkout() as conn:
pass
# Instead of sleeping, artificially age the connection by backdating created_at.
# This makes _should_recreate_connection() see the connection as expired
# without relying on wall-clock time.
pooled_conn = pool._pool.get()
pooled_conn.created_at = pooled_conn.created_at - 1.0 # 1s ago, well past 0.1s max age
pool._pool.put(pooled_conn)
# Next checkout should recreate aged connection
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT 1")
result = cursor.fetchone()
assert result[0] == 1
pool.close_all()
class TestThreadSafety:
"""Tests for thread safety."""
def test_concurrent_checkouts(self, test_db):
"""Test pool handles concurrent checkouts from multiple threads."""
config = ConnectionPoolConfig(min_size=2, max_size=10)
pool = ConnectionPool(test_db, pool_config=config)
results = []
errors = []
def worker(worker_id):
try:
for i in range(5):
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT ?", (worker_id * 100 + i,))
result = cursor.fetchone()
results.append(result[0])
time.sleep(0.001) # Simulate work
except Exception as e:
errors.append(e)
# Create 5 threads
threads = []
for i in range(5):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()
# Wait for all threads
for t in threads:
t.join()
# Verify no errors
assert len(errors) == 0
assert len(results) == 25 # 5 threads * 5 iterations
pool.close_all()
def test_concurrent_statistics_tracking(self, test_db):
"""Test statistics are correctly tracked with concurrent access."""
config = ConnectionPoolConfig(min_size=2, max_size=10)
pool = ConnectionPool(test_db, pool_config=config)
def worker():
for _ in range(10):
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT 1")
time.sleep(0.001)
threads = []
for _ in range(5):
t = threading.Thread(target=worker)
threads.append(t)
t.start()
for t in threads:
t.join()
stats = pool.get_stats()
assert stats["total_checkouts"] == 50 # 5 threads * 10 iterations
assert stats["peak_active"] >= 2
pool.close_all()
class TestStatistics:
"""Tests for pool statistics."""
def test_get_stats_structure(self, test_db):
"""Test get_stats returns correct structure."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
stats = pool.get_stats()
assert "pool_size" in stats
assert "active_connections" in stats
assert "available_connections" in stats
assert "total_checkouts" in stats
assert "average_wait_time_ms" in stats
assert "peak_active" in stats
assert "min_size" in stats
assert "max_size" in stats
pool.close_all()
def test_statistics_accuracy(self, test_db):
"""Test statistics are accurate."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
# Initial stats
stats = pool.get_stats()
assert stats["pool_size"] == 5 # max_size
assert stats["active_connections"] == 2
assert stats["available_connections"] == 2
assert stats["total_checkouts"] == 0
assert stats["min_size"] == 2
assert stats["max_size"] == 5
# After one checkout
with pool.checkout():
pass
stats = pool.get_stats()
assert stats["total_checkouts"] == 1
assert stats["average_wait_time_ms"] >= 0
pool.close_all()
class TestCleanup:
"""Tests for pool cleanup and resource management."""
def test_close_all(self, test_db):
"""Test close_all closes all connections."""
config = ConnectionPoolConfig(min_size=3, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
initial_stats = pool.get_stats()
assert initial_stats["pool_size"] == 5 # max_size
assert initial_stats["active_connections"] == 3
pool.close_all()
final_stats = pool.get_stats()
assert final_stats["pool_size"] == 5 # max_size unchanged after close
assert final_stats["active_connections"] == 0
assert final_stats["available_connections"] == 0
def test_context_manager(self, test_db):
"""Test pool works as context manager."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
with ConnectionPool(test_db, pool_config=config) as pool:
stats = pool.get_stats()
assert stats["pool_size"] == 5 # max_size
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT 1")
# After exiting context, pool should be closed
# (we can't easily test this without accessing internals)
def test_destructor_cleanup(self, test_db):
"""Test destructor closes connections."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
id(pool)
# Delete pool
del pool
# Pool should be garbage collected and connections closed
# (difficult to test directly without accessing internals)
class TestErrorHandling:
"""Tests for error handling."""
def test_connection_creation_failure_during_init(self, tmp_path):
"""Test graceful handling of connection failures during initialization."""
# Create a directory instead of a file to cause connection failure
database_path = tmp_path / "test_dir"
database_path.mkdir()
# Pool should handle this gracefully - no exception raised, but 0 connections created
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(database_path, pool_config=config)
# Pool created, but with 0 active connections due to initialization failures
stats = pool.get_stats()
assert stats["pool_size"] == 5 # max_size unchanged
assert stats["active_connections"] == 0
assert stats["available_connections"] == 0
def test_pool_put_failure(self, test_db):
"""Test handling of errors when returning connection to pool."""
config = ConnectionPoolConfig(min_size=1, max_size=2)
pool = ConnectionPool(test_db, pool_config=config)
with pool.checkout() as conn:
# Connection should work normally
cursor = conn.cursor()
cursor.execute("SELECT 1")
# Pool should handle put errors gracefully
# (hard to test without mocking)
pool.close_all()
class TestIntegration:
"""Integration tests with actual SQLite operations."""
def test_real_world_usage_pattern(self, test_db):
"""Test realistic usage pattern with queries."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
# Insert data
with pool.checkout() as conn:
cursor = conn.cursor()
for i in range(10):
cursor.execute("INSERT INTO test (value) VALUES (?)", (f"value_{i}",))
conn.commit()
# Read data
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM test")
count = cursor.fetchone()[0]
assert count == 11 # 1 initial + 10 inserted
# Update data
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("UPDATE test SET value = 'updated' WHERE id = 1")
conn.commit()
# Verify update
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT value FROM test WHERE id = 1")
value = cursor.fetchone()[0]
assert value == "updated"
pool.close_all()
def test_transaction_isolation(self, test_db):
"""Test each connection maintains proper transaction isolation."""
config = ConnectionPoolConfig(min_size=2, max_size=5)
pool = ConnectionPool(test_db, pool_config=config)
# Start transaction in first connection
with pool.checkout() as conn1:
cursor1 = conn1.cursor()
cursor1.execute("INSERT INTO test (value) VALUES ('tx1')")
# Don't commit yet
# Second connection should not see uncommitted data
with pool.checkout() as conn2:
cursor2 = conn2.cursor()
cursor2.execute("SELECT COUNT(*) FROM test WHERE value = 'tx1'")
count = cursor2.fetchone()[0]
assert count == 0 # Transaction not committed
# Commit in first connection
conn1.commit()
# Now second connection should see the data
with pool.checkout() as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM test WHERE value = 'tx1'")
count = cursor.fetchone()[0]
assert count == 1
pool.close_all()