-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtest_query_session_pool.py
More file actions
233 lines (189 loc) · 8.2 KB
/
Copy pathtest_query_session_pool.py
File metadata and controls
233 lines (189 loc) · 8.2 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
import asyncio
import json
import pytest
import ydb
from typing import Optional
from ydb import QueryExplainResultFormat
from ydb.aio.query.pool import QuerySessionPool
from ydb.aio.query.session import QuerySession
from ydb.aio.query.transaction import QueryTxContext
class TestQuerySessionPool:
@pytest.mark.asyncio
async def test_checkout_provides_created_session(self, pool: QuerySessionPool):
async with pool.checkout() as session:
assert session.is_active
@pytest.mark.asyncio
async def test_oneshot_query_normal(self, pool: QuerySessionPool):
res = await pool.execute_with_retries("select 1;")
assert len(res) == 1
@pytest.mark.asyncio
async def test_oneshot_query_merges_large_result_set_parts(self, pool: QuerySessionPool):
row_count = 100_000
query = f"SELECT * FROM AS_TABLE(ListMap(ListFromRange(0ul, {row_count}ul), ($x) -> (<|id: $x|>)))"
res = await pool.execute_with_retries(query)
assert len(res) == 1
assert res[0].index == 0
assert len(res[0].rows) == row_count
@pytest.mark.asyncio
async def test_oneshot_ddl_query(self, pool: QuerySessionPool):
await pool.execute_with_retries("drop table if exists Queen;")
await pool.execute_with_retries("create table Queen(key UInt64, PRIMARY KEY (key));")
await pool.execute_with_retries("drop table Queen;")
@pytest.mark.asyncio
async def test_oneshot_query_raises(self, pool: QuerySessionPool):
with pytest.raises(ydb.GenericError):
await pool.execute_with_retries("Is this the real life? Is this just fantasy?")
@pytest.mark.asyncio
async def test_retry_op_uses_created_session(self, pool: QuerySessionPool):
async def callee(session: QuerySession):
assert session.is_active
await pool.retry_operation_async(callee)
@pytest.mark.asyncio
async def test_retry_op_normal(self, pool: QuerySessionPool):
async def callee(session: QuerySession):
async with session.transaction() as tx:
iterator = await tx.execute("select 1;", commit_tx=True)
return [result_set async for result_set in iterator]
res = await pool.retry_operation_async(callee)
assert len(res) == 1
@pytest.mark.asyncio
async def test_retry_op_raises(self, pool: QuerySessionPool):
class CustomException(Exception):
pass
async def callee(session: QuerySession):
raise CustomException()
with pytest.raises(CustomException):
await pool.retry_operation_async(callee)
@pytest.mark.parametrize(
"tx_mode",
[
(None),
(ydb.QuerySerializableReadWrite()),
(ydb.QuerySnapshotReadOnly()),
(ydb.QuerySnapshotReadWrite()),
(ydb.QueryOnlineReadOnly()),
(ydb.QueryStaleReadOnly()),
],
)
@pytest.mark.asyncio
async def test_retry_tx_normal(self, pool: QuerySessionPool, tx_mode: Optional[ydb.BaseQueryTxMode]):
retry_no = 0
async def callee(tx: QueryTxContext):
nonlocal retry_no
if retry_no < 2:
retry_no += 1
raise ydb.Unavailable("Fake fast backoff error")
result_stream = await tx.execute("SELECT 1")
return [result_set async for result_set in result_stream]
result = await pool.retry_tx_async(callee=callee, tx_mode=tx_mode)
assert len(result) == 1
assert retry_no == 2
@pytest.mark.asyncio
async def test_retry_tx_raises(self, pool: QuerySessionPool):
class CustomException(Exception):
pass
async def callee(tx: QueryTxContext):
raise CustomException()
with pytest.raises(CustomException):
await pool.retry_tx_async(callee)
@pytest.mark.asyncio
async def test_pool_size_limit_logic(self, pool: QuerySessionPool):
target_size = 5
pool._size = target_size
ids = set()
for i in range(1, target_size + 1):
session = await pool.acquire()
assert pool._current_size == i
assert session.session_id not in ids
ids.add(session.session_id)
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(pool.acquire(), timeout=0.1)
last_id = session.session_id
await pool.release(session)
session = await pool.acquire()
assert session.session_id == last_id
assert pool._current_size == target_size
@pytest.mark.asyncio
async def test_checkout_do_not_increase_size(self, pool: QuerySessionPool):
session_id = None
for _ in range(10):
async with pool.checkout() as session:
if session_id is None:
session_id = session.session_id
assert pool._current_size == 1
assert session_id == session.session_id
@pytest.mark.asyncio
async def test_pool_recreates_bad_sessions(self, pool: QuerySessionPool):
async with pool.checkout() as session:
session_id = session.session_id
await session.delete()
async with pool.checkout() as session:
assert session_id != session.session_id
assert pool._current_size == 1
@pytest.mark.asyncio
async def test_acquire_from_closed_pool_raises(self, pool: QuerySessionPool):
await pool.stop()
with pytest.raises(RuntimeError):
await pool.acquire()
@pytest.mark.asyncio
async def test_acquire_with_timeout_from_closed_pool_raises(self, pool: QuerySessionPool):
await pool.stop()
with pytest.raises(RuntimeError):
await asyncio.wait_for(pool.acquire(), timeout=0.1)
@pytest.mark.asyncio
async def test_no_session_leak(self, driver, docker_project):
pool = ydb.aio.QuerySessionPool(driver, 1)
docker_project.stop()
try:
await asyncio.wait_for(pool.acquire(), timeout=0.1)
except ydb.Error:
pass
assert pool._current_size == 0
docker_project.start()
await pool.stop()
@pytest.mark.asyncio
async def test_acquire_no_race_condition(self, driver):
ids = set()
async with ydb.aio.QuerySessionPool(driver, 1) as pool:
async def acquire_session():
session = await pool.acquire()
ids.add(session.session_id)
await pool.release(session)
tasks = [acquire_session() for _ in range(10)]
await asyncio.gather(*tasks)
assert len(ids) == 1
assert pool._current_size == 1
@pytest.mark.asyncio
async def test_released_after_future_canceled(self, driver):
async with ydb.aio.QuerySessionPool(driver, 1) as pool:
s = await pool.acquire()
waiter = asyncio.ensure_future(pool.acquire())
await asyncio.sleep(0) # let waiter run until waiting point
waiter.cancel()
await pool.release(s)
await asyncio.wait([waiter])
assert pool._queue.qsize() == 1
@pytest.mark.asyncio
async def test_explain_with_retries(self, pool: QuerySessionPool):
await pool.execute_with_retries("DROP TABLE IF EXISTS test_explain")
await pool.execute_with_retries("CREATE TABLE test_explain (id Int64, PRIMARY KEY (id))")
try:
plan = await pool.explain_with_retries(
"SELECT * FROM test_explain", result_format=QueryExplainResultFormat.STR
)
isinstance(plan, str)
assert "FullScan" in plan
plan = await pool.explain_with_retries(
"SELECT * FROM test_explain", result_format=QueryExplainResultFormat.DICT
)
plan_string = json.dumps(plan)
assert "FullScan" in plan_string
plan = await pool.explain_with_retries(
"SELECT * FROM test_explain WHERE id = $id",
{"$id": 1},
result_format=QueryExplainResultFormat.DICT,
)
plan_string = json.dumps(plan)
assert "Lookup" in plan_string
finally:
await pool.execute_with_retries("DROP TABLE test_explain")