Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5)

Side by Side Diff: Lib/test/test_threading.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_threaded_import.py ('k') | Lib/test/test_time.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Very rudimentary test of threading module 1 # Very rudimentary test of threading module
2 2
3 import test.support 3 import test.support
4 from test.support import verbose, strip_python_stderr, import_module 4 from test.support import verbose, strip_python_stderr, import_module
5 import random 5 import random
6 import re 6 import re
7 import sys 7 import sys
8 _thread = import_module('_thread') 8 _thread = import_module('_thread')
9 threading = import_module('threading') 9 threading = import_module('threading')
10 import time 10 import time
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 def test_deamon_param(self): 410 def test_deamon_param(self):
411 t = threading.Thread() 411 t = threading.Thread()
412 self.assertFalse(t.daemon) 412 self.assertFalse(t.daemon)
413 t = threading.Thread(daemon=False) 413 t = threading.Thread(daemon=False)
414 self.assertFalse(t.daemon) 414 self.assertFalse(t.daemon)
415 t = threading.Thread(daemon=True) 415 t = threading.Thread(daemon=True)
416 self.assertTrue(t.daemon) 416 self.assertTrue(t.daemon)
417 417
418 418
419 class ThreadJoinOnShutdown(BaseTestCase): 419 class ThreadJoinOnShutdown(BaseTestCase):
420
421 # Between fork() and exec(), only async-safe functions are allowed (issues
422 # #12316 and #11870), and fork() from a worker thread is known to trigger
423 # problems with some operating systems (issue #3863): skip problematic tests
424 # on platforms known to behave badly.
425 platforms_to_skip = ('freebsd4', 'freebsd5', 'freebsd6', 'netbsd5',
426 'os2emx')
427 420
428 def _run_and_join(self, script): 421 def _run_and_join(self, script):
429 script = """if 1: 422 script = """if 1:
430 import sys, os, time, threading 423 import sys, os, time, threading
431 424
432 # a thread, which waits for the main program to terminate 425 # a thread, which waits for the main program to terminate
433 def joiningfunc(mainthread): 426 def joiningfunc(mainthread):
434 mainthread.join() 427 mainthread.join()
435 print('end of thread') 428 print('end of thread')
436 # stdout is fully buffered because not a tty, we have to flush 429 # stdout is fully buffered because not a tty, we have to flush
(...skipping 11 matching lines...) Expand all
448 import os 441 import os
449 t = threading.Thread(target=joiningfunc, 442 t = threading.Thread(target=joiningfunc,
450 args=(threading.current_thread(),)) 443 args=(threading.current_thread(),))
451 t.start() 444 t.start()
452 time.sleep(0.1) 445 time.sleep(0.1)
453 print('end of main') 446 print('end of main')
454 """ 447 """
455 self._run_and_join(script) 448 self._run_and_join(script)
456 449
457 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") 450 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
458 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
459 def test_2_join_in_forked_process(self): 451 def test_2_join_in_forked_process(self):
460 # Like the test above, but from a forked interpreter 452 # Like the test above, but from a forked interpreter
461 script = """if 1: 453 script = """if 1:
462 childpid = os.fork() 454 childpid = os.fork()
463 if childpid != 0: 455 if childpid != 0:
464 os.waitpid(childpid, 0) 456 os.waitpid(childpid, 0)
465 sys.exit(0) 457 sys.exit(0)
466 458
467 t = threading.Thread(target=joiningfunc, 459 t = threading.Thread(target=joiningfunc,
468 args=(threading.current_thread(),)) 460 args=(threading.current_thread(),))
469 t.start() 461 t.start()
470 print('end of main') 462 print('end of main')
471 """ 463 """
472 self._run_and_join(script) 464 self._run_and_join(script)
473 465
474 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") 466 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
475 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
476 def test_3_join_in_forked_from_thread(self): 467 def test_3_join_in_forked_from_thread(self):
477 # Like the test above, but fork() was called from a worker thread 468 # Like the test above, but fork() was called from a worker thread
478 # In the forked process, the main Thread object must be marked as stoppe d. 469 # In the forked process, the main Thread object must be marked as stoppe d.
479 470
471 # Skip platforms with known problems forking from a worker thread.
472 # See http://bugs.python.org/issue3863.
473 if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'netbsd5',
474 'os2emx'):
475 raise unittest.SkipTest('due to known OS bugs on ' + sys.platform)
480 script = """if 1: 476 script = """if 1:
481 main_thread = threading.current_thread() 477 main_thread = threading.current_thread()
482 def worker(): 478 def worker():
483 childpid = os.fork() 479 childpid = os.fork()
484 if childpid != 0: 480 if childpid != 0:
485 os.waitpid(childpid, 0) 481 os.waitpid(childpid, 0)
486 sys.exit(0) 482 sys.exit(0)
487 483
488 t = threading.Thread(target=joiningfunc, 484 t = threading.Thread(target=joiningfunc,
489 args=(main_thread,)) 485 args=(main_thread,))
490 print('end of main') 486 print('end of main')
491 t.start() 487 t.start()
492 t.join() # Should not block: main_thread is already stopped 488 t.join() # Should not block: main_thread is already stopped
493 489
494 w = threading.Thread(target=worker) 490 w = threading.Thread(target=worker)
495 w.start() 491 w.start()
496 """ 492 """
497 self._run_and_join(script) 493 self._run_and_join(script)
498 494
499 def assertScriptHasOutput(self, script, expected_output): 495 def assertScriptHasOutput(self, script, expected_output):
500 rc, out, err = assert_python_ok("-c", script) 496 rc, out, err = assert_python_ok("-c", script)
501 data = out.decode().replace('\r', '') 497 data = out.decode().replace('\r', '')
502 self.assertEqual(data, expected_output) 498 self.assertEqual(data, expected_output)
503 499
504 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") 500 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
505 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
506 def test_4_joining_across_fork_in_worker_thread(self): 501 def test_4_joining_across_fork_in_worker_thread(self):
507 # There used to be a possible deadlock when forking from a child 502 # There used to be a possible deadlock when forking from a child
508 # thread. See http://bugs.python.org/issue6643. 503 # thread. See http://bugs.python.org/issue6643.
509 504
505 # Skip platforms with known problems forking from a worker thread.
506 # See http://bugs.python.org/issue3863.
507 if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'):
508 raise unittest.SkipTest('due to known OS bugs on ' + sys.platform)
509
510 # The script takes the following steps: 510 # The script takes the following steps:
511 # - The main thread in the parent process starts a new thread and then 511 # - The main thread in the parent process starts a new thread and then
512 # tries to join it. 512 # tries to join it.
513 # - The join operation acquires the Lock inside the thread's _block 513 # - The join operation acquires the Lock inside the thread's _block
514 # Condition. (See threading.py:Thread.join().) 514 # Condition. (See threading.py:Thread.join().)
515 # - We stub out the acquire method on the condition to force it to wait 515 # - We stub out the acquire method on the condition to force it to wait
516 # until the child thread forks. (See LOCK ACQUIRED HERE) 516 # until the child thread forks. (See LOCK ACQUIRED HERE)
517 # - The child thread forks. (See LOCK HELD and WORKER THREAD FORKS 517 # - The child thread forks. (See LOCK HELD and WORKER THREAD FORKS
518 # HERE) 518 # HERE)
519 # - The main thread of the parent process enters Condition.wait(), 519 # - The main thread of the parent process enters Condition.wait(),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 call_count += 1 568 call_count += 1
569 condition.acquire = my_acquire 569 condition.acquire = my_acquire
570 570
571 w.start() 571 w.start()
572 w.join() 572 w.join()
573 print('end of main') 573 print('end of main')
574 """ 574 """
575 self.assertScriptHasOutput(script, "end of main\n") 575 self.assertScriptHasOutput(script, "end of main\n")
576 576
577 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") 577 @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
578 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
579 def test_5_clear_waiter_locks_to_avoid_crash(self): 578 def test_5_clear_waiter_locks_to_avoid_crash(self):
580 # Check that a spawned thread that forks doesn't segfault on certain 579 # Check that a spawned thread that forks doesn't segfault on certain
581 # platforms, namely OS X. This used to happen if there was a waiter 580 # platforms, namely OS X. This used to happen if there was a waiter
582 # lock in the thread's condition variable's waiters list. Even though 581 # lock in the thread's condition variable's waiters list. Even though
583 # we know the lock will be held across the fork, it is not safe to 582 # we know the lock will be held across the fork, it is not safe to
584 # release locks held across forks on all platforms, so releasing the 583 # release locks held across forks on all platforms, so releasing the
585 # waiter lock caused a segfault on OS X. Furthermore, since locks on 584 # waiter lock caused a segfault on OS X. Furthermore, since locks on
586 # OS X are (as of this writing) implemented with a mutex + condition 585 # OS X are (as of this writing) implemented with a mutex + condition
587 # variable instead of a semaphore, while we know that the Python-level 586 # variable instead of a semaphore, while we know that the Python-level
588 # lock will be acquired, we can't know if the internal mutex will be 587 # lock will be acquired, we can't know if the internal mutex will be
589 # acquired at the time of the fork. 588 # acquired at the time of the fork.
590 589
590 # Skip platforms with known problems forking from a worker thread.
591 # See http://bugs.python.org/issue3863.
592 if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'):
593 raise unittest.SkipTest('due to known OS bugs on ' + sys.platform)
591 script = """if True: 594 script = """if True:
592 import os, time, threading 595 import os, time, threading
593 596
594 start_fork = False 597 start_fork = False
595 598
596 def worker(): 599 def worker():
597 # Wait until the main thread has attempted to join this thread 600 # Wait until the main thread has attempted to join this thread
598 # before continuing. 601 # before continuing.
599 while not start_fork: 602 while not start_fork:
600 time.sleep(0.01) 603 time.sleep(0.01)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 import os 643 import os
641 import random 644 import random
642 import sys 645 import sys
643 import time 646 import time
644 import threading 647 import threading
645 648
646 thread_has_run = set() 649 thread_has_run = set()
647 650
648 def random_io(): 651 def random_io():
649 '''Loop for a while sleeping random tiny amounts and doing some I/O.''' 652 '''Loop for a while sleeping random tiny amounts and doing some I/O.'''
653 blank = b'x' * 200
650 while True: 654 while True:
651 in_f = open(os.__file__, 'rb') 655 in_f = open(os.__file__, 'r')
652 stuff = in_f.read(200) 656 stuff = in_f.read(200)
653 null_f = open(os.devnull, 'wb') 657 null_f = open(os.devnull, 'w')
654 null_f.write(stuff) 658 null_f.write(stuff)
655 time.sleep(random.random() / 1995) 659 time.sleep(random.random() / 1995)
656 null_f.close() 660 null_f.close()
657 in_f.close() 661 in_f.close()
658 thread_has_run.add(threading.current_thread()) 662 thread_has_run.add(threading.current_thread())
659 663
660 def main(): 664 def main():
661 count = 0 665 count = 0
662 for _ in range(40): 666 for _ in range(40):
663 new_thread = threading.Thread(target=random_io) 667 new_thread = threading.Thread(target=random_io)
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 ConditionAsRLockTests, ConditionTests, 770 ConditionAsRLockTests, ConditionTests,
767 SemaphoreTests, BoundedSemaphoreTests, 771 SemaphoreTests, BoundedSemaphoreTests,
768 ThreadTests, 772 ThreadTests,
769 ThreadJoinOnShutdown, 773 ThreadJoinOnShutdown,
770 ThreadingExceptionTests, 774 ThreadingExceptionTests,
771 BarrierTests, 775 BarrierTests,
772 ) 776 )
773 777
774 if __name__ == "__main__": 778 if __name__ == "__main__":
775 test_main() 779 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_threaded_import.py ('k') | Lib/test/test_time.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+