33import heapq
44
55from enum import Enum
6- from threading import Thread , Barrier
76from random import shuffle , randint
87
98from test .support import threading_helper
9+ from test .test_free_threading .test_ft import run_concurrently
1010from test import test_heapq
1111
1212
@@ -28,7 +28,7 @@ def test_racing_heapify(self):
2828 heap = list (range (OBJECT_COUNT ))
2929 shuffle (heap )
3030
31- self . run_concurrently (
31+ run_concurrently (
3232 worker_func = heapq .heapify , args = (heap ,), nthreads = NTHREADS
3333 )
3434 self .test_heapq .check_invariant (heap )
@@ -40,7 +40,7 @@ def heappush_func(heap):
4040 for item in reversed (range (OBJECT_COUNT )):
4141 heapq .heappush (heap , item )
4242
43- self . run_concurrently (
43+ run_concurrently (
4444 worker_func = heappush_func , args = (heap ,), nthreads = NTHREADS
4545 )
4646 self .test_heapq .check_invariant (heap )
@@ -61,7 +61,7 @@ def heappop_func(heap, pop_count):
6161 # Each local list should be sorted
6262 self .assertTrue (self .is_sorted_ascending (local_list ))
6363
64- self . run_concurrently (
64+ run_concurrently (
6565 worker_func = heappop_func ,
6666 args = (heap , per_thread_pop_count ),
6767 nthreads = NTHREADS ,
@@ -77,7 +77,7 @@ def heappushpop_func(heap, pushpop_items):
7777 popped_item = heapq .heappushpop (heap , item )
7878 self .assertTrue (popped_item <= item )
7979
80- self . run_concurrently (
80+ run_concurrently (
8181 worker_func = heappushpop_func ,
8282 args = (heap , pushpop_items ),
8383 nthreads = NTHREADS ,
@@ -93,7 +93,7 @@ def heapreplace_func(heap, replace_items):
9393 for item in replace_items :
9494 heapq .heapreplace (heap , item )
9595
96- self . run_concurrently (
96+ run_concurrently (
9797 worker_func = heapreplace_func ,
9898 args = (heap , replace_items ),
9999 nthreads = NTHREADS ,
@@ -105,7 +105,7 @@ def test_racing_heapify_max(self):
105105 max_heap = list (range (OBJECT_COUNT ))
106106 shuffle (max_heap )
107107
108- self . run_concurrently (
108+ run_concurrently (
109109 worker_func = heapq .heapify_max , args = (max_heap ,), nthreads = NTHREADS
110110 )
111111 self .test_heapq .check_max_invariant (max_heap )
@@ -117,7 +117,7 @@ def heappush_max_func(max_heap):
117117 for item in range (OBJECT_COUNT ):
118118 heapq .heappush_max (max_heap , item )
119119
120- self . run_concurrently (
120+ run_concurrently (
121121 worker_func = heappush_max_func , args = (max_heap ,), nthreads = NTHREADS
122122 )
123123 self .test_heapq .check_max_invariant (max_heap )
@@ -138,7 +138,7 @@ def heappop_max_func(max_heap, pop_count):
138138 # Each local list should be sorted
139139 self .assertTrue (self .is_sorted_descending (local_list ))
140140
141- self . run_concurrently (
141+ run_concurrently (
142142 worker_func = heappop_max_func ,
143143 args = (max_heap , per_thread_pop_count ),
144144 nthreads = NTHREADS ,
@@ -154,7 +154,7 @@ def heappushpop_max_func(max_heap, pushpop_items):
154154 popped_item = heapq .heappushpop_max (max_heap , item )
155155 self .assertTrue (popped_item >= item )
156156
157- self . run_concurrently (
157+ run_concurrently (
158158 worker_func = heappushpop_max_func ,
159159 args = (max_heap , pushpop_items ),
160160 nthreads = NTHREADS ,
@@ -170,7 +170,7 @@ def heapreplace_max_func(max_heap, replace_items):
170170 for item in replace_items :
171171 heapq .heapreplace_max (max_heap , item )
172172
173- self . run_concurrently (
173+ run_concurrently (
174174 worker_func = heapreplace_max_func ,
175175 args = (max_heap , replace_items ),
176176 nthreads = NTHREADS ,
@@ -214,27 +214,6 @@ def create_random_list(a, b, size):
214214 """
215215 return [randint (- a , b ) for _ in range (size )]
216216
217- def run_concurrently (self , worker_func , args , nthreads ):
218- """
219- Run the worker function concurrently in multiple threads.
220- """
221- barrier = Barrier (nthreads )
222-
223- def wrapper_func (* args ):
224- # Wait for all threads to reach this point before proceeding.
225- barrier .wait ()
226- worker_func (* args )
227-
228- with threading_helper .catch_threading_exception () as cm :
229- workers = (
230- Thread (target = wrapper_func , args = args ) for _ in range (nthreads )
231- )
232- with threading_helper .start_threads (workers ):
233- pass
234-
235- # Worker threads should not raise any exceptions
236- self .assertIsNone (cm .exc_value )
237-
238217
239218if __name__ == "__main__" :
240219 unittest .main ()
0 commit comments