|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from apify import Actor |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from .conftest import MakeActorFunction, RunActorFunction |
| 10 | + |
| 11 | + |
| 12 | +async def test_actor_start_remaining_timeout( |
| 13 | + make_actor: MakeActorFunction, |
| 14 | + run_actor: RunActorFunction, |
| 15 | +) -> None: |
| 16 | + async def main() -> None: |
| 17 | + from datetime import datetime, timezone |
| 18 | + |
| 19 | + async with Actor: |
| 20 | + actor_input = (await Actor.get_input()) or {} |
| 21 | + if actor_input.get('called_from_another_actor', False) is True: |
| 22 | + return |
| 23 | + |
| 24 | + await asyncio.sleep(1) |
| 25 | + # Start another run of this actor with timeout set to the time remaining in this actor run |
| 26 | + other_run_data = await Actor.start( |
| 27 | + actor_id=Actor.configuration.actor_id or '', |
| 28 | + run_input={'called_from_another_actor': True}, |
| 29 | + timeout='RemainingTime', |
| 30 | + ) |
| 31 | + |
| 32 | + # To make sure that the actor is started |
| 33 | + await asyncio.sleep(5) |
| 34 | + |
| 35 | + assert Actor.configuration.timeout_at is not None |
| 36 | + assert Actor.configuration.started_at is not None |
| 37 | + assert other_run_data is not None |
| 38 | + assert other_run_data.options is not None |
| 39 | + |
| 40 | + remaining_time_after_actor_start = Actor.configuration.timeout_at - datetime.now(tz=timezone.utc) |
| 41 | + |
| 42 | + try: |
| 43 | + assert other_run_data.options.timeout > remaining_time_after_actor_start |
| 44 | + assert other_run_data.options.timeout < Actor.configuration.timeout_at - Actor.configuration.started_at |
| 45 | + finally: |
| 46 | + # Abort the other actor run after asserting the timeouts |
| 47 | + await Actor.apify_client.run(other_run_data.id).abort() |
| 48 | + |
| 49 | + actor = await make_actor(label='remaining-timeout', main_func=main) |
| 50 | + run_result = await run_actor(actor) |
| 51 | + |
| 52 | + assert run_result.status == 'SUCCEEDED' |
| 53 | + |
| 54 | + |
| 55 | +async def test_actor_call_remaining_timeout( |
| 56 | + make_actor: MakeActorFunction, |
| 57 | + run_actor: RunActorFunction, |
| 58 | +) -> None: |
| 59 | + async def main() -> None: |
| 60 | + from datetime import datetime, timezone |
| 61 | + |
| 62 | + async with Actor: |
| 63 | + actor_input = (await Actor.get_input()) or {} |
| 64 | + if actor_input.get('called_from_another_actor', False) is True: |
| 65 | + return |
| 66 | + |
| 67 | + await asyncio.sleep(1) |
| 68 | + # Start another run of this actor with timeout set to the time remaining in this actor run |
| 69 | + other_run_data = await Actor.call( |
| 70 | + actor_id=Actor.configuration.actor_id or '', |
| 71 | + run_input={'called_from_another_actor': True}, |
| 72 | + timeout='RemainingTime', |
| 73 | + ) |
| 74 | + |
| 75 | + # To make sure that the actor is started |
| 76 | + await asyncio.sleep(5) |
| 77 | + |
| 78 | + assert Actor.configuration.timeout_at is not None |
| 79 | + assert Actor.configuration.started_at is not None |
| 80 | + assert other_run_data is not None |
| 81 | + assert other_run_data.options is not None |
| 82 | + |
| 83 | + remaining_time_after_actor_start = Actor.configuration.timeout_at - datetime.now(tz=timezone.utc) |
| 84 | + |
| 85 | + try: |
| 86 | + assert other_run_data.options.timeout > remaining_time_after_actor_start |
| 87 | + assert other_run_data.options.timeout < Actor.configuration.timeout_at - Actor.configuration.started_at |
| 88 | + finally: |
| 89 | + # Abort the other actor run after asserting the timeouts |
| 90 | + await Actor.apify_client.run(other_run_data.id).abort() |
| 91 | + |
| 92 | + actor = await make_actor(label='remaining-timeout', main_func=main) |
| 93 | + run_result = await run_actor(actor) |
| 94 | + |
| 95 | + assert run_result.status == 'SUCCEEDED' |
0 commit comments