33import heapq
44
55from enum import Enum
6- from threading import Thread , Barrier , Lock
6+ from threading import Barrier , Lock
77from random import shuffle , randint
88
99from test .support import threading_helper
10+ from test .support .threading_helper import run_concurrently
1011from test import test_heapq
1112
1213
@@ -28,8 +29,8 @@ def test_racing_heapify(self):
2829 heap = list (range (OBJECT_COUNT ))
2930 shuffle (heap )
3031
31- self . run_concurrently (
32- worker_func = heapq .heapify , args = (heap ,), nthreads = NTHREADS
32+ run_concurrently (
33+ worker_func = heapq .heapify , nthreads = NTHREADS , args = (heap ,)
3334 )
3435 self .test_heapq .check_invariant (heap )
3536
@@ -40,8 +41,8 @@ def heappush_func(heap):
4041 for item in reversed (range (OBJECT_COUNT )):
4142 heapq .heappush (heap , item )
4243
43- self . run_concurrently (
44- worker_func = heappush_func , args = (heap ,), nthreads = NTHREADS
44+ run_concurrently (
45+ worker_func = heappush_func , nthreads = NTHREADS , args = (heap ,)
4546 )
4647 self .test_heapq .check_invariant (heap )
4748
@@ -61,10 +62,10 @@ def heappop_func(heap, pop_count):
6162 # Each local list should be sorted
6263 self .assertTrue (self .is_sorted_ascending (local_list ))
6364
64- self . run_concurrently (
65+ run_concurrently (
6566 worker_func = heappop_func ,
66- args = (heap , per_thread_pop_count ),
6767 nthreads = NTHREADS ,
68+ args = (heap , per_thread_pop_count ),
6869 )
6970 self .assertEqual (len (heap ), 0 )
7071
@@ -77,10 +78,10 @@ def heappushpop_func(heap, pushpop_items):
7778 popped_item = heapq .heappushpop (heap , item )
7879 self .assertTrue (popped_item <= item )
7980
80- self . run_concurrently (
81+ run_concurrently (
8182 worker_func = heappushpop_func ,
82- args = (heap , pushpop_items ),
8383 nthreads = NTHREADS ,
84+ args = (heap , pushpop_items ),
8485 )
8586 self .assertEqual (len (heap ), OBJECT_COUNT )
8687 self .test_heapq .check_invariant (heap )
@@ -93,10 +94,10 @@ def heapreplace_func(heap, replace_items):
9394 for item in replace_items :
9495 heapq .heapreplace (heap , item )
9596
96- self . run_concurrently (
97+ run_concurrently (
9798 worker_func = heapreplace_func ,
98- args = (heap , replace_items ),
9999 nthreads = NTHREADS ,
100+ args = (heap , replace_items ),
100101 )
101102 self .assertEqual (len (heap ), OBJECT_COUNT )
102103 self .test_heapq .check_invariant (heap )
@@ -105,8 +106,8 @@ def test_racing_heapify_max(self):
105106 max_heap = list (range (OBJECT_COUNT ))
106107 shuffle (max_heap )
107108
108- self . run_concurrently (
109- worker_func = heapq .heapify_max , args = (max_heap ,), nthreads = NTHREADS
109+ run_concurrently (
110+ worker_func = heapq .heapify_max , nthreads = NTHREADS , args = (max_heap ,)
110111 )
111112 self .test_heapq .check_max_invariant (max_heap )
112113
@@ -117,8 +118,8 @@ def heappush_max_func(max_heap):
117118 for item in range (OBJECT_COUNT ):
118119 heapq .heappush_max (max_heap , item )
119120
120- self . run_concurrently (
121- worker_func = heappush_max_func , args = (max_heap ,), nthreads = NTHREADS
121+ run_concurrently (
122+ worker_func = heappush_max_func , nthreads = NTHREADS , args = (max_heap ,)
122123 )
123124 self .test_heapq .check_max_invariant (max_heap )
124125
@@ -138,10 +139,10 @@ def heappop_max_func(max_heap, pop_count):
138139 # Each local list should be sorted
139140 self .assertTrue (self .is_sorted_descending (local_list ))
140141
141- self . run_concurrently (
142+ run_concurrently (
142143 worker_func = heappop_max_func ,
143- args = (max_heap , per_thread_pop_count ),
144144 nthreads = NTHREADS ,
145+ args = (max_heap , per_thread_pop_count ),
145146 )
146147 self .assertEqual (len (max_heap ), 0 )
147148
@@ -154,10 +155,10 @@ def heappushpop_max_func(max_heap, pushpop_items):
154155 popped_item = heapq .heappushpop_max (max_heap , item )
155156 self .assertTrue (popped_item >= item )
156157
157- self . run_concurrently (
158+ run_concurrently (
158159 worker_func = heappushpop_max_func ,
159- args = (max_heap , pushpop_items ),
160160 nthreads = NTHREADS ,
161+ args = (max_heap , pushpop_items ),
161162 )
162163 self .assertEqual (len (max_heap ), OBJECT_COUNT )
163164 self .test_heapq .check_max_invariant (max_heap )
@@ -170,10 +171,10 @@ def heapreplace_max_func(max_heap, replace_items):
170171 for item in replace_items :
171172 heapq .heapreplace_max (max_heap , item )
172173
173- self . run_concurrently (
174+ run_concurrently (
174175 worker_func = heapreplace_max_func ,
175- args = (max_heap , replace_items ),
176176 nthreads = NTHREADS ,
177+ args = (max_heap , replace_items ),
177178 )
178179 self .assertEqual (len (max_heap ), OBJECT_COUNT )
179180 self .test_heapq .check_max_invariant (max_heap )
@@ -203,7 +204,7 @@ def worker():
203204 except IndexError :
204205 pass
205206
206- self . run_concurrently (worker , () , n_threads * 2 )
207+ run_concurrently (worker , n_threads * 2 )
207208
208209 @staticmethod
209210 def is_sorted_ascending (lst ):
@@ -241,27 +242,6 @@ def create_random_list(a, b, size):
241242 """
242243 return [randint (- a , b ) for _ in range (size )]
243244
244- def run_concurrently (self , worker_func , args , nthreads ):
245- """
246- Run the worker function concurrently in multiple threads.
247- """
248- barrier = Barrier (nthreads )
249-
250- def wrapper_func (* args ):
251- # Wait for all threads to reach this point before proceeding.
252- barrier .wait ()
253- worker_func (* args )
254-
255- with threading_helper .catch_threading_exception () as cm :
256- workers = (
257- Thread (target = wrapper_func , args = args ) for _ in range (nthreads )
258- )
259- with threading_helper .start_threads (workers ):
260- pass
261-
262- # Worker threads should not raise any exceptions
263- self .assertIsNone (cm .exc_value )
264-
265245
266246if __name__ == "__main__" :
267247 unittest .main ()
0 commit comments