test(accounting): cover batch dependency failure and backpressure

This commit is contained in:
Joshua Valluru 2026-10-31 01:31:20 -07:00
parent ab8f9c0ddc
commit 80f4a31bc2
2 changed files with 30 additions and 4 deletions

View file

@ -1448,7 +1448,12 @@ async def test_batch_capture_retains_request_basis_for_later_tiered_pricing(
@pytest.mark.asyncio
async def test_large_batch_recovers_partial_commit_with_frozen_pricing_and_rejects_changed_usage() -> None:
from litellm.proxy.spend_tracking.usage_capture import PricingCapture, UsageBatchWriter, capture_batch_callback
from litellm.proxy.spend_tracking.usage_capture import (
PricingCapture,
UsageBatchWriter,
capture_batch_callback,
capture_callback,
)
database: Final = StoredUsage()
transactions: Final = asyncio.Queue[tuple[UsageWrite, ...]]()
@ -1485,6 +1490,27 @@ async def test_large_batch_recovers_partial_commit_with_frozen_pricing_and_rejec
"litellm_params": {},
"_accounting_pricing_snapshot": pricing,
}
failed_database: Final = CommitBarrier(failure=RuntimeError("Earlier capture failed"))
failed_database.release.set()
prior_meter: Final = DurableUsageMeter(failed_database.write, UsageCaptureSettings(), wait_for_batch=_manual_flush)
prior: Final = capture_callback(
prior_meter,
{"call_type": "acompletion", "litellm_call_id": "earlier", "litellm_params": {}},
{"id": "earlier"},
NOW,
NOW,
)
await prior_meter.close()
with pytest.raises(RuntimeError, match="Earlier capture failed"):
await capture_batch_callback(
meter,
database.load,
{**kwargs, "_accounting_prior_captures": (prior,)},
response,
NOW,
NOW,
)
assert database.rows == ()
with pytest.raises(RuntimeError, match="Interrupted batch persistence"):
await capture_batch_callback(meter, database.load, kwargs, response, NOW, NOW)
partial: Final = database.rows

View file

@ -395,17 +395,17 @@ async def test_large_write_sequence_drains_within_byte_and_record_bounds() -> No
database.release.set()
meter: Final = DurableUsageMeter(
database.write,
UsageCaptureSettings(batch_size=8, max_pending_records=4, max_pending_bytes=1024),
UsageCaptureSettings(batch_size=8, max_pending_records=2, max_pending_bytes=1024),
wait_for_batch=_manual_flush,
)
writes: Final = tuple(_write(str(index)) for index in range(20))
pending: Final = meter.submit((_write("pending"),))
pending: Final = meter.submit((_write("pending-a"), _write("pending-b")))
await meter.record_writes(writes)
await pending
with pytest.raises(UsageCaptureError, match="One usage observation"):
await meter.record_writes((_write("oversized" * 300),))
await meter.close()
assert database.committed == ("pending", *(write.event.event_id for write in writes))
assert database.committed == ("pending-a", "pending-b", *(write.event.event_id for write in writes))
assert database.transactions > 1
assert (meter.pending_bytes, meter.pending_records) == (0, 0)
with pytest.raises(UsageCaptureError):