Debian Codegraph @

Lib/test/test_free_threading/test_dict_watcher.py

raw
1 import unittest
2
3 from test.support import import_helper, threading_helper
4
5 _testcapi = import_helper.import_module("_testcapi")
6
7 ITERS = 100
8 NTHREADS = 20
9
10
11 @threading_helper.requires_working_threading()
12 class TestDictWatcherThreadSafety(unittest.TestCase):
13 # Watcher kinds from _testcapi
14 EVENTS = 0 # appends dict events as strings to global event list
15
16 def test_concurrent_add_clear_watchers(self):
17 """Race AddWatcher and ClearWatcher from multiple threads.
18
19 Uses more threads than available watcher slots (5 user slots out
20 of DICT_MAX_WATCHERS=8).
21 """
22 results = []
23
24 def worker():
25 for _ in range(ITERS):
26 try:
27 wid = _testcapi.add_dict_watcher(self.EVENTS)
28 except RuntimeError:
29 continue # All slots taken
30 self.assertGreaterEqual(wid, 0)
31 results.append(wid)
32 _testcapi.clear_dict_watcher(wid)
33
34 threading_helper.run_concurrently(worker, NTHREADS)
35
36 # Verify at least some watchers were successfully added
37 self.assertGreater(len(results), 0)
38
39 def test_concurrent_watch_unwatch(self):
40 """Race Watch and Unwatch on the same dict from multiple threads."""
41 wid = _testcapi.add_dict_watcher(self.EVENTS)
42 dicts = [{} for _ in range(10)]
43
44 def worker():
45 for _ in range(ITERS):
46 for d in dicts:
47 _testcapi.watch_dict(wid, d)
48 for d in dicts:
49 _testcapi.unwatch_dict(wid, d)
50
51 try:
52 threading_helper.run_concurrently(worker, NTHREADS)
53
54 # Verify watching still works after concurrent watch/unwatch
55 _testcapi.watch_dict(wid, dicts[0])
56 dicts[0]["key"] = "value"
57 events = _testcapi.get_dict_watcher_events()
58 self.assertIn("new:key:value", events)
59 finally:
60 _testcapi.clear_dict_watcher(wid)
61
62 def test_concurrent_modify_watched_dict(self):
63 """Race dict mutations (triggering callbacks) with watch/unwatch."""
64 wid = _testcapi.add_dict_watcher(self.EVENTS)
65 d = {}
66 _testcapi.watch_dict(wid, d)
67
68 def mutator():
69 for i in range(ITERS):
70 d[f"key_{i}"] = i
71 d.pop(f"key_{i}", None)
72
73 def toggler():
74 for i in range(ITERS):
75 _testcapi.watch_dict(wid, d)
76 d[f"toggler_{i}"] = i
77 _testcapi.unwatch_dict(wid, d)
78
79 workers = [mutator, toggler] * (NTHREADS // 2)
80 try:
81 threading_helper.run_concurrently(workers)
82 events = _testcapi.get_dict_watcher_events()
83 self.assertGreater(len(events), 0)
84 finally:
85 _testcapi.clear_dict_watcher(wid)
86
87
88 if __name__ == "__main__":
89 unittest.main()