Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ async def metamorph(
self.log.error('Actor.metamorph() is only supported when running on the Apify platform.')
return

if not custom_after_sleep:
if custom_after_sleep is None:
custom_after_sleep = self.configuration.metamorph_after_sleep

# If is_at_home() is True, configuration.actor_run_id is always set
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/actor/test_actor_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING
from unittest.mock import AsyncMock, patch

import pytest

Expand Down Expand Up @@ -422,6 +423,42 @@ async def hanging_listener(*_args: object) -> None:
assert len(apify_client_async_patcher.calls['run']['reboot']) == 1


async def test_metamorph_with_zero_custom_after_sleep_does_not_sleep(
apify_client_async_patcher: ApifyClientAsyncPatcher,
) -> None:
"""Test that an explicit `custom_after_sleep=timedelta(0)` is not replaced by the default sleep duration."""
apify_client_async_patcher.patch('run', 'metamorph', return_value=None)

async with Actor:
Actor.configuration.is_at_home = True
Actor.configuration.actor_run_id = 'some-run-id'

with patch('asyncio.sleep', new=AsyncMock()) as sleep_mock:
await Actor.metamorph('target-actor-id', custom_after_sleep=timedelta(0))

sleep_mock.assert_not_awaited()

assert len(apify_client_async_patcher.calls['run']['metamorph']) == 1


async def test_reboot_with_zero_custom_after_sleep_does_not_sleep(
apify_client_async_patcher: ApifyClientAsyncPatcher,
) -> None:
"""Test that an explicit `custom_after_sleep=timedelta(0)` is not replaced by the default sleep duration."""
apify_client_async_patcher.patch('run', 'reboot', return_value=None)

async with Actor:
Actor.configuration.is_at_home = True
Actor.configuration.actor_run_id = 'some-run-id'

with patch('asyncio.sleep', new=AsyncMock()) as sleep_mock:
await Actor.reboot(custom_after_sleep=timedelta(0))

sleep_mock.assert_not_awaited()

assert len(apify_client_async_patcher.calls['run']['reboot']) == 1


async def test_reboot_can_be_retried_after_failed_attempt(
apify_client_async_patcher: ApifyClientAsyncPatcher,
) -> None:
Expand Down