Skip to content

Commit

Permalink
Test retry of failed initial db connection (#1269)
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
c4ffein committed Jul 16, 2020
1 parent cf373a9 commit 76cca00
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Empty file added newsfragments/1269.empty.rst
Empty file.
1 change: 1 addition & 0 deletions parsec/backend/postgresql/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ async def _run_connections(self, task_status=trio.TASK_STATUS_IGNORED):
except OSError:
postgres_initial_connect_failed = True
if self.first_tries_number == 1:
logger.error("initial db connection failed", tries_remaining=0)
raise
if self.first_tries_number > 1:
self.first_tries_number -= 1
Expand Down
59 changes: 59 additions & 0 deletions tests/backend/test_postgres_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Parsec Cloud (https://parsec.cloud) Copyright (c) AGPLv3 2019 Scille SAS

import pytest
import trio


def records_filter_debug(records):
return [record for record in records if record.levelname != "DEBUG"]


@pytest.mark.trio
@pytest.mark.postgresql
async def test_postgresql_connection_ok(postgresql_url, backend_factory, blockstore):
async with backend_factory(config={"db_url": postgresql_url}):
pass


@pytest.mark.trio
@pytest.mark.postgresql
async def test_postgresql_connection_not_ok(
postgresql_url, backend_factory, caplog, unused_tcp_port
):
postgresql_url = f"postgresql://localhost:{unused_tcp_port}/dummy"
with pytest.raises(OSError) as exc:
async with backend_factory(config={"db_url": postgresql_url}):
pass
assert "[Errno 111] Connect call failed" in str(exc.value)
records = records_filter_debug(caplog.records)
assert len(records) == 1
assert records[0].levelname == "ERROR"
assert "initial db connection failed" in records[0].message


@pytest.mark.trio
@pytest.mark.postgresql
async def test_postgresql_connection_not_ok_retrying(
postgresql_url, backend_factory, caplog, unused_tcp_port, autojump_clock
):
tries_number = 4
tries_sleep = 3
postgresql_url = f"postgresql://localhost:{unused_tcp_port}/dummy"
with pytest.raises(OSError) as exc:
with trio.fail_after((tries_number - 1) * tries_sleep + 1):
async with backend_factory(
config={
"db_url": postgresql_url,
"db_first_tries_number": tries_number,
"db_first_tries_sleep": tries_sleep,
}
):
pass
assert "[Errno 111] Connect call failed" in str(exc.value)
records = records_filter_debug(caplog.records)
assert len(records) == 4
for record in records[:3]:
assert record.levelname == "WARNING"
assert "initial db connection failed" in record.message
assert records[3].levelname == "ERROR"
assert "initial db connection failed" in records[3].message

0 comments on commit 76cca00

Please sign in to comment.