|
| 1 | +""" |
| 2 | +Concurrent access tests for DuckDB Python bindings with free threading support. |
| 3 | +
|
| 4 | +These tests verify that the DuckDB Python module can handle concurrent access |
| 5 | +from multiple threads safely, testing module state isolation, memory management, |
| 6 | +and connection handling under various stress conditions. |
| 7 | +""" |
| 8 | + |
| 9 | +import gc |
| 10 | +import random |
| 11 | +import time |
| 12 | +import concurrent.futures |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +import duckdb |
| 17 | + |
| 18 | + |
| 19 | +def test_concurrent_connections(): |
| 20 | + with duckdb.connect() as conn: |
| 21 | + result = conn.execute("SELECT random() as id, random()*2 as doubled").fetchone() |
| 22 | + assert result is not None |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parallel_threads(1) |
| 26 | +def test_shared_connection_stress(num_threads_testing): |
| 27 | + """Test concurrent operations on shared connection using cursors.""" |
| 28 | + iterations = 10 |
| 29 | + |
| 30 | + with duckdb.connect(":memory:") as connection: |
| 31 | + connection.execute( |
| 32 | + "CREATE TABLE stress_test (id INTEGER, thread_id INTEGER, value TEXT)" |
| 33 | + ) |
| 34 | + |
| 35 | + def worker_thread(thread_id: int) -> None: |
| 36 | + cursor = connection.cursor() |
| 37 | + for i in range(iterations): |
| 38 | + cursor.execute( |
| 39 | + "INSERT INTO stress_test VALUES (?, ?, ?)", |
| 40 | + [i, thread_id, f"thread_{thread_id}_value_{i}"], |
| 41 | + ) |
| 42 | + cursor.execute( |
| 43 | + "SELECT COUNT(*) FROM stress_test WHERE thread_id = ?", [thread_id] |
| 44 | + ).fetchone() |
| 45 | + time.sleep(random.uniform(0.0001, 0.001)) |
| 46 | + |
| 47 | + with concurrent.futures.ThreadPoolExecutor( |
| 48 | + max_workers=num_threads_testing |
| 49 | + ) as executor: |
| 50 | + futures = [ |
| 51 | + executor.submit(worker_thread, i) for i in range(num_threads_testing) |
| 52 | + ] |
| 53 | + # Wait for all to complete, will raise if any fail |
| 54 | + for future in concurrent.futures.as_completed(futures): |
| 55 | + future.result() |
| 56 | + |
| 57 | + total_rows = connection.execute("SELECT COUNT(*) FROM stress_test").fetchone()[ |
| 58 | + 0 |
| 59 | + ] |
| 60 | + expected_rows = num_threads_testing * iterations |
| 61 | + assert total_rows == expected_rows |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parallel_threads(1) |
| 65 | +def test_module_state_isolation(): |
| 66 | + """Test that module state is properly accessible.""" |
| 67 | + with duckdb.connect(":memory:"): |
| 68 | + assert hasattr(duckdb, "__version__") |
| 69 | + |
| 70 | + with duckdb.connect() as default_conn: |
| 71 | + result = default_conn.execute("SELECT 'default' as type").fetchone() |
| 72 | + assert result[0] == "default" |
| 73 | + |
| 74 | + int_type = duckdb.type("INTEGER") |
| 75 | + string_type = duckdb.type("VARCHAR") |
| 76 | + assert int_type is not None |
| 77 | + assert string_type is not None |
| 78 | + |
| 79 | + |
| 80 | +def test_rapid_connect_disconnect(): |
| 81 | + connections_count = 10 |
| 82 | + """Test rapid connection creation and destruction.""" |
| 83 | + for i in range(connections_count): |
| 84 | + conn = duckdb.connect(":memory:") |
| 85 | + try: |
| 86 | + result = conn.execute("SELECT 1").fetchone()[0] |
| 87 | + assert result == 1 |
| 88 | + finally: |
| 89 | + conn.close() |
| 90 | + |
| 91 | + # Sometimes force GC to increase pressure |
| 92 | + if i % 3 == 0: |
| 93 | + gc.collect() |
| 94 | + |
| 95 | + |
| 96 | +def test_exception_handling(): |
| 97 | + """Test exception handling doesn't affect module state.""" |
| 98 | + conn = duckdb.connect(":memory:") |
| 99 | + try: |
| 100 | + conn.execute("CREATE TABLE test (x INTEGER)") |
| 101 | + conn.execute("INSERT INTO test VALUES (1), (2), (3)") |
| 102 | + |
| 103 | + for i in range(10): |
| 104 | + if i % 3 == 0: |
| 105 | + with pytest.raises(duckdb.CatalogException): |
| 106 | + conn.execute("SELECT * FROM nonexistent_table") |
| 107 | + else: |
| 108 | + result = conn.execute("SELECT COUNT(*) FROM test").fetchone()[0] |
| 109 | + assert result == 3 |
| 110 | + finally: |
| 111 | + conn.close() |
0 commit comments