Merge 503 during a database restart (DTF-API-8, 9)
All checks were successful
Build and deploy / Validate source (push) Successful in 1m22s
Build and deploy / Integration suite on a real stack (push) Successful in 7m6s
Build and deploy / Secret scan and release gate (push) Successful in 9s
Build and deploy / Publish images (push) Successful in 1m44s

This commit is contained in:
Cauê Faleiros
2026-11-16 09:46:37 -03:00
6 changed files with 64 additions and 8 deletions

View File

@@ -46,13 +46,14 @@ class WorkerDatabaseOutageTest(unittest.TestCase):
mock.patch.object(worker.mail, 'send_pending'), \
mock.patch.object(worker.time, 'sleep',
side_effect=[None, None, None, self.StopLoop]) as sleep, \
mock.patch.object(worker.logging, 'error') as error, \
mock.patch.object(worker.logging, 'warning') as warning, \
mock.patch.object(worker.logging, 'info') as info, \
mock.patch.object(worker.logging, 'debug') as debug:
with self.assertRaises(self.StopLoop):
worker.loop()
self.assertEqual(error.call_count, 1)
self.assertIn('Database unavailable', error.call_args[0][0])
# A warning, not an error: a restart is expected (Sentry DTF-API-8).
self.assertEqual(warning.call_count, 1)
self.assertIn('Database unavailable', warning.call_args[0][0])
self.assertEqual(info.call_count, 1)
self.assertIn('Database back after', info.call_args[0][0])
self.assertEqual(debug.call_count, 2)
@@ -76,8 +77,32 @@ class AuditPersistenceOutageTest(unittest.TestCase):
auth.audit('first')
auth.audit('second')
auth.audit('third')
self.assertEqual(error.call_count, 2)
self.assertEqual(warning.call_count, 3)
# A connection outage: the event lines and, once a minute, a warning.
self.assertEqual(error.call_count, 0)
self.assertEqual(warning.call_count, 5)
def test_any_other_persistence_failure_is_an_error(self):
logger = auth.logging.getLogger('dtf.security')
with mock.patch.object(auth, 'connect', side_effect=RuntimeError('bad table')), \
mock.patch.object(auth.time, 'monotonic', return_value=100.0), \
mock.patch.object(logger, 'error') as error, \
mock.patch.object(logger, 'warning'):
auth.audit('first')
self.assertEqual(error.call_count, 1)
class RequestDuringOutageTest(unittest.TestCase):
def test_a_request_gets_503_with_retry_after(self):
"""Sentry DTF-API-9: the board answered 500 while the database restarted."""
import asyncio
from app import app as module
request = mock.Mock(method='GET', url=mock.Mock(path='/api/operator/board'))
with mock.patch.object(module.logging, 'warning') as warning:
response = asyncio.run(module.database_unavailable(request, psycopg.OperationalError('down')))
self.assertEqual(response.status_code, 503)
self.assertEqual(response.headers['retry-after'], '5')
self.assertIn(b'reiniciando', response.body)
self.assertEqual(warning.call_count, 1)
if __name__ == '__main__':