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

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

Issue 18756: os.urandom() fails under high load
Patch Set: Created 6 years, 3 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
OLDNEW
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 1002 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 self.assertEqual(len(stdout), 16) 1013 self.assertEqual(len(stdout), 16)
1014 return stdout 1014 return stdout
1015 1015
1016 def test_urandom_subprocess(self): 1016 def test_urandom_subprocess(self):
1017 data1 = self.get_urandom_subprocess(16) 1017 data1 = self.get_urandom_subprocess(16)
1018 data2 = self.get_urandom_subprocess(16) 1018 data2 = self.get_urandom_subprocess(16)
1019 self.assertNotEqual(data1, data2) 1019 self.assertNotEqual(data1, data2)
1020 1020
1021 @unittest.skipUnless(resource, "test requires the resource module") 1021 @unittest.skipUnless(resource, "test requires the resource module")
1022 def test_urandom_failure(self): 1022 def test_urandom_failure(self):
1023 soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) 1023 # Spawn a new process to avoid breaking urandom() for the rest
1024 resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) 1024 # of the process' lifetime.
1025 try: 1025 code = """if 1:
1026 with self.assertRaises(OSError) as cm: 1026 import errno
1027 import os
1028 import resource
1029
1030 soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
1031 os.closerange(3, soft_limit)
1032 try:
1027 os.urandom(16) 1033 os.urandom(16)
1028 self.assertEqual(cm.exception.errno, errno.EMFILE) 1034 except OSError as e:
1029 finally: 1035 assert e.errno == errno.EBADF, e.errno
1030 # We restore the old limit as soon as possible. If doing it 1036 else:
1031 # using addCleanup(), code running in between would fail 1037 raise AssertionError("OSError not raised")
1032 # creating any file descriptor. 1038 """
1033 resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit)) 1039 assert_python_ok('-c', code)
1034 1040
1035 1041
1036 @contextlib.contextmanager 1042 @contextlib.contextmanager
1037 def _execvpe_mockup(defpath=None): 1043 def _execvpe_mockup(defpath=None):
1038 """ 1044 """
1039 Stubs out execv and execve functions when used as context manager. 1045 Stubs out execv and execve functions when used as context manager.
1040 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
1041 exception as they would normally never return. 1047 exception as they would normally never return.
1042 """ 1048 """
1043 # A list of tuples containing (function name, first arg, args) 1049 # A list of tuples containing (function name, first arg, args)
(...skipping 1274 matching lines...) Expand 10 before | Expand all | Expand 10 after
2318 ExtendedAttributeTests, 2324 ExtendedAttributeTests,
2319 Win32DeprecatedBytesAPI, 2325 Win32DeprecatedBytesAPI,
2320 TermsizeTests, 2326 TermsizeTests,
2321 OSErrorTests, 2327 OSErrorTests,
2322 RemoveDirsTests, 2328 RemoveDirsTests,
2323 CPUCountTests, 2329 CPUCountTests,
2324 ) 2330 )
2325 2331
2326 if __name__ == "__main__": 2332 if __name__ == "__main__":
2327 test_main() 2333 test_main()
OLDNEW

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