File tree Expand file tree Collapse file tree 2 files changed +36
-33
lines changed Expand file tree Collapse file tree 2 files changed +36
-33
lines changed Original file line number Diff line number Diff line change 6
6
from threading import Thread , Barrier
7
7
from random import shuffle , randint
8
8
9
- from test .support import threading_helper
9
+ from test .support import threading_helper , Py_GIL_DISABLED
10
10
from test import test_heapq
11
11
12
12
@@ -178,6 +178,40 @@ def heapreplace_max_func(max_heap, replace_items):
178
178
self .assertEqual (len (max_heap ), OBJECT_COUNT )
179
179
self .test_heapq .check_max_invariant (max_heap )
180
180
181
+ @unittest .skipUnless (Py_GIL_DISABLED , 'only used to test under free-threaded build' )
182
+ def test_lock_free_list_read (self ):
183
+ n , n_threads = 1_000_000 , 10
184
+ l = []
185
+ barrier = Barrier (n_threads * 2 )
186
+
187
+ def writer ():
188
+ barrier .wait ()
189
+ for i in range (n ):
190
+ heapq .heappush (l , 1 )
191
+ heapq .heappop (l )
192
+
193
+ def reader ():
194
+ barrier .wait ()
195
+ for i in range (n ):
196
+ try :
197
+ l [0 ]
198
+ except IndexError :
199
+ pass
200
+
201
+ import threading
202
+ threads = []
203
+ with threading_helper .catch_threading_exception () as cm :
204
+ for _ in range (n_threads ):
205
+ t1 = threading .Thread (target = writer )
206
+ t2 = threading .Thread (target = reader )
207
+ threads .append (t1 )
208
+ threads .append (t2 )
209
+ t1 .start ()
210
+ t2 .start ()
211
+
212
+ for t in threads :
213
+ t .join ()
214
+
181
215
@staticmethod
182
216
def is_sorted_ascending (lst ):
183
217
"""
Original file line number Diff line number Diff line change 5
5
import doctest
6
6
7
7
from test .support import import_helper
8
- from unittest import TestCase , skipUnless , skipIf
8
+ from unittest import TestCase , skipUnless
9
9
from operator import itemgetter
10
10
11
-
12
11
py_heapq = import_helper .import_fresh_module ('heapq' , blocked = ['_heapq' ])
13
12
c_heapq = import_helper .import_fresh_module ('heapq' , fresh = ['_heapq' ])
14
13
@@ -403,36 +402,6 @@ def __le__(self, other):
403
402
self .assertEqual (hsort (data , LT ), target )
404
403
self .assertRaises (TypeError , data , LE )
405
404
406
- @skipIf (py_heapq , 'only used to test c_heapq' )
407
- def test_lock_free_list_read (self ):
408
- n = 1_000_000
409
- l = []
410
- def writer ():
411
- for i in range (n ):
412
- self .module .heappush (l , 1 )
413
- self .module .heappop (l )
414
-
415
- def reader ():
416
- for i in range (n ):
417
- try :
418
- l [0 ]
419
- except IndexError :
420
- pass
421
-
422
- import threading
423
- threads = []
424
- for _ in range (10 ):
425
- t1 = threading .Thread (target = writer )
426
- t2 = threading .Thread (target = reader )
427
- threads .append (t1 )
428
- threads .append (t2 )
429
- t1 .start ()
430
- t2 .start ()
431
-
432
- for t in threads :
433
- t .join ()
434
-
435
-
436
405
437
406
class TestHeapPython (TestHeap , TestCase ):
438
407
module = py_heapq
You can’t perform that action at this time.
0 commit comments