Skip to content

Commit 068abea

Browse files
authored
fix: checking for internet connection can timeout (#1813)
* fix: checking for internet connection can timeout In some CI environments or low speed connections, ``HAS_INTERNET`` will return False due to a TimeoutError. This fix speeds up the connection speed by avoiding a DNS lookup and application layer connection through HTTP. ``urlopen`` will also return a TimeoutError if the DNS lookup fails. * docs: update docstring, argument name * docs: update docstring * docs: add entry to whats-new
1 parent f6e074a commit 068abea

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

docs/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ Enhancements
6161
``workflow.merge_gridded_data``. If no grid is provided, the default is to
6262
merge all grids of the provided gdirs (:pull:`1779`).
6363
By `Patrick Schmitt <https://github.com/pat-schmitt>`_
64-
64+
- Fixed ``TimeoutError`` when running tests when urlopen hangs on DNS lookup (:pull:`1813`).
65+
By `Nicolas Gampierakis <https://github.com/gampnico>`_.
6566
Bug fixes
6667
~~~~~~~~~
6768

oggm/tests/__init__.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
2-
from packaging.version import Version
2+
import socket
3+
from urllib.request import URLError, urlopen
34

4-
import pytest
55
import matplotlib.ft2font
6-
from urllib.request import urlopen, URLError
6+
import pytest
7+
from packaging.version import Version
78

89
from oggm import cfg
910
from oggm.utils import SAMPLE_DATA_COMMIT
@@ -12,18 +13,34 @@
1213

1314
# Matplotlib version changes plots, too
1415
HAS_MPL_FOR_TESTS = False
15-
if Version(matplotlib.__version__) >= Version('2'):
16+
if Version(matplotlib.__version__) >= Version("2"):
1617
HAS_MPL_FOR_TESTS = True
1718
BASELINE_DIR = os.path.join(cfg.CACHE_DIR,
1819
'oggm-sample-data-%s' % SAMPLE_DATA_COMMIT,
1920
'baseline_images', 'freetype_28')
2021

21-
# quick n dirty method to see if internet is on
22-
try:
23-
_ = urlopen('http://www.google.com', timeout=1)
24-
HAS_INTERNET = True
25-
except URLError:
26-
HAS_INTERNET = False
22+
def check_internet_access(
23+
hostname: str = "8.8.8.8", port: int = 53, timeout: int = 1
24+
):
25+
"""Check if Internet is available.
26+
27+
hostname : str, default "8.8.8.8"
28+
Web address. Can be a public DNS or an HTTP link.
29+
port : int, default 53
30+
An open and unfiltered port number. This should be 53 for
31+
the domain, or 443 for https.
32+
timeout : int, default 1
33+
Time in seconds before the connection times out.
34+
35+
"""
36+
try:
37+
socket.setdefaulttimeout(timeout)
38+
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((hostname, port))
39+
return True
40+
except socket.error as e:
41+
return False
42+
43+
HAS_INTERNET = check_internet_access()
2744

2845

2946
def mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=1, **kwargs):

0 commit comments

Comments
 (0)