-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wlwlwlzhang
committed
Sep 6, 2023
1 parent
89ce48d
commit ff44a2f
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import os | ||
import threading | ||
import unittest | ||
from datetime import datetime | ||
from collections import OrderedDict | ||
|
@@ -598,5 +599,43 @@ def test_iter(self): | |
assert c == len(self.tbl) | ||
|
||
|
||
class DatabasePoolTestCase(unittest.TestCase): | ||
def test_pool(self): | ||
target_num = 30 | ||
table_name = "test_pool" | ||
|
||
def insert_data(): | ||
with db as tx: | ||
tx[table_name].insert(dict(name='John Doe', age=46, country='China')) | ||
for _ in range(10): | ||
engine_kwargs = {"echo": False, "pool_size": target_num, "max_overflow": 0, "connect_args": {'connect_timeout': 2}} | ||
config_str = 'postgresql://postgres:[email protected]:5432/postgres' | ||
db = None | ||
try: | ||
db = connect(config_str, engine_kwargs=engine_kwargs) | ||
with db as tx: | ||
if table_name in tx: | ||
tx[table_name].drop() | ||
tx[table_name].insert(dict(name='John Doe', age=46, country='China')) | ||
threads = [threading.Thread(target=insert_data) for _ in range(target_num)] | ||
[thread.start() for thread in threads] | ||
[thread.join() for thread in threads] | ||
t = db[table_name].count() | ||
with db as tx: | ||
if table_name in tx: | ||
tx[table_name].drop() | ||
assert t == target_num + 1, t | ||
except SQLAlchemyError as e: | ||
if "timeout expired" in str(e) or "Connection refused" in str(e): | ||
break | ||
else: | ||
raise | ||
except Exception: | ||
raise | ||
finally: | ||
if db: | ||
db.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |