I have added both connect and statement timeouts for a postgres database in my django service. So the relevant django setting looks like;
_CONN_TIMEOUT = 5
_STATEMENT_TIMEOUT = 3000 # millisecond
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "some_database",
# ...,
"OPTIONS": {
"connect_timeout": _CONN_TIMEOUT,
"options": "-c statement_timeout={0}ms".format(_STATEMENT_TIMEOUT),
},
}
}
And then, I'm writing tests like this;
class DbTimeoutTest(TestCase):
def test_db_statement_timeout(self):
"""
test carrying out an SQL query that takes longer than the configured
postgres `statement_timeout` value
"""
# set statement_timeout to 1 millisecond
mock_s_timeout = 1
with self.settings(_CONN_TIMEOUT=5, _STATEMENT_TIMEOUT=mock_s_timeout):
self.assertEqual(
settings.DATABASES["default"]["OPTIONS"]["options"],
"-c statement_timeout={0}ms".format(mock_s_timeout),
)
Book.objects.create(name="Hello")
However, that test is not working.
- The assert
self.assertEqualdoes not pass, meaning that the setting override did not work. - I would expect the
Book.objects.createstatement to fail with timeout but it does not.
So questions;
- How do I test for postgres statement_timeout?(been able to also test for connect timeout would also be a plus)
- How do I catch, the statement timeout error in my code?
python manage.py runserver --http_timeout=1800