Skip to content

Commit

Permalink
test(threading): Add test for ThreadPoolExecutor (#2259)
Browse files Browse the repository at this point in the history
ThreadPoolExecutor also obeys hub propagation, but there wasn't a test for it. This PR adds a bit more coverage.

---------

Co-authored-by: Neel Shah <neelshah.sa@gmail.com>
Co-authored-by: Anton Pirker <anton.pirker@sentry.io>
  • Loading branch information
3 people authored Aug 16, 2023
1 parent f1fb5e1 commit 3845489
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/integrations/threading/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
import sys
from threading import Thread

try:
from concurrent import futures
except ImportError:
futures = None

import pytest

import sentry_sdk
from sentry_sdk import configure_scope, capture_message
from sentry_sdk.integrations.threading import ThreadingIntegration

Expand Down Expand Up @@ -73,6 +79,42 @@ def stage2():
assert "stage1" not in event.get("tags", {})


@pytest.mark.skipif(
futures is None,
reason="ThreadPool was added in 3.2",
)
@pytest.mark.parametrize("propagate_hub", (True, False))
def test_propagates_threadpool_hub(sentry_init, capture_events, propagate_hub):
sentry_init(
traces_sample_rate=1.0,
integrations=[ThreadingIntegration(propagate_hub=propagate_hub)],
)
events = capture_events()

def double(number):
with sentry_sdk.start_span(op="task", description=str(number)):
return number * 2

with sentry_sdk.start_transaction(name="test_handles_threadpool"):
with futures.ThreadPoolExecutor(max_workers=1) as executor:
tasks = [executor.submit(double, number) for number in [1, 2, 3, 4]]
for future in futures.as_completed(tasks):
print("Getting future value!", future.result())

sentry_sdk.flush()

if propagate_hub:
assert len(events) == 1
(event,) = events
assert event["spans"][0]["trace_id"] == event["spans"][1]["trace_id"]
assert event["spans"][1]["trace_id"] == event["spans"][2]["trace_id"]
assert event["spans"][2]["trace_id"] == event["spans"][3]["trace_id"]
assert event["spans"][3]["trace_id"] == event["spans"][0]["trace_id"]
else:
(event,) = events
assert len(event["spans"]) == 0


def test_circular_references(sentry_init, request):
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])

Expand Down

0 comments on commit 3845489

Please sign in to comment.