Retry interrupted package response bodies during compatibility tests
This commit is contained in:
1 parent
5707266d5e
commit
13768728ba
2 files changed
+27
-2
No files matched your search
@@ -1,14 +1,38 @@
|
||||
"""Compatibility downloads tolerate transport outages without hiding bad data."""
|
||||
import io
|
||||
import http.client
|
||||
import socket
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
import urllib.error
|
||||
|
||||
from package_compat_test import fetch
|
||||
|
||||
|
||||
class FetchTest(unittest.TestCase):
|
||||
def test_interrupted_body_is_discarded_and_download_restarts(self):
|
||||
interrupted = MagicMock()
|
||||
interrupted.__enter__.return_value.read.side_effect = http.client.IncompleteRead(b"partial")
|
||||
with (
|
||||
patch("package_compat_test.urllib.request.urlopen",
|
||||
side_effect=[interrupted, io.BytesIO(b"package")]) as request,
|
||||
patch("package_compat_test.time.sleep"),
|
||||
):
|
||||
self.assertEqual(fetch("https://api.waozi.xyz/package.zx", 7), b"package")
|
||||
self.assertEqual(request.call_count, 2)
|
||||
interrupted.__exit__.assert_called_once()
|
||||
|
||||
def test_repeated_interrupted_bodies_remain_a_failure(self):
|
||||
interrupted = MagicMock()
|
||||
interrupted.__enter__.return_value.read.side_effect = http.client.IncompleteRead(b"partial")
|
||||
with (
|
||||
patch("package_compat_test.urllib.request.urlopen", return_value=interrupted) as request,
|
||||
patch("package_compat_test.time.sleep"),
|
||||
):
|
||||
with self.assertRaises(http.client.IncompleteRead):
|
||||
fetch("https://api.waozi.xyz/package.zx", 7)
|
||||
self.assertEqual(request.call_count, 4)
|
||||
|
||||
def test_temporary_dns_recovers_and_payload_is_returned(self):
|
||||
outage = urllib.error.URLError(socket.gaierror(socket.EAI_AGAIN, "temporary DNS outage"))
|
||||
with (
|
||||
|
||||
@@ -17,6 +17,7 @@ the computer and restarted to check the saved game.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -73,7 +74,7 @@ def fetch(url, limit):
|
||||
temporary_dns = isinstance(reason, socket.gaierror) and reason.errno == socket.EAI_AGAIN
|
||||
if not (temporary_dns or isinstance(reason, (TimeoutError, ConnectionError))) or attempt == 3:
|
||||
raise
|
||||
except (TimeoutError, ConnectionError):
|
||||
except (TimeoutError, ConnectionError, http.client.IncompleteRead):
|
||||
if attempt == 3:
|
||||
raise
|
||||
destination = urllib.parse.urlsplit(url)
|
||||
|
||||
Reference in new issue
Block a user