: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
chore(tests): fix two backend tests that fail on unrelated PRs (#116783)
Thomas Obermüller374c3f0f5e30
diff --git a/posthog/temporal/conftest.py b/posthog/temporal/conftest.pyindex 52da7f021658..6b1cc2afb271 100644--- a/posthog/temporal/conftest.py+++ b/posthog/temporal/conftest.py@@ -2,18 +2,10 @@ from collections.abc import Iteratorimport pytest-import structlog+from posthog.temporal.tests.utils.logger import restored_structlog_config@pytest.fixture(autouse=True)def _restore_structlog_config() -> Iterator[None]:- # configure_logger replaces the process-wide structlog config. Without this restore, a test that- # binds the logger to its own event loop leaves that binding to every later test on the worker,- # which then produces log lines to a closed loop.- previous_config = structlog.get_config()- previously_configured = structlog.is_configured()- yield- if previously_configured:- structlog.configure(**previous_config)- else:- structlog.reset_defaults()+ with restored_structlog_config():+ yielddiff --git a/posthog/temporal/tests/conftest.py b/posthog/temporal/tests/conftest.pyindex 058648fe8045..006261ecd79c 100644--- a/posthog/temporal/tests/conftest.py+++ b/posthog/temporal/tests/conftest.py@@ -1,6 +1,7 @@import randomimport signalimport asyncio+from collections.abc import AsyncIteratorimport pytest@@ -18,6 +19,7 @@ from posthog.models.user import Userfrom posthog.temporal.common.clickhouse import ClickHouseClientfrom posthog.temporal.common.client import connectfrom posthog.temporal.common.logger import configure_logger+from posthog.temporal.tests.utils.logger import restored_structlog_config@pytest.fixture(autouse=True)@@ -176,9 +178,11 @@ async def temporal_worker(temporal_client, workflows, activities):@pytest_asyncio.fixture(autouse=True, scope="module", loop_scope="module")-async def configure_logger_auto() -> None:+async def configure_logger_auto() -> AsyncIterator[None]:"""Configure logger for running temporal tests."""- configure_logger(cache_logger_on_first_use=False)+ with restored_structlog_config():+ configure_logger(cache_logger_on_first_use=False)+ yield@pytest_asyncio.fixturediff --git a/posthog/temporal/tests/utils/logger.py b/posthog/temporal/tests/utils/logger.pynew file mode 100644index 000000000000..c5ca8b50f7a1--- /dev/null+++ b/posthog/temporal/tests/utils/logger.py@@ -0,0 +1,21 @@+from collections.abc import Iterator+from contextlib import contextmanager++import structlog+++@contextmanager+def restored_structlog_config() -> Iterator[None]:+ # configure_logger replaces the process-wide structlog config. Without this restore, a test that+ # binds the logger to its own event loop leaves that binding to every later test on the worker,+ # which then produces log lines to a closed loop. Every later test in the process also logs+ # straight to stdout instead of through stdlib logging, so assertLogs and caplog see no records.+ previous_config = structlog.get_config()+ previously_configured = structlog.is_configured()+ try:+ yield+ finally:+ if previously_configured:+ structlog.configure(**previous_config)+ else:+ structlog.reset_defaults()diff --git a/products/batch_exports/backend/tests/temporal/conftest.py b/products/batch_exports/backend/tests/temporal/conftest.pyindex 930439a248cc..395f5c5c6b23 100644--- a/products/batch_exports/backend/tests/temporal/conftest.py+++ b/products/batch_exports/backend/tests/temporal/conftest.py@@ -2,6 +2,7 @@ import uuidimport randomimport asyncioimport datetime as dt+from collections.abc import AsyncIteratorimport pytest@@ -26,6 +27,7 @@ from posthog.temporal.common.clickhouse import ClickHouseClientfrom posthog.temporal.common.client import connectfrom posthog.temporal.common.logger import configure_loggerfrom posthog.temporal.tests.utils.events import generate_test_events_in_clickhouse+from posthog.temporal.tests.utils.logger import restored_structlog_configfrom products.batch_exports.backend.facade.api import delete_batch_exports_for_teamsfrom products.batch_exports.backend.temporal import ACTIVITIES, WORKFLOWS@@ -194,9 +196,11 @@ async def temporal_client():@pytest_asyncio.fixture(autouse=True, scope="module", loop_scope="module")-async def configure_logger_auto() -> None:+async def configure_logger_auto() -> AsyncIterator[None]:"""Configure logger when running in a Temporal activity environment."""- configure_logger(cache_logger_on_first_use=False)+ with restored_structlog_config():+ configure_logger(cache_logger_on_first_use=False)+ yield@pytest.fixturediff --git a/products/cdp/backend/test/test_destination_debug_logs.py b/products/cdp/backend/test/test_destination_debug_logs.pyindex 8b682747acc2..8b03516e921a 100644--- a/products/cdp/backend/test/test_destination_debug_logs.py+++ b/products/cdp/backend/test/test_destination_debug_logs.py@@ -70,7 +70,7 @@ class TestDestinationDebugLogs(ClickhouseTestMixin, BaseTest):]for i, (function, message) in enumerate(doomed):self._insert(function, "debug", message, BEFORE + timedelta(seconds=i))- for i, (function, level, message, timestamp) in enumerate(kept):+ for i, (function, level, message, timestamp) in enumerate(kept, start=len(doomed)):self._insert(function, level, message, timestamp + timedelta(seconds=i))scope = DebugLogScope(before=CUTOFF, team_id=self.team.pk)diff --git a/products/tasks/backend/temporal/conftest.py b/products/tasks/backend/temporal/conftest.pyindex cae7d66e36d1..945e3ffb4741 100644--- a/products/tasks/backend/temporal/conftest.py+++ b/products/tasks/backend/temporal/conftest.py@@ -1,6 +1,7 @@import osimport timeimport random+from collections.abc import Iteratorimport pytest@@ -8,6 +9,7 @@ from temporalio.testing import ActivityEnvironmentfrom posthog.models import Integration, OAuthApplication, Organization, OrganizationMembership, Team, Userfrom posthog.temporal.common.logger import configure_logger+from posthog.temporal.tests.utils.logger import restored_structlog_configfrom products.tasks.backend.logic.services.sandbox import Sandbox, SandboxConfig, SandboxTemplatefrom products.tasks.backend.models import SandboxSnapshot, Task, TaskRun@@ -195,9 +197,11 @@ def snapshot_context(github_integration, team) -> SnapshotContext:@pytest.fixture(autouse=True)-def configure_logger_auto() -> None:+def configure_logger_auto() -> Iterator[None]:"""Configure logger when running in a Temporal activity environment."""- configure_logger(cache_logger_on_first_use=False)+ with restored_structlog_config():+ configure_logger(cache_logger_on_first_use=False)+ yielddef get_or_create_test_snapshots(github_integration):