Skip to content

Commit bf088ec

Browse files
authored
Merge pull request #281 raise exception if aquire session from stopped pool
2 parents 5135bca + 4216727 commit bf088ec

File tree

6 files changed

+76
-10
lines changed

6 files changed

+76
-10
lines changed

tests/aio/test_session_pool.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ async def test_close_basic_logic_case_1(driver):
6262
waiter = asyncio.ensure_future(pool.acquire())
6363

6464
await pool.stop()
65-
waiter_sess = waiter.result()
66-
assert not waiter_sess.initialized()
67-
after_stop = await pool.acquire()
68-
assert not after_stop.initialized()
65+
66+
with pytest.raises(ValueError):
67+
waiter.result()
68+
69+
with pytest.raises(ValueError):
70+
await pool.acquire()
6971

7072
await pool.release(s)
71-
await pool.release(after_stop)
72-
await pool.release(waiter_sess)
7373
assert pool._active_count == 0
7474

7575

@@ -106,9 +106,9 @@ async def test_close_basic_logic_case_2(driver):
106106

107107
assert pool._active_count == 0
108108

109-
sess = await pool.acquire()
109+
with pytest.raises(ValueError):
110+
await pool.acquire()
110111

111-
assert not sess.initialized()
112112
await pool.stop()
113113

114114

tests/aio/test_table.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
import ydb.aio
3+
4+
5+
@pytest.mark.asyncio
6+
class TestSessionPool:
7+
async def test_checkout_from_stopped_pool(self, driver):
8+
pool = ydb.aio.SessionPool(driver, 1)
9+
await pool.stop()
10+
11+
with pytest.raises(ValueError):
12+
await pool.acquire()

tests/session_pool.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
3+
import ydb
4+
5+
6+
def test_close_basic_logic_case_1(driver_sync):
7+
pool = ydb.SessionPool(driver_sync, 1)
8+
s = pool.acquire()
9+
10+
pool.stop()
11+
12+
with pytest.raises(ValueError):
13+
pool.acquire()
14+
15+
pool.release(s)
16+
assert pool._pool_impl._active_count == 0
17+
18+
19+
def test_close_basic_logic_case_2(driver_sync):
20+
pool = ydb.SessionPool(driver_sync, 10)
21+
acquired = []
22+
23+
for _ in range(10):
24+
acquired.append(pool.acquire())
25+
26+
for _ in range(3):
27+
pool.release(acquired.pop(-1))
28+
29+
pool.stop()
30+
assert pool._pool_impl._active_count == 7
31+
32+
while acquired:
33+
pool.release(acquired.pop(-1))
34+
35+
assert pool._pool_impl._active_count == 0
36+
37+
with pytest.raises(ValueError):
38+
pool.acquire()
39+
40+
pool.stop()

tests/table/table_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import pytest
12
import ydb
23

34

5+
class TestSessionPool:
6+
def test_checkout_from_stopped_pool(self, driver_sync):
7+
pool = ydb.SessionPool(driver_sync, 1)
8+
pool.stop()
9+
10+
with pytest.raises(ValueError):
11+
pool.acquire()
12+
13+
414
class TestTable:
515
def test_create_table_with_not_null_primary_key_by_api(self, driver_sync, database):
616
table_path = database + "/test_table"

ydb/_sp_impl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ def _on_keep_alive(self, session, f):
335335
self._destroy(session, "keep-alive-error")
336336

337337
def acquire(self, blocking=True, timeout=None):
338+
if self._should_stop.is_set():
339+
self._logger.error("Take session from closed session pool")
340+
raise ValueError("Take session from closed session pool.")
341+
338342
waiter = self.subscribe()
339343
has_result = False
340344
if blocking:

ydb/aio/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ async def _get_session_from_queue(self, timeout: float):
341341
async def acquire(self, timeout: float = None, retry_timeout: float = None, retry_num: int = None) -> ydb.ISession:
342342

343343
if self._should_stop.is_set():
344-
self._logger.debug("Acquired not inited session")
345-
return self._create()
344+
self._logger.error("Take session from closed session pool")
345+
raise ValueError("Take session from closed session pool.")
346346

347347
if retry_timeout is None:
348348
retry_timeout = timeout

0 commit comments

Comments
 (0)