Four traps, four checks
builds on: Happy-path code: the edge cases it skipped, Text, time and money: classic data traps, Fine on test data, broken at scale, Unawaited async work and floating promises, Leaked resources: files, connections, goroutines, Races introduced by plausible concurrent code, Retrying operations that aren't idempotent, Half-finished changes: the call sites it missed, Refactors that quietly change behavior, Invented APIs, flags, config keys and packages
Summary: *'Refunds retried on timeout, tested with 3 orders.'*
- `Order.objects.get` in the loop runs one query per order. Read the query log with 5,000 orders. Later cost: big batches crawl and time out.
- `* 0.9` brings in a float. A float `total` drifts by cents. A `Decimal` one raises `TypeError`. Test with a real amount and use `Decimal('0.9')`.
- A retry after a timeout refunds twice if the first call went through. Test that case, then send an idempotency key. Later cost: double payouts.
- After 3 timeouts the amount is still appended to `refunded`. Test with every call timing out. Later cost: a failed refund is reported as done.
// Python
def refund_all(order_ids):
refunded = []
for oid in order_ids:
order = Order.objects.get(id=oid)
amt = order.total * 0.9
for _ in range(3):
try:
gateway.refund(oid, amt)
break
except TimeoutError:
continue
refunded.append(amt)
return refunded