:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import dataclasses
from datetime import timedelta
import pytest
from temporalio import activity
from temporalio.testing import ActivityEnvironment
from posthog.temporal.common.posthog_client import is_expected_activity_failure
from products.tasks.backend.exceptions import (
CredentialUnavailableError,
GatewayTokenUnavailableError,
SandboxControlPlaneUnavailableError,
SandboxNotRunningError,
SandboxRateLimitedError,
is_transient_failure_error_type,
)
@activity.defn
def _shed_by_the_proxy() -> None:
raise SandboxRateLimitedError("x", {})
def test_temporal_failure_type_defaults_to_class_name():
error = SandboxNotRunningError("boom", {}, cause=RuntimeError("x"), capture=False)
assert error.type == "SandboxNotRunningError"
assert not error.non_retryable
@pytest.mark.parametrize(
"error",
[
SandboxRateLimitedError("x", {}),
SandboxControlPlaneUnavailableError("x", {}),
CredentialUnavailableError("x", {}),
GatewayTokenUnavailableError("x", {}),
],
)
def test_expected_errors_are_not_reported_by_the_activity_interceptor(error: Exception) -> None:
assert is_expected_activity_failure(error) is True
@pytest.mark.parametrize(
"attempt,expected_floor,expected_ceiling",
[
(1, 22.5, 45.0),
(2, 45.0, 90.0),
(3, 90.0, 180.0),
(6, 150.0, 300.0),
],
)
def test_rate_limit_backoff_follows_the_activity_attempt(attempt: int, expected_floor: float, expected_ceiling: float):
environment = ActivityEnvironment()
environment.info = dataclasses.replace(environment.info, attempt=attempt)
with pytest.raises(SandboxRateLimitedError) as error:
environment.run(_shed_by_the_proxy)
assert error.value.non_retryable is False
assert error.value.next_retry_delay is not None
assert timedelta(seconds=expected_floor) <= error.value.next_retry_delay <= timedelta(seconds=expected_ceiling)
@pytest.mark.parametrize(
"error_type,expected",
[
("RepositoryCloneError", True),
("SandboxRateLimitedError", True),
("start_to_close", True),
("RepositoryAccessError", False),
("CredentialUnavailableError", False),
("unspecified", False),
(None, False),
],
)
def test_transient_failure_error_types_cover_transient_subclasses_and_timeouts(error_type: str | None, expected: bool):
assert is_transient_failure_error_type(error_type) is expected