Skip to content

Commit c04fd5b

Browse files
Test future cancellation for invalid pagination callbacks
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 301e206 commit c04fd5b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/test_polling.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,31 @@ def on_page_success(response: dict) -> object:
10311031
assert callback_attempts["count"] == 1
10321032

10331033

1034+
def test_collect_paginated_results_cancels_future_on_page_success_results():
1035+
loop = asyncio.new_event_loop()
1036+
try:
1037+
callback_future = loop.create_future()
1038+
1039+
with pytest.raises(
1040+
HyperbrowserError,
1041+
match="on_page_success must return a non-awaitable result",
1042+
):
1043+
collect_paginated_results(
1044+
operation_name="sync paginated on-page-success future",
1045+
get_next_page=lambda page: {"current": 1, "total": 1, "items": []},
1046+
get_current_page_batch=lambda response: response["current"],
1047+
get_total_page_batches=lambda response: response["total"],
1048+
on_page_success=lambda response: callback_future, # type: ignore[return-value]
1049+
max_wait_seconds=1.0,
1050+
max_attempts=5,
1051+
retry_delay_seconds=0.0001,
1052+
)
1053+
1054+
assert callback_future.cancelled()
1055+
finally:
1056+
loop.close()
1057+
1058+
10341059
def test_collect_paginated_results_fails_fast_when_on_page_success_raises():
10351060
page_attempts = {"count": 0}
10361061

@@ -1205,6 +1230,32 @@ def on_page_success(response: dict) -> object:
12051230
asyncio.run(run())
12061231

12071232

1233+
def test_collect_paginated_results_async_cancels_future_on_page_success_results():
1234+
async def run() -> None:
1235+
callback_future = asyncio.get_running_loop().create_future()
1236+
1237+
with pytest.raises(
1238+
HyperbrowserError,
1239+
match="on_page_success must return a non-awaitable result",
1240+
):
1241+
await collect_paginated_results_async(
1242+
operation_name="async paginated on-page-success future",
1243+
get_next_page=lambda page: asyncio.sleep(
1244+
0, result={"current": 1, "total": 1, "items": []}
1245+
),
1246+
get_current_page_batch=lambda response: response["current"],
1247+
get_total_page_batches=lambda response: response["total"],
1248+
on_page_success=lambda response: callback_future, # type: ignore[return-value]
1249+
max_wait_seconds=1.0,
1250+
max_attempts=5,
1251+
retry_delay_seconds=0.0001,
1252+
)
1253+
1254+
assert callback_future.cancelled()
1255+
1256+
asyncio.run(run())
1257+
1258+
12081259
def test_collect_paginated_results_async_fails_fast_when_on_page_success_raises():
12091260
async def run() -> None:
12101261
page_attempts = {"count": 0}

0 commit comments

Comments
 (0)