Skip to content

Commit 7c0d466

Browse files
committed
ref(asyncio integration): Update context var names
GH-4699
1 parent cfbea39 commit 7c0d466

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

tests/integrations/asyncio/test_asyncio.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -433,47 +433,57 @@ def test_loop_close_flushes_async_transport(sentry_init):
433433
@minimum_python_38
434434
@pytest.mark.asyncio
435435
async def test_internal_tasks_not_wrapped(sentry_init, capture_events):
436+
"""Test that internal Sentry tasks marked with context manager are not wrapped."""
436437

437-
sentry_init(integrations=[AsyncioIntegration()], traces_sample_rate=1.0)
438-
events = capture_events()
438+
# Get the event loop and save original task factory
439+
loop = asyncio.get_running_loop()
440+
original_task_factory = loop.get_task_factory()
439441

440-
# Create a user task that should be wrapped
441-
async def user_task():
442-
await asyncio.sleep(0.01)
443-
return "user_result"
442+
try:
443+
sentry_init(integrations=[AsyncioIntegration()], traces_sample_rate=1.0)
444+
events = capture_events()
444445

445-
# Create an internal task that should NOT be wrapped
446-
async def internal_task():
447-
await asyncio.sleep(0.01)
448-
return "internal_result"
446+
# Create a user task that should be wrapped
447+
async def user_task():
448+
await asyncio.sleep(0.01)
449+
return "user_result"
449450

450-
with sentry_sdk.start_transaction(name="test_transaction"):
451-
user_task_obj = asyncio.create_task(user_task())
451+
# Create an internal task that should NOT be wrapped
452+
async def internal_task():
453+
await asyncio.sleep(0.01)
454+
return "internal_result"
452455

453-
with mark_sentry_task_internal():
454-
internal_task_obj = asyncio.create_task(internal_task())
456+
with sentry_sdk.start_transaction(name="test_transaction"):
457+
user_task_obj = asyncio.create_task(user_task())
455458

456-
user_result = await user_task_obj
457-
internal_result = await internal_task_obj
459+
with mark_sentry_task_internal():
460+
internal_task_obj = asyncio.create_task(internal_task())
458461

459-
assert user_result == "user_result"
460-
assert internal_result == "internal_result"
462+
user_result = await user_task_obj
463+
internal_result = await internal_task_obj
461464

462-
assert len(events) == 1
463-
transaction = events[0]
465+
assert user_result == "user_result"
466+
assert internal_result == "internal_result"
464467

465-
user_spans = []
466-
internal_spans = []
468+
assert len(events) == 1
469+
transaction = events[0]
467470

468-
for span in transaction.get("spans", []):
469-
if "user_task" in span.get("description", ""):
470-
user_spans.append(span)
471-
elif "internal_task" in span.get("description", ""):
472-
internal_spans.append(span)
471+
user_spans = []
472+
internal_spans = []
473473

474-
assert (
475-
len(user_spans) > 0
476-
), f"User task should have been traced. All spans: {[s.get('description') for s in transaction.get('spans', [])]}"
477-
assert (
478-
len(internal_spans) == 0
479-
), f"Internal task should NOT have been traced. All spans: {[s.get('description') for s in transaction.get('spans', [])]}"
474+
for span in transaction.get("spans", []):
475+
if "user_task" in span.get("description", ""):
476+
user_spans.append(span)
477+
elif "internal_task" in span.get("description", ""):
478+
internal_spans.append(span)
479+
480+
assert (
481+
len(user_spans) > 0
482+
), f"User task should have been traced. All spans: {[s.get('description') for s in transaction.get('spans', [])]}"
483+
assert (
484+
len(internal_spans) == 0
485+
), f"Internal task should NOT have been traced. All spans: {[s.get('description') for s in transaction.get('spans', [])]}"
486+
487+
finally:
488+
# Restore original task factory to avoid interfering with other tests
489+
loop.set_task_factory(original_task_factory)

0 commit comments

Comments
 (0)