|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import threading |
| 17 | +import time |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import pyignite_dbapi |
| 22 | +from tests.conftest import TEST_CONNECT_KWARGS |
| 23 | +from tests.util import wait_for_condition |
| 24 | + |
| 25 | +NUM_THREADS = 50 |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture() |
| 29 | +def module_level_threadsafety(): |
| 30 | + assert pyignite_dbapi.threadsafety >= 1, "Module can not be used concurrently" |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture() |
| 34 | +def connection_level_threadsafety(module_level_threadsafety): |
| 35 | + assert pyignite_dbapi.threadsafety >= 2, "Connections can not be used concurrently" |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture() |
| 39 | +def table(table_name, service_cursor, drop_table_cleanup): |
| 40 | + service_cursor.execute(f"CREATE TABLE {table_name} (id int primary key, data varchar)") |
| 41 | + yield table_name |
| 42 | + |
| 43 | + |
| 44 | +def run_threads(fn, n=NUM_THREADS, *args): |
| 45 | + barrier = threading.Barrier(n) |
| 46 | + errors = [] |
| 47 | + errors_lock = threading.Lock() |
| 48 | + |
| 49 | + def wrapper(tid): |
| 50 | + try: |
| 51 | + barrier.wait() |
| 52 | + fn(tid, *args) |
| 53 | + except Exception as e: |
| 54 | + with errors_lock: |
| 55 | + errors.append(e) |
| 56 | + |
| 57 | + threads = [threading.Thread(target=wrapper, args=(i,)) for i in range(n)] |
| 58 | + for t in threads: |
| 59 | + t.start() |
| 60 | + for t in threads: |
| 61 | + t.join() |
| 62 | + |
| 63 | + if errors: |
| 64 | + raise errors[0] |
| 65 | + |
| 66 | + |
| 67 | +def test_concurrent_module_import(module_level_threadsafety): |
| 68 | + import importlib |
| 69 | + |
| 70 | + def task(_): |
| 71 | + m = importlib.import_module(pyignite_dbapi.__name__) |
| 72 | + assert m.threadsafety > 0, "Module can not be used concurrently" |
| 73 | + |
| 74 | + run_threads(task) |
| 75 | + |
| 76 | + |
| 77 | +def test_concurrent_connect_use_close(module_level_threadsafety): |
| 78 | + def task(_): |
| 79 | + c = pyignite_dbapi.connect(**TEST_CONNECT_KWARGS) |
| 80 | + with c.cursor() as cur: |
| 81 | + cur.execute("SELECT 1") |
| 82 | + assert cur.fetchone() is not None |
| 83 | + c.close() |
| 84 | + |
| 85 | + run_threads(task) |
| 86 | + |
| 87 | + |
| 88 | +def test_shared_connection_per_thread_cursors(connection, connection_level_threadsafety): |
| 89 | + def task(_): |
| 90 | + with connection.cursor() as cur: |
| 91 | + cur.execute("SELECT 1") |
| 92 | + row = cur.fetchone() |
| 93 | + assert row is not None |
| 94 | + |
| 95 | + run_threads(task) |
| 96 | + |
| 97 | + |
| 98 | +def test_concurrent_inserts_no_lost_writes(table, connection, connection_level_threadsafety): |
| 99 | + rows_per_thread = 50 |
| 100 | + |
| 101 | + def task(thread_id): |
| 102 | + with connection.cursor() as cur: |
| 103 | + for i in range(rows_per_thread): |
| 104 | + cur.execute(f"INSERT INTO {table} (id, data) VALUES (?, ?)", (thread_id * rows_per_thread + i, f"v{thread_id}-{i}")) |
| 105 | + |
| 106 | + run_threads(task) |
| 107 | + |
| 108 | + with connection.cursor() as cur: |
| 109 | + cur.execute(f"SELECT COUNT(*) FROM {table}") |
| 110 | + count = cur.fetchone()[0] |
| 111 | + assert count == NUM_THREADS * rows_per_thread |
| 112 | + |
| 113 | + |
| 114 | +def test_concurrent_commit_and_rollback(table, module_level_threadsafety): |
| 115 | + """Half the threads commit, half rollback. Only committed rows appear.""" |
| 116 | + committed_ids = [] |
| 117 | + lock = threading.Lock() |
| 118 | + |
| 119 | + def task(thread_id): |
| 120 | + with pyignite_dbapi.connect(**TEST_CONNECT_KWARGS) as conn: |
| 121 | + conn.autocommit = False |
| 122 | + with conn.cursor() as cur: |
| 123 | + cur.execute(f"INSERT INTO {table} (id, data) VALUES (?, ?)", (thread_id, "x")) |
| 124 | + if thread_id % 2 == 0: |
| 125 | + conn.commit() |
| 126 | + with lock: |
| 127 | + committed_ids.append(thread_id) |
| 128 | + else: |
| 129 | + conn.rollback() |
| 130 | + |
| 131 | + run_threads(task) |
| 132 | + |
| 133 | + def get_ids(): |
| 134 | + with pyignite_dbapi.connect(**TEST_CONNECT_KWARGS) as conn: |
| 135 | + with conn.cursor() as cur: |
| 136 | + cur.execute(f"SELECT id FROM {table} ORDER BY id") |
| 137 | + return {row[0] for row in cur.fetchall()} |
| 138 | + |
| 139 | + # There is currently no mechanism to synchronize the observable timestamp across |
| 140 | + # multiple connections, so changes will eventually become visible, but not necessarily immediately. |
| 141 | + wait_for_condition(lambda: get_ids() == set(committed_ids), interval=0.5) |
| 142 | + |
| 143 | + |
| 144 | +def test_concurrent_fetchall_result_integrity(table, connection, connection_level_threadsafety): |
| 145 | + rows_num = 200 |
| 146 | + with connection.cursor() as cur: |
| 147 | + cur.executemany(f"INSERT INTO {table} (id, data) VALUES (?, ?)", [(i, f"val-{i}") for i in range(rows_num)]) |
| 148 | + |
| 149 | + def task(_): |
| 150 | + with connection.cursor() as cur: |
| 151 | + cur.execute(f"SELECT id, data FROM {table} ORDER BY id") |
| 152 | + rows = cur.fetchall() |
| 153 | + assert len(rows) == rows_num, f"Expected {rows_num} rows, got {len(rows)}" |
| 154 | + |
| 155 | + for idx, (rid, val) in enumerate(rows): |
| 156 | + assert val == f"val-{rid}", f"Corrupted row: id={rid}, val={val!r}" |
| 157 | + |
| 158 | + run_threads(task) |
| 159 | + |
| 160 | + |
| 161 | +def test_cursor_description_thread_safety(table, connection, connection_level_threadsafety): |
| 162 | + expected_names = {"ID", "DATA"} |
| 163 | + |
| 164 | + def task(_): |
| 165 | + with connection.cursor() as cur: |
| 166 | + cur.execute(f"SELECT id, data FROM {table} LIMIT 1") |
| 167 | + desc = cur.description |
| 168 | + assert desc is not None |
| 169 | + col_names = {col[0] for col in desc} |
| 170 | + assert col_names == expected_names, f"Unexpected columns: {col_names}" |
| 171 | + |
| 172 | + run_threads(task) |
| 173 | + |
| 174 | + |
| 175 | +def test_concurrent_executemany(table, connection, connection_level_threadsafety): |
| 176 | + rows_per_thread = 20 |
| 177 | + |
| 178 | + def task(thread_id): |
| 179 | + rows = [(thread_id * 1000 + i, f"{thread_id}-{i}") for i in range(rows_per_thread)] |
| 180 | + with connection.cursor() as cur: |
| 181 | + cur.executemany(f"INSERT INTO {table} (id, data) VALUES (?, ?)", rows) |
| 182 | + |
| 183 | + run_threads(task) |
| 184 | + |
| 185 | + with connection.cursor() as cur: |
| 186 | + cur.execute(f"SELECT COUNT(*) FROM {table}") |
| 187 | + count = cur.fetchone()[0] |
| 188 | + |
| 189 | + assert count == NUM_THREADS * rows_per_thread |
0 commit comments