Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test retry of failed initial db connection #1269

Merged
merged 7 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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