Skip to content

: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.

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.py
index 52da7f021658..6b1cc2afb271 100644
--- a/posthog/temporal/conftest.py
+++ b/posthog/temporal/conftest.py
@@ -2,18 +2,10 @@ from collections.abc import Iterator
import 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():
+ yield
diff --git a/posthog/temporal/tests/conftest.py b/posthog/temporal/tests/conftest.py
index 058648fe8045..006261ecd79c 100644
--- a/posthog/temporal/tests/conftest.py
+++ b/posthog/temporal/tests/conftest.py
@@ -1,6 +1,7 @@
import random
import signal
import asyncio
+from collections.abc import AsyncIterator
import pytest
@@ -18,6 +19,7 @@ from posthog.models.user import User
from posthog.temporal.common.clickhouse import ClickHouseClient
from posthog.temporal.common.client import connect
from 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.fixture
diff --git a/posthog/temporal/tests/utils/logger.py b/posthog/temporal/tests/utils/logger.py
new file mode 100644
index 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.py
index 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 uuid
import random
import asyncio
import datetime as dt
+from collections.abc import AsyncIterator
import pytest
@@ -26,6 +27,7 @@ from posthog.temporal.common.clickhouse import ClickHouseClient
from posthog.temporal.common.client import connect
from posthog.temporal.common.logger import configure_logger
from posthog.temporal.tests.utils.events import generate_test_events_in_clickhouse
+from posthog.temporal.tests.utils.logger import restored_structlog_config
from products.batch_exports.backend.facade.api import delete_batch_exports_for_teams
from 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.fixture
diff --git a/products/cdp/backend/test/test_destination_debug_logs.py b/products/cdp/backend/test/test_destination_debug_logs.py
index 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.py
index cae7d66e36d1..945e3ffb4741 100644
--- a/products/tasks/backend/temporal/conftest.py
+++ b/products/tasks/backend/temporal/conftest.py
@@ -1,6 +1,7 @@
import os
import time
import random
+from collections.abc import Iterator
import pytest
@@ -8,6 +9,7 @@ from temporalio.testing import ActivityEnvironment
from posthog.models import Integration, OAuthApplication, Organization, OrganizationMembership, Team, User
from posthog.temporal.common.logger import configure_logger
+from posthog.temporal.tests.utils.logger import restored_structlog_config
from products.tasks.backend.logic.services.sandbox import Sandbox, SandboxConfig, SandboxTemplate
from 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)
+ yield
def get_or_create_test_snapshots(github_integration):