Skip to content

Commit fe007cb

Browse files
committed
test: address review findings (centralize RQ poll timeout, dedup get_run, bound webhook wait)
- Add an rq_poll_timeout fixture deriving the single/shared polling timeout from the request_queue_apify param, replacing the derivation block that was copy-pasted into all 20 request queue tests. - Extract the byte-identical get_run closure in test_actor_charge.py into a module-level _get_run helper used via functools.partial. - Bound the webhook server Actor's wait loop (5s server timeout, 300s deadline) so a webhook that never propagates fails the test quickly with an empty WEBHOOK_BODY and a clear message instead of blocking until the run timeout.
1 parent ccda2ec commit fe007cb

4 files changed

Lines changed: 100 additions & 124 deletions

File tree

‎tests/e2e/test_actor_api_helpers.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ async def test_actor_adds_webhook_and_receives_event(
393393
) -> None:
394394
async def main_server() -> None:
395395
import os
396+
import time
396397
from http.server import BaseHTTPRequestHandler, HTTPServer
397398

398399
from apify_shared.consts import ActorEnvVars
@@ -419,7 +420,12 @@ def do_POST(self) -> None:
419420
container_port = int(os.getenv(ActorEnvVars.WEB_SERVER_PORT, ''))
420421
with HTTPServer(('', container_port), WebhookHandler) as server:
421422
await Actor.set_value('INITIALIZED', value=True)
422-
while not webhook_body:
423+
# Bound the wait so that a webhook that never fires (e.g. one that did not propagate before the
424+
# client run finished) surfaces as an empty WEBHOOK_BODY in the test instead of blocking here
425+
# until the run times out.
426+
server.timeout = 5
427+
deadline = time.monotonic() + 300
428+
while not webhook_body and time.monotonic() < deadline:
423429
server.handle_request()
424430

425431
await Actor.set_value('WEBHOOK_BODY', webhook_body)
@@ -478,7 +484,7 @@ async def main_client() -> None:
478484

479485
webhook_body_record = await server_actor.last_run().key_value_store().get_record('WEBHOOK_BODY')
480486
assert webhook_body_record is not None
481-
assert webhook_body_record['value'] != ''
487+
assert webhook_body_record['value'] != '', 'The ad-hoc webhook never fired (it likely did not propagate in time).'
482488
parsed_webhook_body = json.loads(webhook_body_record['value'])
483489

484490
assert parsed_webhook_body['eventData']['actorId'] == ac_run_result.act_id

‎tests/e2e/test_actor_charge.py‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from decimal import Decimal
4+
from functools import partial
45
from typing import TYPE_CHECKING
56

67
import pytest_asyncio
@@ -18,6 +19,11 @@
1819
from .conftest import MakeActorFunction, RunActorFunction
1920

2021

22+
async def _get_run(apify_client_async: ApifyClientAsync, run_id: str) -> ActorRun:
23+
"""Fetch the current state of the given run from the platform."""
24+
return ActorRun.model_validate(await apify_client_async.run(run_id).get())
25+
26+
2127
@pytest_asyncio.fixture(scope='module', loop_scope='module')
2228
async def ppe_push_data_actor_build(make_actor: MakeActorFunction) -> str:
2329
async def main() -> None:
@@ -116,14 +122,10 @@ async def test_actor_charge_basic(
116122
apify_client_async: ApifyClientAsync,
117123
) -> None:
118124
run = await run_actor(ppe_actor)
119-
run_id = run.id
120-
121-
async def get_run() -> ActorRun:
122-
return ActorRun.model_validate(await apify_client_async.run(run_id).get())
123125

