Skip to content
Prev Previous commit
Next Next commit
more tests
  • Loading branch information
sentrivana committed Nov 2, 2023
commit 3dcec2a17eb58e0644c9727b39e97bb9b1a01036
20 changes: 20 additions & 0 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from sentry_sdk import Hub, start_transaction
from sentry_sdk.transport import Transport

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3


class HealthyTestTransport(Transport):
def _send_event(self, event):
Expand Down Expand Up @@ -82,3 +87,18 @@ def test_transaction_uses_downsampled_rate(
assert transaction.sample_rate == 0.5

assert reports == [("backpressure", "transaction")]


def test_monitor_no_thread_on_shutdown_no_errors(sentry_init):
sentry_init(transport=HealthyTestTransport())

# make it seem like the interpreter is shutting down
with mock.patch(
"threading.Thread.start",
side_effect=RuntimeError("can't create new thread at interpreter shutdown"),
):
monitor = Hub.current.client.monitor
assert monitor is not None
assert monitor._thread is None
monitor.run()
assert monitor._thread is None
34 changes: 34 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from sentry_sdk import Hub
from sentry_sdk.sessions import auto_session_tracking

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3


def sorted_aggregates(item):
aggregates = item["aggregates"]
Expand Down Expand Up @@ -119,3 +124,32 @@ def test_aggregates_explicitly_disabled_session_tracking_request_mode(
assert len(aggregates) == 1
assert aggregates[0]["exited"] == 1
assert "errored" not in aggregates[0]


def test_no_thread_on_shutdown_no_errors(sentry_init):
sentry_init(
release="fun-release",
environment="not-fun-env",
)

hub = Hub.current

# make it seem like the interpreter is shutting down
with mock.patch(
"threading.Thread.start",
side_effect=RuntimeError("can't create new thread at interpreter shutdown"),
):
with auto_session_tracking(session_mode="request"):
with sentry_sdk.push_scope():
try:
raise Exception("all is wrong")
except Exception:
sentry_sdk.capture_exception()

with auto_session_tracking(session_mode="request"):
pass

hub.start_session(session_mode="request")
hub.end_session()

sentry_sdk.flush()
19 changes: 19 additions & 0 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from sentry_sdk.envelope import Envelope, parse_json
from sentry_sdk.integrations.logging import LoggingIntegration

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3

CapturedData = namedtuple("CapturedData", ["path", "event", "envelope", "compressed"])

Expand Down Expand Up @@ -165,6 +169,21 @@ def test_transport_infinite_loop(capturing_server, request, make_client):
assert len(capturing_server.captured) == 1


def test_transport_no_thread_on_shutdown_no_errors(capturing_server, make_client):
client = make_client()

# make it seem like the interpreter is shutting down
with mock.patch(
"threading.Thread.start",
side_effect=RuntimeError("can't create new thread at interpreter shutdown"),
):
with Hub(client):
capture_message("hi")

# nothing exploded but also no events can be sent anymore
assert len(capturing_server.captured) == 0


NOW = datetime(2014, 6, 2)


Expand Down