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

Delta Between Two Patch Sets: Lib/test/test_os.py

Issue 18756: os.urandom() fails under high load
Left Patch Set: Created 6 years, 1 month ago
Right Patch Set: Created 6 years, 1 month 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 # As a test suite for the os module, this is woefully inadequate, but this 1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more 2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be. 3 # portable than they had been thought to be.
4 4
5 import os 5 import os
6 import errno 6 import errno
7 import unittest 7 import unittest
8 import warnings 8 import warnings
9 import sys 9 import sys
10 import signal 10 import signal
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 key = 'key=' 639 key = 'key='
640 self.assertRaises(OSError, os.environ.__delitem__, key) 640 self.assertRaises(OSError, os.environ.__delitem__, key)
641 641
642 def test_key_type(self): 642 def test_key_type(self):
643 missing = 'missingkey' 643 missing = 'missingkey'
644 self.assertNotIn(missing, os.environ) 644 self.assertNotIn(missing, os.environ)
645 645
646 with self.assertRaises(KeyError) as cm: 646 with self.assertRaises(KeyError) as cm:
647 os.environ[missing] 647 os.environ[missing]
648 self.assertIs(cm.exception.args[0], missing) 648 self.assertIs(cm.exception.args[0], missing)
649 self.assertTrue(cm.exception.__suppress_context__)
649 650
650 with self.assertRaises(KeyError) as cm: 651 with self.assertRaises(KeyError) as cm:
651 del os.environ[missing] 652 del os.environ[missing]
652 self.assertIs(cm.exception.args[0], missing) 653 self.assertIs(cm.exception.args[0], missing)
654 self.assertTrue(cm.exception.__suppress_context__)
655
653 656
654 class WalkTests(unittest.TestCase): 657 class WalkTests(unittest.TestCase):
655 """Tests for os.walk().""" 658 """Tests for os.walk()."""
656 659
657 def setUp(self): 660 def setUp(self):
658 import os 661 import os
659 from os.path import join 662 from os.path import join
660 663
661 # Build: 664 # Build:
662 # TESTFN/ 665 # TESTFN/
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 self.assertEqual(len(stdout), 16) 1013 self.assertEqual(len(stdout), 16)
1011 return stdout 1014 return stdout
1012 1015
1013 def test_urandom_subprocess(self): 1016 def test_urandom_subprocess(self):
1014 data1 = self.get_urandom_subprocess(16) 1017 data1 = self.get_urandom_subprocess(16)
1015 data2 = self.get_urandom_subprocess(16) 1018 data2 = self.get_urandom_subprocess(16)
1016 self.assertNotEqual(data1, data2) 1019 self.assertNotEqual(data1, data2)
1017 1020
1018 @unittest.skipUnless(resource, "test requires the resource module") 1021 @unittest.skipUnless(resource, "test requires the resource module")
1019 def test_urandom_failure(self): 1022 def test_urandom_failure(self):
1020 # Spawn a new process to circumvent fd caching in Python/random.c 1023 # Spawn a new process to avoid breaking urandom() for the rest
1024 # of the process' lifetime.
1021 code = """if 1: 1025 code = """if 1:
1022 import errno 1026 import errno
1023 import os 1027 import os
1024 import resource 1028 import resource
1025 1029
1026 soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) 1030 soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
1027 resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) 1031 os.closerange(3, soft_limit)
1028 try: 1032 try:
1029 os.urandom(16) 1033 os.urandom(16)
1030 except OSError as e: 1034 except OSError as e:
1031 assert e.errno == errno.EMFILE, e.errno 1035 assert e.errno == errno.EBADF, e.errno
1032 else: 1036 else:
1033 raise AssertionError("OSError not raised") 1037 raise AssertionError("OSError not raised")
1034 finally:
1035 resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_lim it))
1036 """ 1038 """
1037 assert_python_ok('-c', code) 1039 assert_python_ok('-c', code)
1038 1040
1039 1041
1040 @contextlib.contextmanager 1042 @contextlib.contextmanager
1041 def _execvpe_mockup(defpath=None): 1043 def _execvpe_mockup(defpath=None):
1042 """ 1044 """
1043 Stubs out execv and execve functions when used as context manager. 1045 Stubs out execv and execve functions when used as context manager.
1044 Records exec calls. The mock execv and execve functions always raise an 1046 Records exec calls. The mock execv and execve functions always raise an
1045 exception as they would normally never return. 1047 exception as they would normally never return.
(...skipping 1276 matching lines...) Expand 10 before | Expand all | Expand 10 after
2322 ExtendedAttributeTests, 2324 ExtendedAttributeTests,
2323 Win32DeprecatedBytesAPI, 2325 Win32DeprecatedBytesAPI,
2324 TermsizeTests, 2326 TermsizeTests,
2325 OSErrorTests, 2327 OSErrorTests,
2326 RemoveDirsTests, 2328 RemoveDirsTests,
2327 CPUCountTests, 2329 CPUCountTests,
2328 ) 2330 )
2329 2331
2330 if __name__ == "__main__": 2332 if __name__ == "__main__":
2331 test_main() 2333 test_main()
LEFTRIGHT

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