Skip to content

Commit 6fb2cbb

Browse files
authored
Merge pull request #213 from pnuu/fix-tle-download-timeout-failure
Fix timeout preventing storage of TLEs
2 parents 51bbe8e + b55ac29 commit 6fb2cbb

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

pyorbital/tests/test_tlefile.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,24 @@ def test_fetch_plain_tle_two_sources(self, requests):
560560
assert "source_2" in res
561561
assert len(res["source_2"]) == 1
562562
assert mock.call("mocked_url_1", timeout=15) in requests.get.mock_calls
563-
assert len(requests.get.mock_calls) == 4
563+
assert len([c for c in requests.get.mock_calls if c.args]) == 4
564+
565+
@mock.patch("logging.error")
566+
@mock.patch("pyorbital.tlefile.requests.get")
567+
def test_fetch_plain_tle_timeout(self, requests_get, logging_error):
568+
"""Test that timeout is logged."""
569+
from requests.exceptions import Timeout
570+
571+
requests_get.side_effect = Timeout
572+
573+
self.dl.config["downloaders"] = FETCH_PLAIN_TLE_CONFIG
574+
575+
res = self.dl.fetch_plain_tle()
576+
for url in ["mocked_url_1", "mocked_url_2", "mocked_url_3", "mocked_url_4"]:
577+
expected = mock.call(f"Failed to make request to {url} within 15 seconds!")
578+
assert expected in logging_error.mock_calls
579+
assert not res["source_1"]
580+
assert not res["source_2"]
564581

565582
@mock.patch("pyorbital.tlefile.requests")
566583
def test_fetch_plain_tle_server_is_a_teapot(self, requests):
@@ -580,7 +597,7 @@ def test_fetch_plain_tle_server_is_a_teapot(self, requests):
580597
assert len(res["source_2"]) == 0
581598

582599
assert mock.call("mocked_url_1", timeout=15) in requests.get.mock_calls
583-
assert len(requests.get.mock_calls) == 4
600+
assert len([c for c in requests.get.mock_calls if c.args]) == 4
584601

585602
@mock.patch("pyorbital.tlefile.requests")
586603
def test_fetch_spacetrack_login_fails(self, requests):

pyorbital/tlefile.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252
LOGGER = logging.getLogger(__name__)
5353
PKG_CONFIG_DIR = os.path.join(os.path.realpath(os.path.dirname(__file__)), "etc")
5454

55-
class TleDownloadTimeoutError(Exception):
56-
"""TLE download timeout exception."""
57-
5855

5956
def _get_config_path():
6057
"""Get the config path for Pyorbital."""
@@ -485,17 +482,17 @@ def fetch_plain_tle(self):
485482
try:
486483
req = requests.get(uri, timeout=15) # 15 seconds
487484
except requests.exceptions.Timeout:
488-
raise TleDownloadTimeoutError(f"Failed to make request to {str(uri)} within 15 seconds!")
489-
if req.status_code == 200:
485+
logging.error(f"Failed to make request to {str(uri)} within 15 seconds!")
486+
req = None
487+
if req and req.status_code == 200:
490488
tles[source] += _parse_tles_for_downloader((req.text,), io.StringIO)
491489
else:
492490
failures.append(uri)
493491
if len(failures) > 0:
494492
logging.error(
495493
"Could not fetch TLEs from %s, %d failure(s): [%s]",
496494
source, len(failures), ", ".join(failures))
497-
logging.info("Downloaded %d TLEs from %s",
498-
len(tles[source]), source)
495+
logging.info("Downloaded %d TLEs from %s", len(tles[source]), source)
499496
return tles
500497

501498
def fetch_spacetrack(self):

0 commit comments

Comments
 (0)