Skip to content
Draft
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
4 changes: 2 additions & 2 deletions src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,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 Expand Up @@ -1208,7 +1208,7 @@ async def reboot(

self._is_rebooting = True

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

# Call all the listeners for the PERSIST_STATE and MIGRATING events, and wait for them to finish.
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 @@ -420,3 +421,39 @@ async def hanging_listener(*_args: object) -> None:

# The reboot API call proceeded despite the hanging listener.
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
Loading