this repo has no description
at trunk 45 lines 1.3 kB view raw
1#!/usr/bin/env python3 2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 3import _io 4import _thread 5import contextlib 6import time 7import unittest 8 9 10_thread._enable_threads = True 11 12 13class UnderThreadTest(unittest.TestCase): 14 def test_start_new_thread_returns_new_thread(self): 15 def bootstrap(): 16 pass 17 18 current_id = _thread.get_ident() 19 new_id = _thread.start_new_thread(bootstrap, ()) 20 time.sleep(1.0) # TODO(T66337218): remove when we can join on threads 21 22 self.assertIsInstance(new_id, int) 23 self.assertNotEqual(new_id, current_id) 24 25 def test_start_new_thread_with_exception_prints_error(self): 26 def bootstrap(): 27 raise RuntimeError 28 29 with _io.StringIO() as stderr, contextlib.redirect_stderr(stderr): 30 _thread.start_new_thread(bootstrap, ()) 31 time.sleep(1.0) # TODO(T66337218): remove when we can join on threads 32 33 self.maxDiff = 10000 34 self.assertRegex( 35 stderr.getvalue(), 36 r"""Exception ignored in thread started by: <function .*bootstrap at 0x[0-9a-f]+> 37Traceback \(most recent call last\): 38 File ".*/_thread_test.py", line \d+, in bootstrap 39 raise RuntimeError 40RuntimeError""", 41 ) 42 43 44if __name__ == "__main__": 45 unittest.main()