|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | import asyncio |
| 9 | +import contextlib |
9 | 10 | import json |
10 | 11 | from typing import Any |
11 | 12 |
|
| 13 | +import aiohttp |
| 14 | + |
12 | 15 | from teslemetry_stream.stream import TeslemetryStream |
13 | 16 |
|
14 | 17 | VIN = "LRW3F7EK4NC000001" |
@@ -55,13 +58,16 @@ def close(self) -> None: |
55 | 58 |
|
56 | 59 |
|
57 | 60 | class FakeSession: |
58 | | - def __init__(self, responses: list[FakeResponse]) -> None: |
| 61 | + def __init__(self, responses: list[FakeResponse | Exception]) -> None: |
59 | 62 | self._responses = list(responses) |
60 | 63 | self.calls: list[dict[str, Any]] = [] |
61 | 64 |
|
62 | 65 | async def get(self, url: str, **kwargs: Any) -> FakeResponse: |
63 | 66 | self.calls.append({"url": url, **kwargs}) |
64 | | - return self._responses.pop(0) |
| 67 | + response = self._responses.pop(0) |
| 68 | + if isinstance(response, Exception): |
| 69 | + raise response |
| 70 | + return response |
65 | 71 |
|
66 | 72 |
|
67 | 73 | def make_stream(session: FakeSession, **kwargs: Any) -> TeslemetryStream: |
@@ -153,10 +159,37 @@ async def test_vehicle_change(results: list[bool]) -> None: |
153 | 159 | ) |
154 | 160 |
|
155 | 161 |
|
| 162 | +async def test_failed_connect_after_topic_change(results: list[bool]) -> None: |
| 163 | + session = FakeSession( |
| 164 | + [ |
| 165 | + FakeResponse([event(5), event(7)]), |
| 166 | + aiohttp.ClientError("refused"), |
| 167 | + FakeResponse([event()]), |
| 168 | + ] |
| 169 | + ) |
| 170 | + stream = make_stream(session, topics=["data"]) |
| 171 | + stream.active = True |
| 172 | + |
| 173 | + await stream.__anext__() |
| 174 | + stream.topics = ["data", "alerts"] |
| 175 | + with contextlib.suppress(aiohttp.ClientError): |
| 176 | + await stream.connect() # GET with the new topics fails |
| 177 | + await stream.__anext__() # the old response is still supplying cursors |
| 178 | + await stream.__anext__() # old body ends, reconnects with the new topics |
| 179 | + results.append( |
| 180 | + check( |
| 181 | + "old response's cursor is not sent for the new topics", |
| 182 | + sent_cursor(session, 2) is None, |
| 183 | + f"got {sent_cursor(session, 2)!r}", |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + |
156 | 188 | async def main() -> int: |
157 | 189 | results: list[bool] = [] |
158 | 190 | await test_resume(results) |
159 | 191 | await test_vehicle_change(results) |
| 192 | + await test_failed_connect_after_topic_change(results) |
160 | 193 | passed = sum(results) |
161 | 194 | print(f"\n{passed}/{len(results)} checks passed") |
162 | 195 | return 0 if passed == len(results) else 1 |
|
0 commit comments