Skip to content

Commit 92c10dc

Browse files
authored
Merge pull request #777 from MISP/codex/improve-test_wikidata-to-add-retry-logic
Improve Wikidata test resiliency with retries and backoff
2 parents 7dc20bb + 7001463 commit 92c10dc

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

tests/test_expansions.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import os
6+
import time
67
import unittest
78
from base64 import b64encode
89
from urllib.parse import urljoin
@@ -787,13 +788,43 @@ def test_vulners(self):
787788

788789
def test_wikidata(self):
789790
query = {"module": "wiki", "text": "Google"}
790-
response = self.misp_modules_post(query)
791+
retryable_errors = {"Something went wrong, look in the server logs for details"}
792+
max_attempts = 4
793+
wait_seconds = 2
794+
795+
last_response = None
796+
last_exception = None
797+
for attempt in range(1, max_attempts + 1):
798+
try:
799+
last_response = self.misp_modules_post(query)
800+
except requests.exceptions.RequestException as request_exception:
801+
last_exception = request_exception
802+
if attempt < max_attempts:
803+
time.sleep(wait_seconds)
804+
continue
805+
self.fail(f"Wikidata request failed after {max_attempts} attempts: {request_exception}")
806+
807+
try:
808+
self.assertEqual(self.get_values(last_response), "http://www.wikidata.org/entity/Q95")
809+
return
810+
except Exception:
811+
try:
812+
error_message = self.get_errors(last_response)
813+
except Exception:
814+
error_message = None
815+
816+
if error_message not in retryable_errors or attempt == max_attempts:
817+
break
818+
819+
time.sleep(wait_seconds)
820+
821+
if last_response is None and last_exception is not None:
822+
self.fail(f"Wikidata request failed after {max_attempts} attempts: {last_exception}")
823+
791824
try:
792-
self.assertEqual(self.get_values(response), "http://www.wikidata.org/entity/Q95")
793-
except KeyError:
794-
self.assertEqual(self.get_errors(response), "Something went wrong, look in the server logs for details")
825+
self.assertEqual(self.get_values(last_response), "No additional data found on Wikidata")
795826
except Exception:
796-
self.assertEqual(self.get_values(response), "No additional data found on Wikidata")
827+
self.assertEqual(self.get_errors(last_response), "Something went wrong, look in the server logs for details")
797828

798829
def test_xforceexchange(self):
799830
module_name = "xforceexchange"

0 commit comments

Comments
 (0)