Skip to content
Closed
32 changes: 32 additions & 0 deletions clients/python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from collections.abc import Callable, Generator
from datetime import UTC, datetime
from typing import Any

import pytest
import sentry_sdk
import time_machine
from arroyo.backends.kafka import KafkaProducer
from sentry_sdk.envelope import Envelope
from sentry_sdk.transport import Transport

from taskbroker_client.types import AtMostOnceStore

Expand All @@ -21,6 +27,32 @@ def freeze_time(t: str | datetime | None = None) -> time_machine.travel:
return time_machine.travel(t, tick=False)


@pytest.fixture
def sentry_init() -> Generator[Callable[..., None], None, None]:
clients = []

def inner(*a: Any, **kw: Any) -> None:
kw.setdefault("transport", TestTransport())
client = sentry_sdk.Client(*a, **kw)
clients.append(client)
sentry_sdk.get_global_scope().set_client(client)

old_client = sentry_sdk.get_global_scope().client
try:
sentry_sdk.get_current_scope().set_client(None)
yield inner
finally:
for client in clients:
client.close()
sentry_sdk.get_global_scope().set_client(old_client)
Comment thread
alexander-alderman-webb marked this conversation as resolved.


class TestTransport(Transport):
def capture_envelope(self, _: Envelope) -> None:
"""No-op capture_envelope for tests"""
pass


class StubAtMostOnce(AtMostOnceStore):
def __init__(self) -> None:
self._keys: dict[str, str] = {}
Expand Down
20 changes: 16 additions & 4 deletions clients/python/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
from collections.abc import MutableMapping
from concurrent.futures import Future
from typing import Any
from typing import Any, Callable
from unittest.mock import patch

import msgpack
Expand Down Expand Up @@ -297,7 +297,11 @@ def with_parameters(one: str, two: int, org_id: int) -> None:
assert activation.parameters == ""


def test_create_activation_tracing(task_namespace: TaskNamespace) -> None:
def test_create_activation_tracing(
sentry_init: Callable[..., None], task_namespace: TaskNamespace
) -> None:
sentry_init(traces_sample_rate=1.0)

@task_namespace.register(name="test.parameters")
def with_parameters(one: str, two: int, org_id: int) -> None:
raise NotImplementedError
Expand All @@ -310,7 +314,11 @@ def with_parameters(one: str, two: int, org_id: int) -> None:
assert "baggage" in headers


def test_create_activation_tracing_headers(task_namespace: TaskNamespace) -> None:
def test_create_activation_tracing_headers(
sentry_init: Callable[..., None], task_namespace: TaskNamespace
) -> None:
sentry_init(traces_sample_rate=1.0)

@task_namespace.register(name="test.parameters")
def with_parameters(one: str, two: int, org_id: int) -> None:
raise NotImplementedError
Expand All @@ -326,7 +334,11 @@ def with_parameters(one: str, two: int, org_id: int) -> None:
assert headers["key"] == "value"


def test_create_activation_tracing_disable(task_namespace: TaskNamespace) -> None:
def test_create_activation_tracing_disable(
sentry_init: Callable[..., None], task_namespace: TaskNamespace
) -> None:
sentry_init(traces_sample_rate=1.0)

@task_namespace.register(name="test.parameters")
def with_parameters(one: str, two: int, org_id: int) -> None:
raise NotImplementedError
Expand Down
Loading
Loading