44Validates RLock collector functionality and integration points.
55"""
66
7- import threading
87import sys
9- import os
8+ import threading
109import time
1110
11+
1212# Add the project to Python path
13- sys .path .insert (0 , '/Users/vlad.scherbich/go/src/github.com/DataDog/dd-trace-py-2' )
13+ sys .path .insert (0 , "/Users/vlad.scherbich/go/src/github.com/DataDog/dd-trace-py-2" )
14+
1415
1516def test_rlock_import_and_creation ():
1617 """Test that RLock collector can be imported and created"""
1718 print ("=== RLock Import and Creation Test ===" )
18-
19+
1920 try :
2021 from ddtrace .profiling .collector import threading as collector_threading
22+
2123 print ("β
Successfully imported ThreadingRLockCollector" )
22-
24+
2325 # Create collectors
2426 lock_collector = collector_threading .ThreadingLockCollector ()
2527 rlock_collector = collector_threading .ThreadingRLockCollector ()
26-
28+
2729 print (f"β
Lock collector created: { type (lock_collector )} " )
2830 print (f"β
RLock collector created: { type (rlock_collector )} " )
29-
31+
3032 return True
31-
33+
3234 except Exception as e :
3335 print (f"β Import/creation failed: { e } " )
3436 return False
3537
38+
3639def test_rlock_behavior_patterns ():
3740 """Test RLock reentrant behavior patterns (without profiler active)"""
3841 print ("\n === RLock Behavior Patterns Test ===" )
39-
42+
4043 try :
4144 # Test standard RLock reentrant behavior
4245 rlock = threading .RLock ()
4346 results = []
44-
47+
4548 def test_reentrant_pattern ():
4649 """Test multi-level reentrant acquisition"""
4750 with rlock :
@@ -51,51 +54,52 @@ def test_reentrant_pattern():
5154 with rlock : # Double reentrant
5255 results .append ("Level 3" )
5356 time .sleep (0.01 )
54-
57+
5558 # Test with multiple threads
5659 threads = []
5760 for i in range (2 ):
5861 t = threading .Thread (target = test_reentrant_pattern , name = f"Thread-{ i } " )
5962 threads .append (t )
60-
63+
6164 for t in threads :
6265 t .start ()
63-
66+
6467 for t in threads :
6568 t .join ()
66-
69+
6770 expected_results = 2 * 3 # 2 threads Γ 3 levels each
6871 print (f"Completed { len (results )} reentrant operations" )
6972 print (f"Expected { expected_results } operations" )
70-
73+
7174 success = len (results ) == expected_results
7275 if success :
7376 print ("β
RLock reentrant behavior works correctly!" )
7477 else :
7578 print (f"β οΈ Unexpected result count: { len (results )} vs { expected_results } " )
76-
79+
7780 return success
78-
81+
7982 except Exception as e :
8083 print (f"β RLock behavior test failed: { e } " )
8184 return False
8285
86+
8387def test_lock_vs_rlock_differences ():
8488 """Test the key differences between Lock and RLock"""
8589 print ("\n === Lock vs RLock Differences Test ===" )
86-
90+
8791 try :
8892 lock = threading .Lock ()
8993 rlock = threading .RLock ()
90-
94+
9195 print (f"Lock type: { type (lock )} " )
9296 print (f"RLock type: { type (rlock )} " )
93-
97+
9498 # Test Lock (non-reentrant)
9599 print ("Testing Lock (non-reentrant)..." )
96100 with lock :
97101 print (" Lock acquired and released successfully" )
98-
102+
99103 # Test RLock (reentrant)
100104 print ("Testing RLock (reentrant)..." )
101105 with rlock :
@@ -107,116 +111,120 @@ def test_lock_vs_rlock_differences():
107111 print (" RLock level 3 released" )
108112 print (" RLock level 2 released" )
109113 print (" RLock level 1 released" )
110-
114+
111115 print ("β
Lock vs RLock behavior differences confirmed!" )
112116 return True
113-
117+
114118 except Exception as e :
115119 print (f"β Lock vs RLock test failed: { e } " )
116120 return False
117121
122+
118123def test_threading_module_integration ():
119124 """Test integration with threading module"""
120125 print ("\n === Threading Module Integration Test ===" )
121-
126+
122127 try :
123128 # Verify we can create locks normally
124129 locks_created = []
125-
130+
126131 # Create various lock types
127132 regular_lock = threading .Lock ()
128133 reentrant_lock = threading .RLock ()
129134 condition = threading .Condition ()
130135 semaphore = threading .Semaphore ()
131-
132- locks_created .extend ([
133- ("Lock" , regular_lock ),
134- ("RLock" , reentrant_lock ),
135- ("Condition" , condition ),
136- ("Semaphore" , semaphore )
137- ])
138-
136+
137+ locks_created .extend (
138+ [("Lock" , regular_lock ), ("RLock" , reentrant_lock ), ("Condition" , condition ), ("Semaphore" , semaphore )]
139+ )
140+
139141 print ("Created lock types:" )
140142 for name , lock_obj in locks_created :
141143 print (f" { name } : { type (lock_obj )} " )
142-
144+
143145 # Test basic functionality
144146 print ("Testing basic functionality..." )
145-
147+
146148 with regular_lock :
147149 print (" Regular Lock works" )
148-
150+
149151 with reentrant_lock :
150152 with reentrant_lock : # Reentrant
151153 print (" RLock reentrant functionality works" )
152-
154+
153155 with condition :
154156 print (" Condition works" )
155-
157+
156158 with semaphore :
157159 print (" Semaphore works" )
158-
160+
159161 print ("β
Threading module integration successful!" )
160162 return True
161-
163+
162164 except Exception as e :
163165 print (f"β Threading integration test failed: { e } " )
164166 return False
165167
168+
166169def test_profiler_readiness ():
167170 """Test that the environment is ready for profiling"""
168171 print ("\n === Profiler Readiness Test ===" )
169-
172+
170173 try :
171174 # Test imports
172- import ddtrace
175+ import ddtrace # noqa: F401
176+
173177 print ("β
ddtrace imports successfully" )
174-
178+
175179 from ddtrace .profiling .collector import threading as collector_threading
180+
176181 print ("β
Threading collector imports successfully" )
177-
178- from ddtrace .profiling .collector import _lock
182+
183+ from ddtrace .profiling .collector import _lock # noqa: F401
184+
179185 print ("β
Lock collector base imports successfully" )
180-
186+
181187 # Test collector classes exist
182188 lock_collector_class = collector_threading .ThreadingLockCollector
183189 rlock_collector_class = collector_threading .ThreadingRLockCollector
184-
190+
185191 print (f"β
Lock collector class: { lock_collector_class } " )
186192 print (f"β
RLock collector class: { rlock_collector_class } " )
187-
193+
188194 # Test profiled lock classes exist
189195 profiled_lock_class = collector_threading ._ProfiledThreadingLock
190196 profiled_rlock_class = collector_threading ._ProfiledThreadingRLock
191-
197+
192198 print (f"β
Profiled Lock class: { profiled_lock_class } " )
193199 print (f"β
Profiled RLock class: { profiled_rlock_class } " )
194-
200+
195201 print ("β
Environment is ready for RLock profiling!" )
196202 return True
197-
203+
198204 except Exception as e :
199205 print (f"β Profiler readiness test failed: { e } " )
200206 import traceback
207+
201208 traceback .print_exc ()
202209 return False
203210
211+
204212if __name__ == "__main__" :
205213 print ("π E2E RLock Profiling Validation" )
206214 print ("=" * 50 )
207215 print ("This test validates RLock profiling integration" )
208216 print ("and core functionality." )
209217 print ("=" * 50 )
210218 print ()
211-
219+
212220 try :
213221 # Run test suites
214222 test1_passed = test_rlock_import_and_creation ()
215223 test2_passed = test_rlock_behavior_patterns ()
216224 test3_passed = test_lock_vs_rlock_differences ()
217225 test4_passed = test_threading_module_integration ()
218226 test5_passed = test_profiler_readiness ()
219-
227+
220228 print (f"\n { '=' * 50 } " )
221229 print ("π FINAL RESULTS" )
222230 print (f"{ '=' * 50 } " )
@@ -225,19 +233,20 @@ def test_profiler_readiness():
225233 print (f"Lock vs RLock differences: { 'β
PASS' if test3_passed else 'β FAIL' } " )
226234 print (f"Threading module integration: { 'β
PASS' if test4_passed else 'β FAIL' } " )
227235 print (f"Profiler readiness: { 'β
PASS' if test5_passed else 'β FAIL' } " )
228-
236+
229237 all_passed = all ([test1_passed , test2_passed , test3_passed , test4_passed , test5_passed ])
230-
238+
231239 if all_passed :
232- print (f "\n π ALL E2E TESTS PASSED!" )
240+ print ("\n π ALL E2E TESTS PASSED!" )
233241 print ("RLock profiling implementation is ready!" )
234242 else :
235- print (f "\n β οΈ Some tests had issues." )
236-
243+ print ("\n β οΈ Some tests had issues." )
244+
237245 print (f"\n { '=' * 50 } " )
238246 print ("E2E validation complete!" )
239-
247+
240248 except Exception as e :
241249 print (f"\n π₯ Tests failed with exception: { e } " )
242250 import traceback
251+
243252 traceback .print_exc ()
0 commit comments