Skip to content

Commit b832919

Browse files
committed
Draft of actor call with RemainingTime timeout
1 parent a992dcb commit b832919

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

tests/integration/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
_TOKEN_ENV_VAR = 'APIFY_TEST_USER_API_TOKEN'
3131
_API_URL_ENV_VAR = 'APIFY_INTEGRATION_TESTS_API_URL'
3232
_SDK_ROOT_PATH = Path(__file__).parent.parent.parent.resolve()
33-
33+
_DEFAULT_TEST_TIMEOUT = 600
3434

3535
@pytest.fixture
3636
def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callable[[], None]:
@@ -315,7 +315,7 @@ async def _make_actor(
315315
name=actor_name,
316316
default_run_build='latest',
317317
default_run_memory_mbytes=256,
318-
default_run_timeout_secs=600,
318+
default_run_timeout_secs=_DEFAULT_TEST_TIMEOUT,
319319
versions=[
320320
{
321321
'versionNumber': '0.0',
@@ -331,7 +331,7 @@ async def _make_actor(
331331
print(f'Building Actor {actor_name}...')
332332
build_result = await actor_client.build(version_number='0.0')
333333
build_client = client.build(build_result['id'])
334-
build_client_result = await build_client.wait_for_finish(wait_secs=600)
334+
build_client_result = await build_client.wait_for_finish(wait_secs=_DEFAULT_TEST_TIMEOUT)
335335

336336
assert build_client_result is not None
337337
assert build_client_result['status'] == ActorJobStatus.SUCCEEDED
@@ -408,7 +408,7 @@ async def _run_actor(
408408

409409
client = ApifyClientAsync(token=apify_token, api_url=os.getenv(_API_URL_ENV_VAR))
410410
run_client = client.run(call_result['id'])
411-
run_result = await run_client.wait_for_finish(wait_secs=600)
411+
run_result = await run_client.wait_for_finish(wait_secs=_DEFAULT_TEST_TIMEOUT)
412412

413413
return ActorRun.model_validate(run_result)
414414

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
from apify import Actor
6+
from tests.integration.conftest import MakeActorFunction, RunActorFunction
7+
8+
9+
async def test_actor_start_remaining_timeout(
10+
make_actor: MakeActorFunction,
11+
run_actor: RunActorFunction,
12+
) -> None:
13+
async def main() -> None:
14+
async with Actor:
15+
actor_input = (await Actor.get_input()) or {}
16+
if actor_input.get("called_from_another_actor",False) is True:
17+
# Do nothing and only wait for
18+
await asyncio.sleep(1000)
19+
return
20+
21+
22+
self_run_client = Actor.apify_client.run(Actor.configuration.actor_run_id)
23+
self_run_data_1 = await self_run_client.get()
24+
run_time_1 = self_run_data_1.get('runTimeSecs', 0)
25+
self_timeout = self_run_data_1.get('options', {}).get('timeoutSecs', 0)
26+
27+
# Start another run of this actor with timeout set to the time remaining in this actor
28+
other_run_data = await Actor.start(actor_id=Actor.configuration.actor_id,
29+
run_input={"called_from_another_actor": True},
30+
timeout="RemainingTime")
31+
32+
self_run_data_2 = await self_run_client.get()
33+
run_time_2 = self_run_data_2.get('runTimeSecs', 0)
34+
other_actor_timeout = other_run_data.get('options', {}).get('timeoutSecs', 0)
35+
36+
assert other_actor_timeout > self_timeout - run_time_1
37+
assert other_actor_timeout < self_timeout - run_time_2
38+
39+
actor = await make_actor(label='remaining timeout', main_func=main)
40+
run_result = await run_actor(actor)
41+
42+
assert run_result.status == 'SUCCEEDED'

0 commit comments

Comments
 (0)