Skip to content

Commit e134f41

Browse files
committed
fix: tie the cursor scope to the response that supplied it
1 parent 52ee408 commit e134f41

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎teslemetry_stream/stream.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,7 @@ async def connect(self) -> None:
272272
if self.topics:
273273
params["topics"] = ",".join(self.topics)
274274
scope = (url, params.get("topics"))
275-
if scope != self._cursor_scope:
276-
self._cursor = None
277-
self._cursor_scope = scope
278-
elif self._cursor is not None:
275+
if scope == self._cursor_scope and self._cursor is not None:
279276
params["cursor"] = str(self._cursor)
280277
response = await self._session.get(
281278
url,
@@ -295,6 +292,10 @@ async def connect(self) -> None:
295292
if self._response is not None:
296293
self._response.close()
297294
self._response = response
295+
# Only now, so a failed GET leaves the cursor tied to the response still supplying it.
296+
if scope != self._cursor_scope:
297+
self._cursor = None
298+
self._cursor_scope = scope
298299
LOGGER.debug(
299300
"Connected to %s with status %s", self._response.url, self._response.status
300301
)

‎tests/test_sse_cursor.py‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
from __future__ import annotations
77

88
import asyncio
9+
import contextlib
910
import json
1011
from typing import Any
1112

13+
import aiohttp
14+
1215
from teslemetry_stream.stream import TeslemetryStream
1316

1417
VIN = "LRW3F7EK4NC000001"
@@ -55,13 +58,16 @@ def close(self) -> None:
5558

5659

5760
class FakeSession:
58-
def __init__(self, responses: list[FakeResponse]) -> None:
61+
def __init__(self, responses: list[FakeResponse | Exception]) -> None:
5962
self._responses = list(responses)
6063
self.calls: list[dict[str, Any]] = []
6164

6265
async def get(self, url: str, **kwargs: Any) -> FakeResponse:
6366
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
6571

6672

6773
def make_stream(session: FakeSession, **kwargs: Any) -> TeslemetryStream:
@@ -153,10 +159,37 @@ async def test_vehicle_change(results: list[bool]) -> None:
153159
)
154160

155161

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+
156188
async def main() -> int:
157189
results: list[bool] = []
158190
await test_resume(results)
159191
await test_vehicle_change(results)
192+
await test_failed_connect_after_topic_change(results)
160193
passed = sum(results)
161194
print(f"\n{passed}/{len(results)} checks passed")
162195
return 0 if passed == len(results) else 1

0 commit comments

Comments
 (0)