diff options
| author | Étienne Buira <etienne.buira@gmail.com> | 2016-11-27 12:00:20 +0100 |
|---|---|---|
| committer | Étienne Buira <etienne.buira@gmail.com> | 2016-11-28 01:12:58 +0100 |
| commit | e6ca28e736e13f9bd073f5211aa1acd957f146ef (patch) | |
| tree | 6324309a03d3584b451c3744f2b8463013d78cd8 /test/__init__.py | |
| parent | 505ceb1feba51dbd5987868b9f2fc7a215d45e4d (diff) | |
| download | urllib3-e6ca28e736e13f9bd073f5211aa1acd957f146ef.tar.gz | |
tests: Actually check for network in @requires_network
This is needed because @requires_network:
- was too tightly coupled with the kind of exceptions a test might
raise,
- needed an errno to check against, but this errno is not always
available. An example is
test/with_dummyserver/test_proxy_poolmanager.py:TestHTTPProxyManager.test_https_proxy_timeout
where only a string representation is available in exception.
This check still assumes that TARPIT_HOST is reachable iif there is a
route to Internet.
Diffstat (limited to 'test/__init__.py')
| -rw-r--r-- | test/__init__.py | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/test/__init__.py b/test/__init__.py index bab39ed7..c6560b86 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -88,6 +88,7 @@ def onlyPy3(test): return test(*args, **kwargs) return wrapper +_requires_network_has_route = None def requires_network(test): """Helps you skip tests that require the network""" @@ -95,23 +96,32 @@ def requires_network(test): return getattr(err, 'errno', None) in (errno.ENETUNREACH, errno.EHOSTUNREACH) # For OSX - @functools.wraps(test) - def wrapper(*args, **kwargs): - msg = "Can't run {name} because the network is unreachable".format( - name=test.__name__) + def _has_route(): try: - return test(*args, **kwargs) + sock = socket.create_connection((TARPIT_HOST, 80), 0.0001) + sock.close() + return True + except socket.timeout: + return True except socket.error as e: - # This test needs an initial network connection to attempt the - # connection to the TARPIT_HOST. This fails if you are in a place - # without an Internet connection, so we skip the test in that case. if _is_unreachable_err(e): - raise SkipTest(msg) - raise - except MaxRetryError as e: - if _is_unreachable_err(e.reason): - raise SkipTest(msg) - raise + return False + else: + raise + + @functools.wraps(test) + def wrapper(*args, **kwargs): + global _requires_network_has_route + + if _requires_network_has_route is None: + _requires_network_has_route = _has_route() + + if _requires_network_has_route: + return test(*args, **kwargs) + else: + msg = "Can't run {name} because the network is unreachable".format( + name=test.__name__) + raise SkipTest(msg) return wrapper |
