Debian Codegraph @

Lib/test/test_free_threading/test_gc.py

raw
1 import unittest
2
3 import threading
4 from threading import Thread
5 from unittest import TestCase
6 import gc
7
8 from test.support import threading_helper
9
10
11 class MyObj:
12 pass
13
14
15 @threading_helper.requires_working_threading()
16 class TestGC(TestCase):
17 def test_get_objects(self):
18 event = threading.Event()
19
20 def gc_thread():
21 for i in range(100):
22 o = gc.get_objects()
23 event.set()
24
25 def mutator_thread():
26 while not event.is_set():
27 o1 = MyObj()
28 o2 = MyObj()
29 o3 = MyObj()
30 o4 = MyObj()
31
32 gcs = [Thread(target=gc_thread)]
33 mutators = [Thread(target=mutator_thread) for _ in range(4)]
34 with threading_helper.start_threads(gcs + mutators):
35 pass
36
37 def test_get_referrers(self):
38 event = threading.Event()
39
40 obj = MyObj()
41
42 def gc_thread():
43 for i in range(100):
44 o = gc.get_referrers(obj)
45 event.set()
46
47 def mutator_thread():
48 while not event.is_set():
49 d1 = { "key": obj }
50 d2 = { "key": obj }
51 d3 = { "key": obj }
52 d4 = { "key": obj }
53
54 gcs = [Thread(target=gc_thread) for _ in range(2)]
55 mutators = [Thread(target=mutator_thread) for _ in range(4)]
56 with threading_helper.start_threads(gcs + mutators):
57 pass
58
59
60 if __name__ == "__main__":
61 unittest.main()