Commit e7587151 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #18756: make test_urandom_failure more robust by executing its code in a subprocess

parent c8f26f50
...@@ -16,6 +16,7 @@ except ImportError: ...@@ -16,6 +16,7 @@ except ImportError:
resource = None resource = None
from test import test_support from test import test_support
from test.script_helper import assert_python_ok
import mmap import mmap
import uuid import uuid
...@@ -569,17 +570,26 @@ class URandomTests (unittest.TestCase): ...@@ -569,17 +570,26 @@ class URandomTests (unittest.TestCase):
@unittest.skipUnless(resource, "test requires the resource module") @unittest.skipUnless(resource, "test requires the resource module")
def test_urandom_failure(self): def test_urandom_failure(self):
# Check urandom() failing when it is not able to open /dev/random.
# We spawn a new process to make the test more robust (if getrlimit()
# failed to restore the file descriptor limit after this, the whole
# test suite would crash; this actually happened on the OS X Tiger
# buildbot).
code = """if 1:
import errno
import os
import resource
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
try: try:
with self.assertRaises(OSError) as cm:
os.urandom(16) os.urandom(16)
self.assertEqual(cm.exception.errno, errno.EMFILE) except OSError as e:
finally: assert e.errno == errno.EMFILE, e.errno
# We restore the old limit as soon as possible. If doing it else:
# using addCleanup(), code running in between would fail raise AssertionError("OSError not raised")
# creating any file descriptor. """
resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit)) assert_python_ok('-c', code)
class ExecvpeTests(unittest.TestCase): class ExecvpeTests(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment