Debian Codegraph @

Lib/test/test_free_threading/test_iteration.py

raw
1 import threading
2 import unittest
3 from test import support
4
5 # The race conditions these tests were written for only happen every now and
6 # then, even with the current numbers. To find rare race conditions, bumping
7 # these up will help, but it makes the test runtime highly variable under
8 # free-threading. Overhead is much higher under ThreadSanitizer, but it's
9 # also much better at detecting certain races, so we don't need as many
10 # items/threads.
11 if support.check_sanitizer(thread=True):
12 NUMITEMS = 1000
13 NUMTHREADS = 2
14 else:
15 NUMITEMS = 5000
16 NUMTHREADS = 5
17 NUMMUTATORS = 2
18
19 class ContendedTupleIterationTest(unittest.TestCase):
20 def make_testdata(self, n):
21 return tuple(range(n))
22
23 def assert_iterator_results(self, results, expected):
24 # Most iterators are not atomic (yet?) so they can skip or duplicate
25 # items, but they should not invent new items (like the range
26 # iterator currently does).
27 extra_items = set(results) - set(expected)
28 self.assertEqual(set(), extra_items)
29
30 def run_threads(self, func, *args, numthreads=NUMTHREADS):
31 threads = []
32 for _ in range(numthreads):
33 t = threading.Thread(target=func, args=args)
34 t.start()
35 threads.append(t)
36 return threads
37
38 def test_iteration(self):
39 """Test iteration over a shared container"""
40 seq = self.make_testdata(NUMITEMS)
41 results = []
42 start = threading.Barrier(NUMTHREADS)
43 def worker():
44 idx = 0
45 start.wait()
46 for item in seq:
47 idx += 1
48 results.append(idx)
49 threads = self.run_threads(worker)
50 for t in threads:
51 t.join()
52 # Each thread has its own iterator, so results should be entirely predictable.
53 self.assertEqual(results, [NUMITEMS] * NUMTHREADS)
54
55 def test_shared_iterator(self):
56 """Test iteration over a shared iterator"""
57 seq = self.make_testdata(NUMITEMS)
58 it = iter(seq)
59 results = []
60 start = threading.Barrier(NUMTHREADS)
61 def worker():
62 items = []
63 start.wait()
64 # We want a tight loop, so put items in the shared list at the end.
65 for item in it:
66 items.append(item)
67 results.extend(items)
68 threads = self.run_threads(worker)
69 for t in threads:
70 t.join()
71 self.assert_iterator_results(results, seq)
72
73 class ContendedListIterationTest(ContendedTupleIterationTest):
74 def make_testdata(self, n):
75 return list(range(n))
76
77 def test_iteration_while_mutating(self):
78 """Test iteration over a shared mutating container."""
79 seq = self.make_testdata(NUMITEMS)
80 results = []
81 start = threading.Barrier(NUMTHREADS + NUMMUTATORS)
82 endmutate = threading.Event()
83 def mutator():
84 orig = seq[:]
85 # Make changes big enough to cause resizing of the list, with
86 # items shifted around for good measure.
87 replacement = (orig * 3)[NUMITEMS//2:]
88 start.wait()
89 while not endmutate.is_set():
90 seq.extend(replacement)
91 seq[:0] = orig
92 seq.__imul__(2)
93 seq.extend(seq)
94 seq[:] = orig
95 def worker():
96 items = []
97 start.wait()
98 # We want a tight loop, so put items in the shared list at the end.
99 for item in seq:
100 items.append(item)
101 results.extend(items)
102 mutators = ()
103 try:
104 threads = self.run_threads(worker)
105 mutators = self.run_threads(mutator, numthreads=NUMMUTATORS)
106 for t in threads:
107 t.join()
108 finally:
109 endmutate.set()
110 for m in mutators:
111 m.join()
112 self.assert_iterator_results(results, list(seq))
113
114
115 class ContendedRangeIterationTest(ContendedTupleIterationTest):
116 def make_testdata(self, n):
117 return range(n)
118
119 def assert_iterator_results(self, results, expected):
120 # Range iterators that are shared between threads will (right now)
121 # sometimes produce items after the end of the range, sometimes
122 # _far_ after the end of the range. That should be fixed, but for
123 # now, let's just check they're integers that could have resulted
124 # from stepping beyond the range bounds.
125 extra_items = set(results) - set(expected)
126 for item in extra_items:
127 self.assertEqual((item - expected.start) % expected.step, 0)