| 
									
										
										
										
											2024-10-24 09:33:11 -04:00
										 |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import threading | 
					
						
							|  |  |  | from threading import Thread | 
					
						
							|  |  |  | from unittest import TestCase | 
					
						
							|  |  |  | import gc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from test.support import threading_helper | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MyObj: | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @threading_helper.requires_working_threading() | 
					
						
							|  |  |  | class TestGC(TestCase): | 
					
						
							|  |  |  |     def test_get_objects(self): | 
					
						
							|  |  |  |         event = threading.Event() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gc_thread(): | 
					
						
							|  |  |  |             for i in range(100): | 
					
						
							|  |  |  |                 o = gc.get_objects() | 
					
						
							|  |  |  |             event.set() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def mutator_thread(): | 
					
						
							|  |  |  |             while not event.is_set(): | 
					
						
							|  |  |  |                 o1 = MyObj() | 
					
						
							|  |  |  |                 o2 = MyObj() | 
					
						
							|  |  |  |                 o3 = MyObj() | 
					
						
							|  |  |  |                 o4 = MyObj() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         gcs = [Thread(target=gc_thread)] | 
					
						
							|  |  |  |         mutators = [Thread(target=mutator_thread) for _ in range(4)] | 
					
						
							|  |  |  |         with threading_helper.start_threads(gcs + mutators): | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_referrers(self): | 
					
						
							| 
									
										
										
										
											2025-03-26 12:08:20 -04:00
										 |  |  |         NUM_GC = 2 | 
					
						
							|  |  |  |         NUM_MUTATORS = 4 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         b = threading.Barrier(NUM_GC + NUM_MUTATORS) | 
					
						
							| 
									
										
										
										
											2024-10-24 09:33:11 -04:00
										 |  |  |         event = threading.Event() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         obj = MyObj() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def gc_thread(): | 
					
						
							| 
									
										
										
										
											2025-03-26 12:08:20 -04:00
										 |  |  |             b.wait() | 
					
						
							| 
									
										
										
										
											2024-10-24 09:33:11 -04:00
										 |  |  |             for i in range(100): | 
					
						
							|  |  |  |                 o = gc.get_referrers(obj) | 
					
						
							|  |  |  |             event.set() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def mutator_thread(): | 
					
						
							| 
									
										
										
										
											2025-03-26 12:08:20 -04:00
										 |  |  |             b.wait() | 
					
						
							| 
									
										
										
										
											2024-10-24 09:33:11 -04:00
										 |  |  |             while not event.is_set(): | 
					
						
							|  |  |  |                 d1 = { "key": obj } | 
					
						
							|  |  |  |                 d2 = { "key": obj } | 
					
						
							|  |  |  |                 d3 = { "key": obj } | 
					
						
							|  |  |  |                 d4 = { "key": obj } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-26 12:08:20 -04:00
										 |  |  |         gcs = [Thread(target=gc_thread) for _ in range(NUM_GC)] | 
					
						
							|  |  |  |         mutators = [Thread(target=mutator_thread) for _ in range(NUM_MUTATORS)] | 
					
						
							| 
									
										
										
										
											2024-10-24 09:33:11 -04:00
										 |  |  |         with threading_helper.start_threads(gcs + mutators): | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     unittest.main() |