diff --git a/app/tiny.py b/app/tiny.py index 5467691..9a75946 100644 --- a/app/tiny.py +++ b/app/tiny.py @@ -873,6 +873,9 @@ class TinyOrders: failed reading leaves the order without one: it must still go.""" try: seller = (self.request('GET', f'/contatos/{contact_id}').get('vendedor') or {}).get('id') + except TinyRateLimit: + # Busy for a moment: the whole delivery is tried again, the seller with it. + raise except TinyError as exc: logging.warning('Tiny: could not read the seller of contact %s: %s', contact_id, exc) return None diff --git a/tests/test_tiny.py b/tests/test_tiny.py index cb420bf..fcddc5e 100644 --- a/tests/test_tiny.py +++ b/tests/test_tiny.py @@ -77,7 +77,7 @@ class FakeTinyCase(unittest.TestCase): if request.method == 'GET' and path.startswith('/contatos/'): wanted = int(path.rsplit('/', 1)[-1]) if self.refuse_contact: - return httpx.Response(503, text='indisponível') + return httpx.Response(429 if self.refuse_contact == 429 else 503, text='indisponível') return httpx.Response(200, json=next(c for c in self.contacts if c['id'] == wanted)) if request.method == 'POST' and path == '/contatos': contact = {**json.loads(request.content), 'id': 500 + len(self.contacts)} @@ -273,6 +273,14 @@ class TinyTests(FakeTinyCase): self.assertEqual(self.tiny.deliver('k1', PAID)['status'], 'created') self.assertNotIn('vendedor', self.orders[0]) + def test_a_busy_tiny_retries_the_delivery_instead_of_dropping_the_seller(self): + from app.tiny import TinyRateLimit + self.contacts.append({'id': 321, 'cpfCnpj': '11.222.333/0001-81', 'vendedor': {'id': 977231477}}) + self.refuse_contact = 429 + with self.assertRaises(TinyRateLimit): + self.tiny.deliver('k1', PAID) + self.assertEqual(self.orders, []) + def test_a_contact_typed_without_punctuation_is_found_too(self): self.contacts.append({'id': 322, 'cpfCnpj': '11222333000181'}) self.tiny.deliver('k1', PAID)