Recover stale PostgreSQL connections before starting requests
This commit is contained in:
@@ -9,7 +9,7 @@ from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
from conftest import DATABASE, new_user
|
||||
from conftest import DATABASE, free_port, new_user
|
||||
from messenger.shared.infrastructure.readiness import DRAINING
|
||||
|
||||
DRAIN = 1.0
|
||||
@@ -67,7 +67,7 @@ def begin_registration(port: int) -> tuple[socket.socket, bytes]:
|
||||
|
||||
|
||||
def test_sigterm_leaves_rotation_then_closes_and_cancels_stuck_requests(tmp_path: Path) -> None:
|
||||
port = 18083
|
||||
port = free_port()
|
||||
settings = {
|
||||
"HOST": "127.0.0.1",
|
||||
"PORT": str(port),
|
||||
@@ -98,6 +98,7 @@ def test_sigterm_leaves_rotation_then_closes_and_cancels_stuck_requests(tmp_path
|
||||
time.sleep(0.3)
|
||||
during = status(port, "/health/ready"), status(port, "/health/live")
|
||||
while status(port, "/health/live") == 200:
|
||||
assert time.monotonic() - sent < SHUTDOWN + 2, log_path.read_text()
|
||||
time.sleep(0.05)
|
||||
closed = time.monotonic() - sent
|
||||
pending.sendall(rest)
|
||||
|
||||
@@ -7,11 +7,21 @@ import pytest
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.exc import DBAPIError, IntegrityError
|
||||
|
||||
from conftest import actor, keyed, make_chat, register, resolve, running_app
|
||||
from conftest import (
|
||||
actor,
|
||||
keyed,
|
||||
make_chat,
|
||||
new_user,
|
||||
register,
|
||||
resolve,
|
||||
running_app,
|
||||
sql_connection,
|
||||
)
|
||||
from messenger.identity.domain.models import Session
|
||||
from messenger.identity.infrastructure.postgres import (
|
||||
PostgresIdentityUnitOfWork,
|
||||
PostgresSessionDirectory,
|
||||
UserRow,
|
||||
)
|
||||
from messenger.messaging.domain.models import Message
|
||||
from messenger.messaging.domain.values import MessageText
|
||||
@@ -65,6 +75,28 @@ async def test_idle_session_does_not_acquire_connection() -> None:
|
||||
await database.ping()
|
||||
|
||||
|
||||
async def test_closed_idle_connection_is_replaced_before_registration() -> None:
|
||||
async with running_app(db_pool_max_size=1) as http:
|
||||
database = await resolve(http, Database)
|
||||
async with database.session() as session:
|
||||
pid = await session.scalar(select(func.pg_backend_pid()))
|
||||
async with sql_connection() as connection:
|
||||
assert await connection.scalar(select(func.pg_terminate_backend(pid, 1000)))
|
||||
body = new_user("reconnect")
|
||||
response = await http.post("/api/v1/users", json=body)
|
||||
assert response.status_code == 201
|
||||
assert response.headers["X-Instance-Id"] and response.headers["X-Request-Id"]
|
||||
async with database.session() as session:
|
||||
assert (
|
||||
await session.scalar(
|
||||
select(func.count())
|
||||
.select_from(UserRow)
|
||||
.where(UserRow.username == body["username"])
|
||||
)
|
||||
== 1
|
||||
)
|
||||
|
||||
|
||||
async def test_readiness_timeout_releases_waiter() -> None:
|
||||
async with running_app(db_pool_max_size=1, db_acquire_timeout_seconds=2) as http:
|
||||
database = await resolve(http, Database)
|
||||
|
||||
Reference in New Issue
Block a user