124126
# Refetch until the charged event counts propagate on the platform.
125127
run = await poll_until_condition(
126-
get_run,
128+
partial(_get_run, apify_client_async, run.id),
127129
lambda r: r.status == ActorJobStatus.SUCCEEDED and r.charged_event_counts == {'foobar': 4},
128130
timeout=30,
129131
poll_interval=1,
@@ -139,14 +141,10 @@ async def test_actor_charge_limit(
139141
apify_client_async: ApifyClientAsync,
140142
) -> None:
141143
run = await run_actor(ppe_actor, max_total_charge_usd=Decimal('0.2'))
142-
run_id = run.id
143-
144-
async def get_run() -> ActorRun:
145-
return ActorRun.model_validate(await apify_client_async.run(run_id).get())
146144

147145
# Refetch until the charged event counts propagate on the platform.
148146
run = await poll_until_condition(
149-
get_run,
147+
partial(_get_run, apify_client_async, run.id),
150148
lambda r: r.status == ActorJobStatus.SUCCEEDED and r.charged_event_counts == {'foobar': 2},
151149
timeout=30,
152150
poll_interval=1,
@@ -164,11 +162,6 @@ async def test_actor_push_data_charges_both_events(
164162
"""Test that push_data charges both the explicit event and the synthetic apify-default-dataset-item event."""
165163
run = await run_actor(ppe_push_data_actor)
166164

167-
run_id = run.id
168-
169-
async def get_run() -> ActorRun:
170-
return ActorRun.model_validate(await apify_client_async.run(run_id).get())
171-
172165
expected_counts = {
173166
'push-item': 5,
174167
'apify-default-dataset-item': 5,
@@ -178,7 +171,7 @@ async def get_run() -> ActorRun:
178171
# from dataset writes asynchronously, so they propagate more slowly than explicit charges (which are
179172
# reflected immediately via the charge endpoint).
180173
run = await poll_until_condition(
181-
get_run,
174+
partial(_get_run, apify_client_async, run.id),
182175
lambda r: r.status == ActorJobStatus.SUCCEEDED and r.charged_event_counts == expected_counts,
183176
timeout=120,
184177
poll_interval=1,
@@ -199,11 +192,6 @@ async def test_actor_push_data_combined_budget_limit(
199192
"""
200193
run = await run_actor(ppe_push_data_actor, max_total_charge_usd=Decimal('0.20'))
201194

202-
run_id = run.id
203-
204-
async def get_run() -> ActorRun:
205-
return ActorRun.model_validate(await apify_client_async.run(run_id).get())
206-
207195
expected_counts = {
208196
'push-item': 2,
209197
'apify-default-dataset-item': 2,
@@ -213,7 +201,7 @@ async def get_run() -> ActorRun:
213201
# from dataset writes asynchronously, so they propagate more slowly than explicit charges (which are
214202
# reflected immediately via the charge endpoint).
215203
run = await poll_until_condition(
216-
get_run,
204+
partial(_get_run, apify_client_async, run.id),
217205
lambda r: r.status == ActorJobStatus.SUCCEEDED and r.charged_event_counts == expected_counts,
218206
timeout=120,
219207
poll_interval=1,

‎tests/integration/conftest.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ async def request_queue_apify(
9494
await rq.drop()
9595

9696

97+
@pytest.fixture
98+
def rq_access_mode(request: pytest.FixtureRequest) -> str:
99+
"""Return the access mode (`single` or `shared`) of the parametrized `request_queue_apify` fixture."""
100+
return request.node.callspec.params.get('request_queue_apify')
101+
102+
103+
@pytest.fixture
104+
def rq_poll_timeout(rq_access_mode: str) -> int:
105+
"""Return the `poll_until_condition` timeout matching the `request_queue_apify` access mode.
106+
107+
In single mode, reads are immediately consistent, so the caller should poll exactly once (`timeout=0`). In
108+
shared mode, there is a propagation delay between operations, so reads are retried for up to 30 seconds.
109+
See https://github.com/apify/apify-sdk-python/issues/808.
110+
"""
111+
return 0 if rq_access_mode == 'single' else 30
112+
113+
97114
@pytest.fixture(autouse=True)
98115
def _isolate_test_environment(prepare_test_env: Callable[[], None]) -> None:
99116
"""Isolate the testing environment by resetting global state before each test.

0 commit comments

Comments
 (0)