35 lines
867 B
Python
35 lines
867 B
Python
from psycopg2 import errors
|
|
|
|
from app.core.db import db_read_retry
|
|
|
|
|
|
def test_db_read_retry_retries_retryable_error_once() -> None:
|
|
attempts = {"count": 0}
|
|
|
|
@db_read_retry(attempts=3, initial_delay_seconds=0)
|
|
def flaky() -> str:
|
|
attempts["count"] += 1
|
|
if attempts["count"] == 1:
|
|
raise errors.DeadlockDetected()
|
|
return "ok"
|
|
|
|
assert flaky() == "ok"
|
|
assert attempts["count"] == 2
|
|
|
|
|
|
def test_db_read_retry_does_not_swallow_non_retryable_error() -> None:
|
|
attempts = {"count": 0}
|
|
|
|
@db_read_retry(attempts=3, initial_delay_seconds=0)
|
|
def broken() -> str:
|
|
attempts["count"] += 1
|
|
raise ValueError("boom")
|
|
|
|
try:
|
|
broken()
|
|
except ValueError as exc:
|
|
assert str(exc) == "boom"
|
|
else:
|
|
raise AssertionError("expected ValueError")
|
|
assert attempts["count"] == 1
|