|
1 | 1 | from concurrent.futures import ThreadPoolExecutor |
2 | 2 | from threading import get_ident |
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | from pytest_mock.plugin import MockerFixture |
5 | 6 |
|
6 | 7 | from sqlmesh.utils.connection_pool import ( |
@@ -323,3 +324,27 @@ def test_thread_local_shared_connection_pool_close(mocker: MockerFixture): |
323 | 324 | assert cursor_mock.close.call_count == 2 |
324 | 325 | assert connection_factory_mock.call_count == 2 |
325 | 326 | assert connection_mock.close.call_count == 2 |
| 327 | + |
| 328 | + |
| 329 | +@pytest.mark.parametrize( |
| 330 | + "pool_class", |
| 331 | + [SingletonConnectionPool, ThreadLocalConnectionPool, ThreadLocalSharedConnectionPool], |
| 332 | +) |
| 333 | +@pytest.mark.parametrize("method", ["commit", "rollback"]) |
| 334 | +def test_connection_pool_failed_commit_or_rollback_ends_transaction( |
| 335 | + mocker: MockerFixture, pool_class, method: str |
| 336 | +): |
| 337 | + cursor_mock = mocker.Mock() |
| 338 | + connection_mock = mocker.Mock() |
| 339 | + connection_mock.cursor.return_value = cursor_mock |
| 340 | + pool = pool_class(mocker.Mock(return_value=connection_mock)) |
| 341 | + |
| 342 | + pool.begin() |
| 343 | + assert pool.is_transaction_active |
| 344 | + |
| 345 | + getattr(cursor_mock, method).side_effect = RuntimeError("serializable isolation violation") |
| 346 | + with pytest.raises(RuntimeError): |
| 347 | + getattr(pool, method)() |
| 348 | + |
| 349 | + # a later transaction must start with a BEGIN, not be taken for part of the failed one |
| 350 | + assert not pool.is_transaction_active |
0 commit comments