Parcourir la source

test: retry dropped connections in the web-stack harness (app closes a very early keep-alive connection once while warming up)

clanker il y a 1 mois
Parent
commit
083286d86e
1 fichiers modifiés avec 12 ajouts et 5 suppressions
  1. 12 5
      test/web_stack_test.py

+ 12 - 5
test/web_stack_test.py

@@ -77,11 +77,18 @@ def http(method, url, data=None, headers=None, timeout=30):
         request.add_header("Content-Type", "application/x-www-form-urlencoded")
     for key, value in (headers or {}).items():
         request.add_header(key, value)
-    try:
-        with urllib.request.urlopen(request, timeout=timeout) as response:
-            return response.status, response.read().decode()
-    except urllib.error.HTTPError as error:
-        return error.code, error.read().decode()
+    # One retry on a dropped connection: the freshly started app can
+    # close a very early keep-alive connection once while warming up.
+    for attempt in range(2):
+        try:
+            with urllib.request.urlopen(request, timeout=timeout) as response:
+                return response.status, response.read().decode()
+        except urllib.error.HTTPError as error:
+            return error.code, error.read().decode()
+        except (urllib.error.URLError, OSError):
+            if attempt > 0:
+                raise
+            time.sleep(0.5)
 
 
 def directives(uri, auth=None):