fix: retry transactions who fail due to deadlocks

In my opinion, this kind of handling of transactions is pretty ugly.
The being said, we have issues with running into deadlocks on aur.al,
so this commit works against that immediate bug.

An ideal solution would be to deal with retrying transactions through
the `db.begin()` scope, so we wouldn't have to explicitly annotate
functions as "retry functions," which is what this commit does.

Closes #376

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2022-09-13 12:47:52 -07:00
parent f450b5dfc7
commit ec3152014b
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
16 changed files with 241 additions and 82 deletions

View file

@ -5,6 +5,7 @@ import tempfile
from unittest import mock
import pytest
from sqlalchemy.exc import OperationalError
import aurweb.config
import aurweb.initdb
@ -226,3 +227,22 @@ def test_name_without_pytest_current_test():
with mock.patch.dict("os.environ", {}, clear=True):
dbname = aurweb.db.name()
assert dbname == aurweb.config.get("database", "name")
def test_retry_deadlock():
@db.retry_deadlock
def func():
raise OperationalError("Deadlock found", tuple(), "")
with pytest.raises(OperationalError):
func()
@pytest.mark.asyncio
async def test_async_retry_deadlock():
@db.async_retry_deadlock
async def func():
raise OperationalError("Deadlock found", tuple(), "")
with pytest.raises(OperationalError):
await func()