|
1 | 1 | import json |
2 | 2 | from pathlib import Path |
| 3 | +from types import TracebackType |
| 4 | +from typing import Self |
3 | 5 |
|
4 | 6 | import pytest |
5 | 7 |
|
6 | 8 | from scripts.e2e_home_assistant import ( |
7 | 9 | addon_options, |
8 | 10 | assetlink_sites, |
| 11 | + wait_for_http, |
9 | 12 | write_json, |
10 | 13 | ) |
11 | 14 |
|
12 | 15 |
|
| 16 | +class SuccessfulResponse: |
| 17 | + """Minimal typed context manager matching the urllib response boundary.""" |
| 18 | + |
| 19 | + status = 200 |
| 20 | + |
| 21 | + def __enter__(self) -> Self: |
| 22 | + return self |
| 23 | + |
| 24 | + def __exit__( |
| 25 | + self, |
| 26 | + exception_type: type[BaseException] | None, |
| 27 | + exception: BaseException | None, |
| 28 | + traceback: TracebackType | None, |
| 29 | + ) -> None: |
| 30 | + return None |
| 31 | + |
| 32 | + def read(self) -> bytes: |
| 33 | + return b"ready" |
| 34 | + |
| 35 | + |
13 | 36 | def test_addon_options_constructs_independent_serializable_values() -> None: |
14 | 37 | sites = ["https://ha.example.com"] |
15 | 38 |
|
@@ -49,3 +72,23 @@ def test_write_json_replaces_document_deterministically(tmp_path: Path) -> None: |
49 | 72 |
|
50 | 73 | assert destination.read_text(encoding="utf-8") == ('{\n "a": true,\n "z": 1\n}\n') |
51 | 74 | assert not destination.with_suffix(".json.tmp").exists() |
| 75 | + |
| 76 | + |
| 77 | +def test_wait_for_http_retries_connection_reset( |
| 78 | + monkeypatch: pytest.MonkeyPatch, |
| 79 | +) -> None: |
| 80 | + attempts = 0 |
| 81 | + |
| 82 | + def open_with_startup_reset(url: str, *, timeout: float) -> SuccessfulResponse: |
| 83 | + nonlocal attempts |
| 84 | + del url, timeout |
| 85 | + attempts += 1 |
| 86 | + if attempts == 1: |
| 87 | + raise ConnectionResetError("service is still starting") |
| 88 | + return SuccessfulResponse() |
| 89 | + |
| 90 | + monkeypatch.setattr("urllib.request.urlopen", open_with_startup_reset) |
| 91 | + monkeypatch.setattr("time.sleep", lambda _seconds: None) |
| 92 | + |
| 93 | + assert wait_for_http("http://home-assistant.test", timeout=1) == b"ready" |
| 94 | + assert attempts == 2 |
0 commit comments