-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_retry.py
More file actions
38 lines (33 loc) · 1.13 KB
/
test_retry.py
File metadata and controls
38 lines (33 loc) · 1.13 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
import pytest
import sqlalchemy
from sqlalchemy.exc import DBAPIError
from sqlalchemy.ext import asyncio as sa_async
from db_retry.retry import postgres_retry
@pytest.mark.parametrize(
"error_code",
[
"08000", # PostgresConnectionError - backoff triggered
"08003", # subclass of PostgresConnectionError - backoff triggered
"40001", # SerializationError - backoff triggered
"40002", # StatementCompletionUnknownError - backoff not triggered
],
)
async def test_postgres_retry(async_engine: sa_async.AsyncEngine, error_code: str) -> None:
async with async_engine.connect() as connection:
await connection.execute(
sqlalchemy.text(
f"""
CREATE OR REPLACE FUNCTION raise_error()
RETURNS VOID AS $$
BEGIN
RAISE SQLSTATE '{error_code}';
END;
$$ LANGUAGE plpgsql;
""",
),
)
@postgres_retry
async def raise_error() -> None:
await connection.execute(sqlalchemy.text("SELECT raise_error()"))
with pytest.raises(DBAPIError):
await raise_error()