MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_foreign_thread

Method test_foreign_thread

Lib/test/test_threading.py:268–318  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

266 threading.stack_size(0)
267
268 def test_foreign_thread(self):
269 # Check that a "foreign" thread can use the threading module.
270 dummy_thread = None
271 error = None
272 def f(mutex):
273 try:
274 nonlocal dummy_thread
275 nonlocal error
276 # Calling current_thread() forces an entry for the foreign
277 # thread to get made in the threading._active map.
278 dummy_thread = threading.current_thread()
279 tid = dummy_thread.ident
280 self.assertIn(tid, threading._active)
281 self.assertIsInstance(dummy_thread, threading._DummyThread)
282 self.assertIs(threading._active.get(tid), dummy_thread)
283 # gh-29376
284 self.assertTrue(
285 dummy_thread.is_alive(),
286 'Expected _DummyThread to be considered alive.'
287 )
288 self.assertIn('_DummyThread', repr(dummy_thread))
289 except BaseException as e:
290 error = e
291 finally:
292 mutex.release()
293
294 mutex = threading.Lock()
295 mutex.acquire()
296 with threading_helper.wait_threads_exit():
297 tid = _thread.start_new_thread(f, (mutex,))
298 # Wait for the thread to finish.
299 mutex.acquire()
300 if error is not None:
301 raise error
302 self.assertEqual(tid, dummy_thread.ident)
303 # Issue gh-106236:
304 with self.assertRaises(RuntimeError):
305 dummy_thread.join()
306 dummy_thread._started.clear()
307 with self.assertRaises(RuntimeError):
308 dummy_thread.is_alive()
309 # Busy wait for the following condition: after the thread dies, the
310 # related dummy thread must be removed from threading._active.
311 timeout = 5
312 timeout_at = time.monotonic() + timeout
313 while time.monotonic() < timeout_at:
314 if threading._active.get(dummy_thread.ident) is not dummy_thread:
315 break
316 time.sleep(.1)
317 else:
318 self.fail('It was expected that the created threading._DummyThread was removed from threading._active.')
319
320 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
321 # exposed at the Python level. This test relies on ctypes to get at it.

Callers

nothing calls this directly

Calls 10

acquireMethod · 0.95
LockMethod · 0.80
assertEqualMethod · 0.45
assertRaisesMethod · 0.45
joinMethod · 0.45
clearMethod · 0.45
is_aliveMethod · 0.45
getMethod · 0.45
sleepMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected