|
| 1 | +import asyncio |
1 | 2 | from types import SimpleNamespace |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from sqlalchemy.exc import OperationalError |
4 | 6 | from sqlmodel import select |
5 | 7 |
|
6 | 8 | from astrbot.core.agent.response import AgentStats |
@@ -63,3 +65,143 @@ async def test_record_internal_agent_stats_persists_provider_stat( |
63 | 65 | assert record.start_time == 100.0 |
64 | 66 | assert record.end_time == 108.5 |
65 | 67 | assert record.time_to_first_token == 0.6 |
| 68 | + |
| 69 | + |
| 70 | +def _provider_stats_recording_args(): |
| 71 | + event = SimpleNamespace(unified_msg_origin="webchat:FriendMessage:session-42") |
| 72 | + req = ProviderRequest(conversation=SimpleNamespace(cid="conv-123")) |
| 73 | + provider = SimpleNamespace( |
| 74 | + provider_config={"id": "provider-1"}, |
| 75 | + meta=lambda: SimpleNamespace(id="provider-1", type="openai"), |
| 76 | + get_model=lambda: "gpt-4.1", |
| 77 | + ) |
| 78 | + agent_runner = SimpleNamespace( |
| 79 | + provider=provider, |
| 80 | + stats=AgentStats(), |
| 81 | + was_aborted=lambda: False, |
| 82 | + ) |
| 83 | + return event, req, agent_runner, SimpleNamespace(role="assistant") |
| 84 | + |
| 85 | + |
| 86 | +def _provider_stats_operational_error(message: str) -> OperationalError: |
| 87 | + return OperationalError("insert into provider_stats", {}, Exception(message)) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +@pytest.mark.parametrize( |
| 92 | + "lock_message", |
| 93 | + ["database is locked", "database table is locked"], |
| 94 | +) |
| 95 | +async def test_record_internal_agent_stats_retries_transient_database_locks( |
| 96 | + monkeypatch: pytest.MonkeyPatch, |
| 97 | + lock_message: str, |
| 98 | +): |
| 99 | + attempts = 0 |
| 100 | + |
| 101 | + class LockedOnceDb: |
| 102 | + async def insert_provider_stat(self, **kwargs): |
| 103 | + nonlocal attempts |
| 104 | + attempts += 1 |
| 105 | + if attempts == 1: |
| 106 | + raise _provider_stats_operational_error(lock_message) |
| 107 | + return SimpleNamespace(**kwargs) |
| 108 | + |
| 109 | + monkeypatch.setattr(internal, "db_helper", LockedOnceDb()) |
| 110 | + |
| 111 | + async def no_sleep(delay: float) -> None: |
| 112 | + return None |
| 113 | + |
| 114 | + monkeypatch.setattr(internal.asyncio, "sleep", no_sleep) |
| 115 | + |
| 116 | + await internal._record_internal_agent_stats( |
| 117 | + *_provider_stats_recording_args(), |
| 118 | + ) |
| 119 | + |
| 120 | + assert attempts == 2 |
| 121 | + |
| 122 | + |
| 123 | +@pytest.mark.asyncio |
| 124 | +async def test_record_internal_agent_stats_logs_after_exhausting_database_lock_retries( |
| 125 | + monkeypatch: pytest.MonkeyPatch, |
| 126 | +): |
| 127 | + attempts = 0 |
| 128 | + sleep_delays = [] |
| 129 | + warnings = [] |
| 130 | + |
| 131 | + class AlwaysLockedDb: |
| 132 | + async def insert_provider_stat(self, **kwargs): |
| 133 | + nonlocal attempts |
| 134 | + attempts += 1 |
| 135 | + raise _provider_stats_operational_error("database is locked") |
| 136 | + |
| 137 | + monkeypatch.setattr(internal, "db_helper", AlwaysLockedDb()) |
| 138 | + |
| 139 | + async def record_sleep(delay: float) -> None: |
| 140 | + sleep_delays.append(delay) |
| 141 | + |
| 142 | + monkeypatch.setattr(internal.asyncio, "sleep", record_sleep) |
| 143 | + monkeypatch.setattr( |
| 144 | + internal.logger, |
| 145 | + "warning", |
| 146 | + lambda *args, **kwargs: warnings.append((args, kwargs)), |
| 147 | + ) |
| 148 | + |
| 149 | + await internal._record_internal_agent_stats(*_provider_stats_recording_args()) |
| 150 | + |
| 151 | + assert attempts == internal.PROVIDER_STATS_SQLITE_LOCK_RETRY_ATTEMPTS |
| 152 | + base_delay = internal.PROVIDER_STATS_SQLITE_LOCK_RETRY_BASE_DELAY |
| 153 | + expected_sleep_delays = [ |
| 154 | + base_delay * (2**attempt) |
| 155 | + for attempt in range(internal.PROVIDER_STATS_SQLITE_LOCK_RETRY_ATTEMPTS - 1) |
| 156 | + ] |
| 157 | + assert sleep_delays == expected_sleep_delays |
| 158 | + assert len(warnings) == 1 |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.asyncio |
| 162 | +async def test_record_internal_agent_stats_does_not_retry_other_operational_errors( |
| 163 | + monkeypatch: pytest.MonkeyPatch, |
| 164 | +): |
| 165 | + attempts = 0 |
| 166 | + warnings = [] |
| 167 | + |
| 168 | + class FailingDb: |
| 169 | + async def insert_provider_stat(self, **kwargs): |
| 170 | + nonlocal attempts |
| 171 | + attempts += 1 |
| 172 | + raise _provider_stats_operational_error("no such table: provider_stats") |
| 173 | + |
| 174 | + monkeypatch.setattr(internal, "db_helper", FailingDb()) |
| 175 | + monkeypatch.setattr( |
| 176 | + internal.logger, |
| 177 | + "warning", |
| 178 | + lambda *args, **kwargs: warnings.append((args, kwargs)), |
| 179 | + ) |
| 180 | + |
| 181 | + await internal._record_internal_agent_stats(*_provider_stats_recording_args()) |
| 182 | + |
| 183 | + assert attempts == 1 |
| 184 | + assert len(warnings) == 1 |
| 185 | + |
| 186 | + |
| 187 | +@pytest.mark.asyncio |
| 188 | +async def test_record_internal_agent_stats_propagates_cancelled_error( |
| 189 | + monkeypatch: pytest.MonkeyPatch, |
| 190 | +): |
| 191 | + warnings = [] |
| 192 | + |
| 193 | + class CancellingDb: |
| 194 | + async def insert_provider_stat(self, **kwargs): |
| 195 | + raise asyncio.CancelledError |
| 196 | + |
| 197 | + monkeypatch.setattr(internal, "db_helper", CancellingDb()) |
| 198 | + monkeypatch.setattr( |
| 199 | + internal.logger, |
| 200 | + "warning", |
| 201 | + lambda *args, **kwargs: warnings.append((args, kwargs)), |
| 202 | + ) |
| 203 | + |
| 204 | + with pytest.raises(asyncio.CancelledError): |
| 205 | + await internal._record_internal_agent_stats(*_provider_stats_recording_args()) |
| 206 | + |
| 207 | + assert warnings == [] |
0 commit comments