11import unittest
22from test .support import import_helper , threading_helper
33import random
4+ from threading import Thread , Barrier
45
56py_bisect = import_helper .import_fresh_module ('bisect' , blocked = ['_bisect' ])
67c_bisect = import_helper .import_fresh_module ('bisect' , fresh = ['_bisect' ])
@@ -18,7 +19,7 @@ def insert(data):
1819 insert_method (data , x )
1920
2021 data = list (range (OBJECT_COUNT ))
21- threading_helper .run_concurrently (
22+ self .run_concurrently (
2223 worker_func = insert , args = (data ,), nthreads = NTHREADS
2324 )
2425 if False :
@@ -41,6 +42,28 @@ def is_sorted_ascending(lst):
4142 """
4243 return all (lst [i - 1 ] <= lst [i ] for i in range (1 , len (lst )))
4344
45+ def run_concurrently (self , worker_func , args , nthreads ):
46+ """
47+ Run the worker function concurrently in multiple threads.
48+ """
49+ barrier = Barrier (nthreads )
50+
51+ def wrapper_func (* args ):
52+ # Wait for all threads to reach this point before proceeding.
53+ barrier .wait ()
54+ worker_func (* args )
55+
56+ with threading_helper .catch_threading_exception () as cm :
57+ workers = (
58+ Thread (target = wrapper_func , args = args )
59+ for _ in range (nthreads )
60+ )
61+ with threading_helper .start_threads (workers ):
62+ pass
63+
64+ # Worker threads should not raise any exceptions
65+ self .assertIsNone (cm .exc_value )
66+
4467
4568@threading_helper .requires_working_threading ()
4669class TestPyBisect (unittest .TestCase , TestBase ):
0 commit comments