Skip to content

Commit

Permalink
added test_multi_sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Shershen committed Oct 17, 2024
1 parent 7aa6dec commit d95a2b0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import asyncio

import pytest
from sqlalchemy import text
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from starlette.middleware.base import BaseHTTPMiddleware
Expand Down Expand Up @@ -148,3 +151,24 @@ async def test_db_context_session_args(app, db, SQLAlchemyMiddleware, commit_on_
session_args = {"expire_on_commit": False}
async with db(session_args=session_args):
db.session


@pytest.mark.asyncio
async def test_multi_sessions(app, db, SQLAlchemyMiddleware):
app.add_middleware(SQLAlchemyMiddleware, db_url=db_url)

async with db(multi_sessions=True):
async def execute_query(query):
return await db.session.execute(text(query))

tasks = [
asyncio.create_task(execute_query("SELECT 1")),
asyncio.create_task(execute_query("SELECT 2")),
asyncio.create_task(execute_query("SELECT 3")),
asyncio.create_task(execute_query("SELECT 4")),
asyncio.create_task(execute_query("SELECT 5")),
asyncio.create_task(execute_query("SELECT 6")),
]

res = await asyncio.gather(*tasks)
assert len(res) == 6

0 comments on commit d95a2b0

Please sign in to comment.