Skip to content

Commit 5927032

Browse files
Expand future-cancellation coverage for invalid polling callbacks
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c04fd5b commit 5927032

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

tests/test_polling.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,28 @@ def get_status() -> object:
343343
assert attempts["count"] == 1
344344

345345

346+
def test_poll_until_terminal_status_cancels_future_status_callback_results():
347+
loop = asyncio.new_event_loop()
348+
try:
349+
status_future = loop.create_future()
350+
351+
with pytest.raises(
352+
HyperbrowserError, match="get_status must return a non-awaitable result"
353+
):
354+
poll_until_terminal_status(
355+
operation_name="sync poll status future",
356+
get_status=lambda: status_future, # type: ignore[return-value]
357+
is_terminal_status=lambda value: value == "completed",
358+
poll_interval_seconds=0.0001,
359+
max_wait_seconds=1.0,
360+
max_status_failures=5,
361+
)
362+
363+
assert status_future.cancelled()
364+
finally:
365+
loop.close()
366+
367+
346368
def test_poll_until_terminal_status_fails_fast_when_terminal_callback_raises():
347369
attempts = {"count": 0}
348370

@@ -1007,6 +1029,30 @@ def get_next_page(page: int) -> object:
10071029
assert attempts["count"] == 1
10081030

10091031

1032+
def test_collect_paginated_results_cancels_future_page_callback_results():
1033+
loop = asyncio.new_event_loop()
1034+
try:
1035+
page_future = loop.create_future()
1036+
1037+
with pytest.raises(
1038+
HyperbrowserError, match="get_next_page must return a non-awaitable result"
1039+
):
1040+
collect_paginated_results(
1041+
operation_name="sync paginated page future",
1042+
get_next_page=lambda page: page_future, # type: ignore[return-value]
1043+
get_current_page_batch=lambda response: response["current"],
1044+
get_total_page_batches=lambda response: response["total"],
1045+
on_page_success=lambda response: None,
1046+
max_wait_seconds=1.0,
1047+
max_attempts=5,
1048+
retry_delay_seconds=0.0001,
1049+
)
1050+
1051+
assert page_future.cancelled()
1052+
finally:
1053+
loop.close()
1054+
1055+
10101056
def test_collect_paginated_results_rejects_awaitable_on_page_success_result():
10111057
callback_attempts = {"count": 0}
10121058

@@ -1310,6 +1356,8 @@ async def get_next_page(page: int) -> dict:
13101356

13111357
def test_collect_paginated_results_async_rejects_awaitable_current_page_callback_result():
13121358
async def run() -> None:
1359+
callback_future = asyncio.get_running_loop().create_future()
1360+
13131361
with pytest.raises(
13141362
HyperbrowserError,
13151363
match="get_current_page_batch must return a non-awaitable result",
@@ -1319,13 +1367,14 @@ async def run() -> None:
13191367
get_next_page=lambda page: asyncio.sleep(
13201368
0, result={"current": 1, "total": 1, "items": []}
13211369
),
1322-
get_current_page_batch=lambda response: asyncio.sleep(0), # type: ignore[return-value]
1370+
get_current_page_batch=lambda response: callback_future, # type: ignore[return-value]
13231371
get_total_page_batches=lambda response: response["total"],
13241372
on_page_success=lambda response: None,
13251373
max_wait_seconds=1.0,
13261374
max_attempts=5,
13271375
retry_delay_seconds=0.0001,
13281376
)
1377+
assert callback_future.cancelled()
13291378

13301379
asyncio.run(run())
13311380

0 commit comments

Comments
 (0)