From 824a44a4869d6b8c15ee96c790e1887fa3412598 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 25 Oct 2022 21:53:44 +1100 Subject: [PATCH] Improvements for CI tests (#3850) * Improvements for CI tests * Add delay between retries * Reduce tiemeout for request * Change exception type we are looking for --- InvenTree/InvenTree/tests.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 145b271480..f8d64d1261 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -258,7 +258,7 @@ class TestHelpers(TestCase): def test_download_image(self): """Test function for downloading image from remote URL""" - # Run check with a sequency of bad URLs + # Run check with a sequence of bad URLs for url in [ "blog", "htp://test.com/?", @@ -268,17 +268,35 @@ class TestHelpers(TestCase): with self.assertRaises(django_exceptions.ValidationError): helpers.download_image_from_url(url) + def dl_helper(url, expected_error, timeout=2.5, retries=3): + """Helper function for unit testing downloads. + + As the httpstat.us service occassionaly refuses a connection, + we will simply try multiple times + """ + + with self.assertRaises(expected_error): + while retries > 0: + + try: + helpers.download_image_from_url(url, timeout=timeout) + break + except Exception as exc: + if type(exc) is expected_error: + # Re-throw this error + raise exc + + time.sleep(30) + retries -= 1 + # Attempt to download an image which throws a 404 - with self.assertRaises(requests.exceptions.HTTPError): - helpers.download_image_from_url("https://httpstat.us/404", timeout=10) + dl_helper("https://httpstat.us/404", requests.exceptions.HTTPError, timeout=10) # Attempt to download, but timeout - with self.assertRaises(requests.exceptions.Timeout): - helpers.download_image_from_url("https://httpstat.us/200?sleep=5000") + dl_helper("https://httpstat.us/200?sleep=5000", requests.exceptions.ReadTimeout, timeout=1) # Attempt to download, but not a valid image - with self.assertRaises(TypeError): - helpers.download_image_from_url("https://httpstat.us/200", timeout=10) + dl_helper("https://httpstat.us/200", TypeError, timeout=10) large_img = "https://github.com/inventree/InvenTree/raw/master/InvenTree/InvenTree/static/img/paper_splash_large.jpg"