8000 test(threading): Add test for `ThreadPoolExecutor` (#2259) · crt-fork/sentry-python@3845489 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3845489

Browse files
gggritsosl0thentr0pyantonpirker
authored
test(threading): Add test for ThreadPoolExecutor (getsentry#2259)
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>
1 parent f1fb5e1 commit 3845489

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/integrations/threading/test_threading.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
import sys
33
from threading import Thread
44

5+
try:
6+
from concurrent import futures
7+
except ImportError:
8+
futures = None
9+
510
import pytest
611

12+
import sentry_sdk
713
from sentry_sdk import configure_scope, capture_message
814
from sentry_sdk.integrations.threading import ThreadingIntegration
915

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

7581

82+
@pytest.mark.skipif(
83+
futures is None,
84+
reason="ThreadPool was added in 3.2",
85+
)
86+
@pytest.mark.parametrize("propagate_hub", (True, False))
87+
def test_propagates_threadpool_hub(sentry_init, capture_events, propagate_hub):
88+
sentry_init(
89+
traces_sample_rate=1.0,
90+
integrations=[ThreadingIntegration(propagate_hub=propagate_hub)],
91+
)
92+
events = capture_events()
93+
94+
def double(number):
95+
with sentry_sdk.start_span(op="task", description=str(number)):
96+
return number * 2
97+
98+
with sentry_sdk.start_transaction(name="test_handles_threadpool"):
99+
with futures.ThreadPoolExecutor(max_workers=1) as executor:
100+
tasks = [executor.submit(double, number) for number in [1, 2, 3, 4]]
101+
for future in futures.as_completed(tasks):
102+
print("Getting future value!", future.result())
103+
104+
sentry_sdk.flush()
105+
106+
if propagate_hub:
107+
assert len(events) == 1
108+
(event,) = events
109+
assert event["spans"][0]["trace_id"] == event["spans"][1]["trace_id"]
110+
assert event["spans"][1]["trace_id"] == event["spans"][2]["trace_id"]
111+
assert event["spans"][2]["trace_id"] == event["spans"][3]["trace_id"]
112+
assert event["spans"][3]["trace_id"] == event["spans"][0]["trace_id"]
113+
else:
114+
(event,) = events
115+
assert len(event["spans"]) == 0
116+
117+
76118
def test_circular_references(sentry_init, request):
77119
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])
78120

0 commit comments

Comments
 (0)
0