Skip to content

Commit 463963d

Browse files
committed
update tests
1 parent 1fcb426 commit 463963d

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

Lib/test/test_free_threading/test_heapq.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from threading import Thread, Barrier
77
from random import shuffle, randint
88

9-
from test.support import threading_helper
9+
from test.support import threading_helper, Py_GIL_DISABLED
1010
from test import test_heapq
1111

1212

@@ -178,6 +178,40 @@ def heapreplace_max_func(max_heap, replace_items):
178178
self.assertEqual(len(max_heap), OBJECT_COUNT)
179179
self.test_heapq.check_max_invariant(max_heap)
180180

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+
181215
@staticmethod
182216
def is_sorted_ascending(lst):
183217
"""

Lib/test/test_heapq.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import doctest
66

77
from test.support import import_helper
8-
from unittest import TestCase, skipUnless, skipIf
8+
from unittest import TestCase, skipUnless
99
from operator import itemgetter
1010

11-
1211
py_heapq = import_helper.import_fresh_module('heapq', blocked=['_heapq'])
1312
c_heapq = import_helper.import_fresh_module('heapq', fresh=['_heapq'])
1413

@@ -403,36 +402,6 @@ def __le__(self, other):
403402
self.assertEqual(hsort(data, LT), target)
404403
self.assertRaises(TypeError, data, LE)
405404

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-
436405

437406
class TestHeapPython(TestHeap, TestCase):
438407
module = py_heapq

0 commit comments

Comments
 (0)