Skip to content

Commit 7ef3b6a

Browse files
new tests with different config settings
1 parent 576b1ea commit 7ef3b6a

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

ddtrace/profiling/collector/_lock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ def _maybe_update_self_name(self) -> None:
238238
if config.enable_asserts:
239239
frame: FrameType = sys._getframe(1)
240240
if frame.f_code.co_name not in ACQUIRE_RELEASE_CO_NAMES:
241-
raise AssertionError("Unexpected frame %s" % frame.f_code.co_name)
241+
raise AssertionError(f"Unexpected frame in stack: '{frame.f_code.co_name}'")
242242

243243
frame = sys._getframe(2)
244244
if frame.f_code.co_name not in ENTER_EXIT_CO_NAMES:
245-
raise AssertionError("Unexpected frame %s" % frame.f_code.co_name)
245+
raise AssertionError(f"Unexpected frame in stack: '{frame.f_code.co_name}'")
246246

247247
# First, look at the local variables of the caller frame, and then the global variables
248248
frame = sys._getframe(3)

tests/profiling_v2/collector/test_threading.py

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,67 @@ def validate_and_cleanup() -> None:
360360
validate_and_cleanup()
361361

362362

363+
@pytest.mark.subprocess(env=dict(DD_PROFILING_ENABLE_ASSERTS="true"))
364+
def test_assertion_error_raised_with_enable_asserts():
365+
"""Ensure that AssertionError is propagated when config.enable_asserts=True."""
366+
import threading
367+
368+
import mock
369+
import pytest
370+
371+
from ddtrace.internal.datadog.profiling import ddup
372+
from ddtrace.profiling.collector.threading import ThreadingLockCollector
373+
374+
# Initialize ddup (required before using collectors)
375+
assert ddup.is_available, "ddup is not available"
376+
ddup.config(env="test", service="test_asserts", version="1.0", output_filename="/tmp/test_asserts")
377+
ddup.start()
378+
379+
with ThreadingLockCollector(capture_pct=100):
380+
lock = threading.Lock()
381+
382+
# Patch _maybe_update_self_name to raise AssertionError
383+
lock._maybe_update_self_name = mock.Mock(side_effect=AssertionError("test: unexpected frame in stack"))
384+
385+
with pytest.raises(AssertionError):
386+
# AssertionError should be propagated when enable_asserts=True
387+
lock.acquire()
388+
389+
390+
@pytest.mark.subprocess(env=dict(DD_PROFILING_ENABLE_ASSERTS="false"))
391+
def test_all_exceptions_suppressed_by_default() -> None:
392+
"""Ensure that exceptions are silently suppressed in the `_acquire` method when config.enable_asserts=False (default)."""
393+
import threading
394+
395+
import mock
396+
397+
from ddtrace.internal.datadog.profiling import ddup
398+
from ddtrace.profiling.collector.threading import ThreadingLockCollector
399+
400+
# Initialize ddup (required before using collectors)
401+
assert ddup.is_available, "ddup is not available"
402+
ddup.config(env="test", service="test_exceptions", version="1.0", output_filename="/tmp/test_exceptions")
403+
ddup.start()
404+
405+
with ThreadingLockCollector(capture_pct=100):
406+
lock = threading.Lock()
407+
408+
# Patch _maybe_update_self_name to raise AssertionError
409+
lock._maybe_update_self_name = mock.Mock(side_effect=AssertionError("Unexpected frame in stack: 'fubar'"))
410+
lock.acquire()
411+
lock.release()
412+
413+
# Patch _maybe_update_self_name to raise RuntimeError
414+
lock._maybe_update_self_name = mock.Mock(side_effect=RuntimeError("Some profiling error"))
415+
lock.acquire()
416+
lock.release()
417+
418+
# Patch _maybe_update_self_name to raise Exception
419+
lock._maybe_update_self_name = mock.Mock(side_effect=Exception("Wut happened?!?!"))
420+
lock.acquire()
421+
lock.release()
422+
423+
363424
class BaseThreadingLockCollectorTest:
364425
# These should be implemented by child classes
365426
@property
@@ -979,32 +1040,6 @@ def test_upload_resets_profile(self) -> None:
9791040
with pytest.raises(AssertionError):
9801041
pprof_utils.parse_newest_profile(self.output_filename)
9811042

982-
def test_assertion_exceptions_raised_in_acquire(self) -> None:
983-
"""Ensure that AssertionError exceptions is propagated in the `_acquire` method."""
984-
985-
with self.collector_class(capture_pct=100):
986-
lock = self.lock_class()
987-
988-
# Patch _maybe_update_self_name to raise AssertionError
989-
lock._maybe_update_self_name = mock.Mock(side_effect=AssertionError("test: unexpected frame in stack"))
990-
991-
with pytest.raises(AssertionError):
992-
# acquire() will propagate AssertionError if _maybe_update_self_name raises it
993-
lock.acquire()
994-
995-
def test_non_assertion_exceptions_suppressed_in_acquire(self) -> None:
996-
"""Ensure that non - AssertionError exceptions are silently suppressed in the `_acquire` method."""
997-
998-
with self.collector_class(capture_pct=100):
999-
lock = self.lock_class()
1000-
1001-
# Patch _maybe_update_self_name to raise RuntimeError
1002-
lock._maybe_update_self_name = mock.Mock(side_effect=RuntimeError("Some profiling error"))
1003-
1004-
# RuntimeError should be caught and suppressed - lock operations should succeed
1005-
lock.acquire()
1006-
lock.release()
1007-
10081043

10091044
class TestThreadingLockCollector(BaseThreadingLockCollectorTest):
10101045
"""Test Lock profiling"""

0 commit comments

Comments
 (0)