fix(api): coalesce consumer readback DB queries
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Successful in 1m20s
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Successful in 1m20s
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import pytest
|
||||
@@ -63,6 +64,8 @@ class _FailingContext:
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _stub_telegram_alert_card_readback(monkeypatch):
|
||||
consumer_module._reset_consumer_readback_cache_for_tests()
|
||||
|
||||
async def fake_alert_card_readback(**_kwargs):
|
||||
return _telegram_alert_card_readback()
|
||||
|
||||
@@ -71,6 +74,8 @@ def _stub_telegram_alert_card_readback(monkeypatch):
|
||||
"list_ai_alert_card_delivery_readback",
|
||||
fake_alert_card_readback,
|
||||
)
|
||||
yield
|
||||
consumer_module._reset_consumer_readback_cache_for_tests()
|
||||
|
||||
|
||||
def _ledger_rows() -> list[dict]:
|
||||
@@ -215,6 +220,69 @@ async def test_log_controlled_writeback_consumer_loader_reads_live_ledger(monkey
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_log_controlled_writeback_consumer_loader_reuses_fresh_success(
|
||||
monkeypatch,
|
||||
):
|
||||
fake_db = _FakeDb(_ledger_rows(), _consumer_receipt_rows())
|
||||
monkeypatch.setattr(
|
||||
consumer_module,
|
||||
"get_db_context",
|
||||
lambda project_id: _FakeContext(fake_db),
|
||||
)
|
||||
|
||||
first = await load_latest_ai_agent_log_controlled_writeback_consumer_readback()
|
||||
second = await load_latest_ai_agent_log_controlled_writeback_consumer_readback()
|
||||
|
||||
assert first["readback_freshness"]["cache_hit"] is False
|
||||
assert second["readback_freshness"]["cache_hit"] is True
|
||||
assert second["readback_freshness"]["stale"] is False
|
||||
assert second["readback_freshness"]["cache_age_seconds"] >= 0
|
||||
assert fake_db.params == [
|
||||
{"operation_type": "log_controlled_writeback_dispatched"},
|
||||
{"operation_type": "log_controlled_writeback_consumed"},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_log_controlled_writeback_consumer_loader_coalesces_concurrent_reads(
|
||||
monkeypatch,
|
||||
):
|
||||
started = asyncio.Event()
|
||||
release = asyncio.Event()
|
||||
call_count = 0
|
||||
|
||||
async def delayed_rows(*, project_id: str):
|
||||
nonlocal call_count
|
||||
assert project_id == "awoooi"
|
||||
call_count += 1
|
||||
started.set()
|
||||
await release.wait()
|
||||
return _ledger_rows(), _consumer_receipt_rows()
|
||||
|
||||
monkeypatch.setattr(
|
||||
consumer_module,
|
||||
"_load_consumer_rows_with_retry",
|
||||
delayed_rows,
|
||||
)
|
||||
|
||||
first_task = asyncio.create_task(
|
||||
load_latest_ai_agent_log_controlled_writeback_consumer_readback()
|
||||
)
|
||||
await started.wait()
|
||||
second_task = asyncio.create_task(
|
||||
load_latest_ai_agent_log_controlled_writeback_consumer_readback()
|
||||
)
|
||||
await asyncio.sleep(0)
|
||||
release.set()
|
||||
first, second = await asyncio.gather(first_task, second_task)
|
||||
|
||||
assert call_count == 1
|
||||
assert first["readback_freshness"]["inflight_coalesced"] is False
|
||||
assert second["readback_freshness"]["inflight_coalesced"] is True
|
||||
assert first["consumer_bindings"] == second["consumer_bindings"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_log_controlled_writeback_consumer_loader_reads_apply_receipts(monkeypatch):
|
||||
fake_db = _FakeDb(_ledger_rows(), _consumer_receipt_rows())
|
||||
@@ -270,6 +338,39 @@ async def test_log_controlled_writeback_consumer_loader_degrades_on_db_pressure(
|
||||
assert payload["operation_boundaries"]["runtime_target_write_performed"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_log_controlled_writeback_consumer_loader_does_not_cache_db_fallback(
|
||||
monkeypatch,
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
consumer_module,
|
||||
"get_db_context",
|
||||
lambda project_id: _FailingContext(),
|
||||
)
|
||||
direct_attempt_count = 0
|
||||
|
||||
async def fake_direct_unavailable(*, project_id: str):
|
||||
nonlocal direct_attempt_count
|
||||
assert project_id == "awoooi"
|
||||
direct_attempt_count += 1
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
consumer_module,
|
||||
"_load_consumer_rows_direct",
|
||||
fake_direct_unavailable,
|
||||
)
|
||||
|
||||
first = await load_latest_ai_agent_log_controlled_writeback_consumer_readback()
|
||||
second = await load_latest_ai_agent_log_controlled_writeback_consumer_readback()
|
||||
|
||||
assert direct_attempt_count == 2
|
||||
assert first["readback_freshness"]["cache_hit"] is False
|
||||
assert second["readback_freshness"]["cache_hit"] is False
|
||||
assert first["readback"]["db_read_status"] == "unavailable"
|
||||
assert second["readback"]["db_read_status"] == "unavailable"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_log_controlled_writeback_consumer_loader_uses_direct_fallback(
|
||||
monkeypatch,
|
||||
|
||||
Reference in New Issue
Block a user