Skip to content

Commit 9b7311f

Browse files
avoid 'test_subclassing_wrapped_lock' test for 'threading.Lock' and 'threadomg/Rlock'
1 parent b96570e commit 9b7311f

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

tests/profiling/collector/test_asyncio.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def teardown_method(self):
5151
except Exception as e:
5252
print("Error while deleting file: ", e)
5353

54-
def test_subclassing_wrapped_lock(self) -> None:
54+
async def test_subclassing_wrapped_lock(self) -> None:
5555
"""Test that subclassing of a wrapped lock type when profiling is active."""
5656
from typing import Optional
5757

@@ -74,13 +74,11 @@ def __init__(self) -> None:
7474
assert custom_lock._owner is None
7575
assert custom_lock._count == 0
7676

77-
async def test_async_lock() -> None:
78-
await custom_lock.acquire()
79-
assert custom_lock.locked()
80-
custom_lock.release()
81-
assert not custom_lock.locked()
82-
83-
asyncio.get_event_loop().run_until_complete(test_async_lock())
77+
# Test async acquire/release
78+
await custom_lock.acquire()
79+
assert custom_lock.locked()
80+
custom_lock.release()
81+
assert not custom_lock.locked()
8482

8583
async def test_asyncio_lock_events(self):
8684
with collector_asyncio.AsyncioLockCollector(capture_pct=100):

tests/profiling/collector/test_threading.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -574,27 +574,6 @@ def __init__(self, lock_class: LockTypeClass) -> None:
574574
# Try this way too
575575
Foobar(self.lock_class)
576576

577-
def test_subclassing_wrapped_lock(self) -> None:
578-
"""Test that subclassing of a wrapped lock type when profiling is active."""
579-
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
580-
581-
with self.collector_class():
582-
assert isinstance(self.lock_class, _LockAllocatorWrapper)
583-
584-
# This should NOT raise TypeError
585-
class CustomLock(self.lock_class): # type: ignore[misc]
586-
def __init__(self) -> None:
587-
super().__init__()
588-
self.custom_attr: str = "test"
589-
590-
# Verify subclassing and functionality
591-
custom_lock: CustomLock = CustomLock()
592-
assert hasattr(custom_lock, "custom_attr")
593-
assert custom_lock.custom_attr == "test"
594-
595-
assert custom_lock.acquire()
596-
custom_lock.release()
597-
598577
def test_lock_events(self) -> None:
599578
# The first argument is the recorder.Recorder which is used for the
600579
# v1 exporter. We don't need it for the v2 exporter.
@@ -1410,6 +1389,32 @@ class BaseSemaphoreTest(BaseThreadingLockCollectorTest):
14101389
particularly those related to internal lock detection and Condition-based implementation.
14111390
"""
14121391

1392+
def test_subclassing_wrapped_lock(self) -> None:
1393+
"""Test that subclassing of a wrapped lock type works when profiling is active.
1394+
1395+
This test is only valid for Semaphore-like types (pure Python classes).
1396+
threading.Lock and threading.RLock are C types that don't support subclassing
1397+
through __mro_entries__.
1398+
"""
1399+
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
1400+
1401+
with self.collector_class():
1402+
assert isinstance(self.lock_class, _LockAllocatorWrapper)
1403+
1404+
# This should NOT raise TypeError
1405+
class CustomLock(self.lock_class): # type: ignore[misc]
1406+
def __init__(self) -> None:
1407+
super().__init__()
1408+
self.custom_attr: str = "test"
1409+
1410+
# Verify subclassing and functionality
1411+
custom_lock: CustomLock = CustomLock()
1412+
assert hasattr(custom_lock, "custom_attr")
1413+
assert custom_lock.custom_attr == "test"
1414+
1415+
assert custom_lock.acquire()
1416+
custom_lock.release()
1417+
14131418
def _verify_no_double_counting(self, marker_name: str, lock_var_name: str) -> None:
14141419
"""Helper method to verify no double-counting in profile output.
14151420

0 commit comments

Comments
 (0)