Fix postgres_status before first connection attempt

It's possible to call postgres_status before the first connection is
established. Before this fix, an exception was raised due to
`self._postgres_connected` being `None`.

```
  File "/home/ar/code/github/sprockets/sprockets-postgres/sprockets_postgres.py", line 411, in postgres_status
    if not self._postgres_connected.is_set():
AttributeError: 'NoneType' object has no attribute 'is_set'
```
This commit is contained in:
Andrew Rabert 2021-01-20 16:01:13 -05:00
parent 16b12cf470
commit afb6ea99a5
2 changed files with 14 additions and 1 deletions

View file

@ -408,7 +408,8 @@ class ApplicationMixin:
} }
""" """
if not self._postgres_connected.is_set(): if not self._postgres_connected or \
not self._postgres_connected.is_set():
return { return {
'available': False, 'available': False,
'pool_size': 0, 'pool_size': 0,

View file

@ -349,6 +349,18 @@ class TestCase(testing.SprocketsHttpTestCase):
return self.app return self.app
class PostgresStatusTestCase(asynctest.TestCase):
async def test_postgres_status_before_first_connection(self):
app = Application()
status = await app.postgres_status()
self.assertEqual(
status,
{'available': False,
'pool_size': 0,
'pool_free': 0})
class RequestHandlerMixinTestCase(TestCase): class RequestHandlerMixinTestCase(TestCase):
def test_postgres_status(self): def test_postgres_status(self):