|
10 | 10 | import logging |
11 | 11 | from typing import Any |
12 | 12 |
|
13 | | -from teslemetry_stream.vehicle import TeslemetryStreamVehicle |
| 13 | +from teslemetry_stream.vehicle import LOGGER, TeslemetryStreamVehicle |
14 | 14 |
|
15 | 15 | VIN_A = "TESTVIN0000000001" |
16 | 16 | VIN_B = "TESTVIN0000000002" |
@@ -67,6 +67,17 @@ def emit(self, record: logging.LogRecord) -> None: |
67 | 67 | self.messages.append(record.getMessage()) |
68 | 68 |
|
69 | 69 |
|
| 70 | +class CaptureErrors(logging.Handler): |
| 71 | + """Collect formatted ERROR records emitted by the library.""" |
| 72 | + |
| 73 | + def __init__(self) -> None: |
| 74 | + super().__init__(level=logging.ERROR) |
| 75 | + self.messages: list[str] = [] |
| 76 | + |
| 77 | + def emit(self, record: logging.LogRecord) -> None: |
| 78 | + self.messages.append(record.getMessage()) |
| 79 | + |
| 80 | + |
70 | 81 | def check(label: str, ok: bool, detail: str = "") -> bool: |
71 | 82 | print(f"{label:<56} {'PASS' if ok else 'FAIL'}{' ' + detail if detail else ''}") |
72 | 83 | return ok |
@@ -191,6 +202,102 @@ async def main() -> None: |
191 | 202 | ) |
192 | 203 | ) |
193 | 204 |
|
| 205 | + # A streaming allowance refusal logs the description and drops the batch. |
| 206 | + over = { |
| 207 | + "response": None, |
| 208 | + "error": "streaming_allowance_exceeded", |
| 209 | + "error_description": "This streaming configuration uses 120% of your allowance", |
| 210 | + } |
| 211 | + vehicle = make_vehicle(VIN_A, [over]) |
| 212 | + captured = CaptureErrors() |
| 213 | + LOGGER.addHandler(captured) |
| 214 | + try: |
| 215 | + await vehicle.update_config({"fields": {"BatteryLevel": None}}) |
| 216 | + finally: |
| 217 | + LOGGER.removeHandler(captured) |
| 218 | + results.append( |
| 219 | + check( |
| 220 | + "allowance refusal logs the error description", |
| 221 | + any("120% of your allowance" in m for m in captured.messages) |
| 222 | + and not any("streaming_allowance_exceeded" in m for m in captured.messages), |
| 223 | + f"logged {captured.messages}", |
| 224 | + ) |
| 225 | + ) |
| 226 | + results.append( |
| 227 | + check( |
| 228 | + "allowance refusal clears the pending config", |
| 229 | + vehicle._config == {} and vehicle.fields == {}, |
| 230 | + f"pending {vehicle._config}, fields {sorted(vehicle.fields)}", |
| 231 | + ) |
| 232 | + ) |
| 233 | + |
| 234 | + # The config-refused listener gets (error, description) once; a removed |
| 235 | + # listener is not called; a raising listener does not block the others. |
| 236 | + vehicle = make_vehicle(VIN_A, [over, FAILED, over]) |
| 237 | + calls: list[tuple[str, str | None]] = [] |
| 238 | + later: list[tuple[str, str | None]] = [] |
| 239 | + |
| 240 | + def boom(error: str, description: str | None) -> None: |
| 241 | + raise RuntimeError("listener bug") |
| 242 | + |
| 243 | + vehicle.listen_config_refused(boom) |
| 244 | + vehicle.listen_config_refused(lambda e, d: calls.append((e, d))) |
| 245 | + remove_later = vehicle.listen_config_refused(lambda e, d: later.append((e, d))) |
| 246 | + await vehicle.update_config({"fields": {"BatteryLevel": None}}) |
| 247 | + results.append( |
| 248 | + check( |
| 249 | + "config-refused listener called once with error and description", |
| 250 | + calls == [(over["error"], over["error_description"])] |
| 251 | + and later == calls, |
| 252 | + f"calls {calls}", |
| 253 | + ) |
| 254 | + ) |
| 255 | + remove_later() |
| 256 | + await vehicle.update_config({"fields": {"BatteryLevel": None}}) |
| 257 | + results.append( |
| 258 | + check( |
| 259 | + "removed listener not called; other errors reach listeners", |
| 260 | + len(later) == 1 and calls[-1] == ("upstream internal error", None), |
| 261 | + f"calls {calls}, later {later}", |
| 262 | + ) |
| 263 | + ) |
| 264 | + vehicle = make_vehicle(VIN_A, [ACCEPTED]) |
| 265 | + seen: list[Any] = [] |
| 266 | + vehicle.listen_config_refused(lambda e, d: seen.append(e)) |
| 267 | + await vehicle.update_config({"fields": {"BatteryLevel": None}}) |
| 268 | + results.append(check("success does not call the listener", seen == [])) |
| 269 | + |
| 270 | + # The same callback registered twice has independent, idempotent removers. |
| 271 | + vehicle = make_vehicle(VIN_A, [over, over]) |
| 272 | + twice: list[str] = [] |
| 273 | + |
| 274 | + def same(error: str, description: str | None) -> None: |
| 275 | + twice.append(error) |
| 276 | + |
| 277 | + remove_first = vehicle.listen_config_refused(same) |
| 278 | + vehicle.listen_config_refused(same) |
| 279 | + remove_first() |
| 280 | + remove_first() |
| 281 | + await vehicle.update_config({"fields": {"BatteryLevel": None}}) |
| 282 | + results.append( |
| 283 | + check( |
| 284 | + "repeated remove only removes its own registration", |
| 285 | + len(twice) == 1, |
| 286 | + f"calls {twice}", |
| 287 | + ) |
| 288 | + ) |
| 289 | + |
| 290 | + # Any other error keeps the pending config for the next explicit trigger. |
| 291 | + vehicle = make_vehicle(VIN_A, [FAILED]) |
| 292 | + await vehicle.update_config({"fields": {"BatteryLevel": None}}) |
| 293 | + results.append( |
| 294 | + check( |
| 295 | + "other errors keep the pending config", |
| 296 | + vehicle._config == {"fields": {"BatteryLevel": None}}, |
| 297 | + f"pending {vehicle._config}", |
| 298 | + ) |
| 299 | + ) |
| 300 | + |
194 | 301 | # Pending config must not be shared between vehicles. |
195 | 302 | first = make_vehicle(VIN_A, [FAILED]) |
196 | 303 | second = make_vehicle(VIN_B, [ACCEPTED]) |
|
0 commit comments