Skip to content

Commit 28cfd59

Browse files
committed
add test
1 parent 3689e40 commit 28cfd59

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Lib/test/test_heapq.py

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

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

11+
1112
py_heapq = import_helper.import_fresh_module('heapq', blocked=['_heapq'])
1213
c_heapq = import_helper.import_fresh_module('heapq', fresh=['_heapq'])
1314

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

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+
405436

406437
class TestHeapPython(TestHeap, TestCase):
407438
module = py_heapq

0 commit comments

Comments
 (0)