"""The outbox worker drains a backlog instead of one job per second (item 9). Run from the repository root: python3 -m unittest tests.test_worker_drain -v. The worker imports psycopg and boto3, so a host without the API image's dependencies cannot import it; CI runs this in the api container. """ import unittest from unittest import mock from app import worker class WorkerDrainTest(unittest.TestCase): def test_drain_stops_when_the_queue_is_empty(self): with mock.patch.object(worker, 'tick', side_effect=[True, True, True, False]) as tick: worker.drain() self.assertEqual(tick.call_count, 4) def test_drain_stops_at_the_limit(self): with mock.patch.object(worker, 'tick', return_value=True) as tick: worker.drain() self.assertEqual(tick.call_count, 50) class RefusedDeliveryTest(unittest.TestCase): """A delivery the provider refuses reaches Sentry (logging.error) on its first failure and every tenth after (#42, 2026-11-18).""" def tick_with(self, attempts, refusal=None): job = {'id': 1, 'provider': 'tiny', 'event_key': 'o:paid:tiny', 'payload': {}, 'attempts': attempts} conn = mock.MagicMock() conn.execute.return_value.fetchone.return_value = job connect = mock.MagicMock() connect.return_value.__enter__.return_value = conn refusing = mock.MagicMock() refusing.deliver.side_effect = refusal or RuntimeError('Forma de recebimento não está habilitada') with mock.patch.object(worker, 'connect', connect), \ mock.patch.dict(worker.adapters, {'tiny': refusing}), \ mock.patch.object(worker.logging, 'error') as error: self.assertTrue(worker.tick()) return error def test_first_and_every_tenth_failure_are_reported(self): self.assertEqual(self.tick_with(0).call_count, 1) self.assertEqual(self.tick_with(9).call_count, 1) self.assertEqual(self.tick_with(4).call_count, 0) def test_a_rate_limit_is_reported_only_when_it_repeats(self): from app.tiny import TinyRateLimit limit = TinyRateLimit('Tiny rate limit reached; the outbox will retry') self.assertEqual(self.tick_with(0, limit).call_count, 0) self.assertEqual(self.tick_with(9, limit).call_count, 1) class HeldTinyTest(unittest.TestCase): """Production without the real Tiny holds the sends and tells Sentry; the simulator marked real orders delivered with made-up receipts.""" def test_production_without_the_real_tiny_holds(self): self.assertIsInstance(worker.tiny_adapter('fake', 'production'), worker.HeldTiny) self.assertIsInstance(worker.tiny_adapter(None, 'production'), worker.HeldTiny) self.assertIsInstance(worker.tiny_adapter('fake', 'local'), worker.FakeTiny) def test_a_held_send_stays_undelivered_and_is_reported(self): job = {'id': 1, 'provider': 'tiny', 'event_key': 'o:paid:tiny', 'payload': {}, 'attempts': 0} conn = mock.MagicMock() conn.execute.return_value.fetchone.return_value = job connect = mock.MagicMock() connect.return_value.__enter__.return_value = conn with mock.patch.object(worker, 'connect', connect), \ mock.patch.dict(worker.adapters, {'tiny': worker.HeldTiny()}), \ mock.patch.object(worker.logging, 'error') as error: worker.tick() updates = [call.args[0] for call in conn.execute.call_args_list[1:]] self.assertTrue(updates and 'delivered_at=now()' not in updates[0] and 'last_error' in updates[0], updates) self.assertEqual(conn.execute.call_args_list[1].args[1][0], worker.HELD) self.assertEqual(error.call_count, 1) if __name__ == '__main__': unittest.main()