Skip to content

Commit 56f7b24

Browse files
committed
query session pool tests
1 parent 8bb8a6f commit 56f7b24

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

tests/query/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from ydb.query.session import QuerySessionSync
3+
from ydb.query.pool import QuerySessionPool
34

45

56
@pytest.fixture
@@ -25,3 +26,9 @@ def tx(session):
2526
transaction.rollback()
2627
except BaseException:
2728
pass
29+
30+
31+
@pytest.fixture
32+
def pool(driver_sync):
33+
pool = QuerySessionPool(driver_sync)
34+
yield pool
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
3+
from ydb.query.pool import QuerySessionPool
4+
from ydb.query.session import QuerySessionSync, QuerySessionStateEnum
5+
6+
7+
class TestQuerySessionPool:
8+
def test_checkout_provides_created_session(self, pool: QuerySessionPool):
9+
with pool.checkout() as session:
10+
assert session._state._state == QuerySessionStateEnum.CREATED
11+
12+
assert session._state._state == QuerySessionStateEnum.CLOSED
13+
14+
def test_oneshot_query_normal(self, pool: QuerySessionPool):
15+
res = pool.execute_with_retries("select 1;")
16+
assert len(res) == 1
17+
18+
def test_oneshot_ddl_query(self, pool: QuerySessionPool):
19+
pool.execute_with_retries("create table Queen(key UInt64, PRIMARY KEY (key));")
20+
pool.execute_with_retries("drop table Queen;")
21+
22+
def test_oneshot_query_raises(self, pool: QuerySessionPool):
23+
with pytest.raises(Exception):
24+
pool.execute_with_retries("Is this the real life? Is this just fantasy?")
25+
26+
def test_retry_op_uses_created_session(self, pool: QuerySessionPool):
27+
def callee(session: QuerySessionSync):
28+
assert session._state._state == QuerySessionStateEnum.CREATED
29+
pool.retry_operation_sync(callee)
30+
31+
def test_retry_op_normal(self, pool: QuerySessionPool):
32+
def callee(session: QuerySessionSync):
33+
with session.transaction() as tx:
34+
iterator = tx.execute("select 1;", commit_tx=True)
35+
return [result_set for result_set in iterator]
36+
37+
res = pool.retry_operation_sync(callee)
38+
assert len(res) == 1
39+
40+
def test_retry_op_raises(self, pool: QuerySessionPool):
41+
def callee(session: QuerySessionSync):
42+
res = session.execute("Caught in a landslide, no escape from reality")
43+
for _ in res:
44+
pass
45+
46+
with pytest.raises(Exception):
47+
pool.retry_operation_sync(callee)

ydb/query/transaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def _move_to_commited(self):
316316
if self._tx_state._already_in(QueryTxStateEnum.COMMITTED):
317317
return
318318
self._tx_state._change_state(QueryTxStateEnum.COMMITTED)
319+
self._tx_state.tx_id = None
319320

320321
def execute(
321322
self,

0 commit comments

Comments
 (0)