Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(threading): Add spec for ThreadPoolExecutor #2259

Merged
merged 8 commits into from
Aug 16, 2023
Merged
38 changes: 38 additions & 0 deletions tests/integrations/threading/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from sentry_sdk import configure_scope, capture_message
from sentry_sdk.integrations.threading import ThreadingIntegration
from concurrent.futures import ThreadPoolExecutor, as_completed
import sentry_sdk

original_start = Thread.start
original_run = Thread.run
Expand Down Expand Up @@ -73,6 +75,42 @@ def stage2():
assert "stage1" not in event.get("tags", {})


@pytest.mark.skipif(
sys.version_info < (3, 2),
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 ThreadPoolExecutor(max_workers=1) as executor:
futures = [executor.submit(double, number) for number in [1, 2, 3, 4]]
for future in as_completed(futures):
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
gggritso marked this conversation as resolved.
Show resolved Hide resolved


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

Expand Down