Skip to content

Commit d6fc8b8

Browse files
committed
Add more tests
1 parent 8500f75 commit d6fc8b8

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,7 @@ def test_py_thread_id(self):
28602860
# gh-112535: Test _Py_ThreadId(): make sure that thread identifiers
28612861
# in a few threads are unique
28622862
py_thread_id = _testinternalcapi.py_thread_id
2863+
short_sleep = 0.010
28632864

28642865
class GetThreadId(threading.Thread):
28652866
def __init__(self):
@@ -2873,6 +2874,8 @@ def run(self):
28732874
self.started_lock.set()
28742875
self.get_lock.acquire()
28752876
self.py_tid = py_thread_id()
2877+
time.sleep(short_sleep)
2878+
self.py_tid2 = py_thread_id()
28762879

28772880
nthread = 5
28782881
threads = [GetThreadId() for _ in range(nthread)]
@@ -2894,10 +2897,14 @@ def run(self):
28942897
for thread in threads:
28952898
thread.join()
28962899
py_thread_ids.append(thread.py_tid)
2897-
for tid in py_thread_ids:
2898-
self.assertIsInstance(tid, int)
2900+
# _PyThread_Id() should not change for a given thread.
2901+
# For example, it should remain the same after a short sleep.
2902+
self.assertEqual(thread.py_tid2, thread.py_tid)
28992903

29002904
# make sure that all _Py_ThreadId() are unique
2905+
for tid in py_thread_ids:
2906+
self.assertIsInstance(tid, int)
2907+
self.assertGreater(tid, 0)
29012908
self.assertEqual(len(set(py_thread_ids)), len(py_thread_ids),
29022909
py_thread_ids)
29032910

0 commit comments

Comments
 (0)