Commit 41d4e323 authored by Brett Cannon's avatar Brett Cannon

Add test.test_support.transient_internet . Returns a context manager that

nests test.test_support.TransientResource context managers that capture
exceptions raised when the Internet connection is flaky.

Initially using in test_socket_ssl but should probably be expanded to cover any
test that should not raise the captured exceptions if the Internet connection
works.
parent 192cd567
...@@ -27,7 +27,7 @@ def test_basic(): ...@@ -27,7 +27,7 @@ def test_basic():
print "didn't raise TypeError" print "didn't raise TypeError"
socket.RAND_add("this is a random string", 75.0) socket.RAND_add("this is a random string", 75.0)
with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT): with test_support.transient_internet():
f = urllib.urlopen('https://sf.net') f = urllib.urlopen('https://sf.net')
buf = f.read() buf = f.read()
f.close() f.close()
......
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
if __name__ != 'test.test_support': if __name__ != 'test.test_support':
raise ImportError, 'test_support must be imported from the test package' raise ImportError, 'test_support must be imported from the test package'
from contextlib import contextmanager import contextlib
import errno
import socket
import sys import sys
import warnings import warnings
...@@ -271,7 +273,7 @@ def open_urlresource(url): ...@@ -271,7 +273,7 @@ def open_urlresource(url):
fn, _ = urllib.urlretrieve(url, filename) fn, _ = urllib.urlretrieve(url, filename)
return open(fn) return open(fn)
@contextmanager @contextlib.contextmanager
def guard_warnings_filter(): def guard_warnings_filter():
"""Guard the warnings filter from being permanently changed.""" """Guard the warnings filter from being permanently changed."""
original_filters = warnings.filters[:] original_filters = warnings.filters[:]
...@@ -338,6 +340,15 @@ class TransientResource(object): ...@@ -338,6 +340,15 @@ class TransientResource(object):
raise ResourceDenied("an optional resource is not available") raise ResourceDenied("an optional resource is not available")
def transient_internet():
"""Return a context manager that raises ResourceDenied when various issues
with the Internet connection manifest themselves as exceptions."""
time_out = TransientResource(IOError, errno=errno.ETIMEDOUT)
socket_peer_reset = TransientResource(socket.error, errno=errno.ECONNRESET)
ioerror_peer_reset = TransientResource(IOError, errno=errno.ECONNRESET)
return contextlib.nested(time_out, peer_reset, ioerror_peer_reset)
#======================================================================= #=======================================================================
# Decorator for running a function in a different locale, correctly resetting # Decorator for running a function in a different locale, correctly resetting
# it afterwards. # it afterwards.
......
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