fix: task 8 — add timeout to all requests calls
Add timeout= to every requests.get/post/put/patch/delete call so that background threads cannot hang indefinitely when the network is stalled: - timeout=30 on all API calls (delete, post, patch, get — 13 locations) - timeout=120 on file upload calls (requests.put with data= — 3 locations) to allow sufficient time for large file transfers Add 1 new unit test that scans the source file and fails if any requests.* call is missing a timeout= parameter. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -280,5 +280,50 @@ class TestTask7BareExcept(unittest.TestCase):
|
||||
"on_download_clicked does not sanitize item['name'] with basename for folder downloads")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Task 8: all requests.* calls must carry a timeout= parameter
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestNetworkTimeouts(unittest.TestCase):
|
||||
"""
|
||||
Every requests.get/post/put/patch/delete call must include timeout=
|
||||
so that background threads cannot hang indefinitely on a stalled network.
|
||||
"""
|
||||
|
||||
def _calls_missing_timeout(self):
|
||||
"""
|
||||
Return a list of (line_number, line_text) for every requests.*()
|
||||
call in the source file that is missing a timeout= argument.
|
||||
|
||||
Strategy: single-line calls are the norm in this codebase. We look
|
||||
for lines that contain 'requests.METHOD(' and do NOT also contain
|
||||
'timeout='. Lines that contain 'timeout=' anywhere on the same line
|
||||
are considered compliant.
|
||||
"""
|
||||
import re
|
||||
src_path = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||
"sharepoint_browser.py"
|
||||
)
|
||||
pattern = re.compile(
|
||||
r'requests\.(get|post|put|patch|delete)\('
|
||||
)
|
||||
missing = []
|
||||
with open(src_path, encoding='utf-8') as fh:
|
||||
for lineno, line in enumerate(fh, 1):
|
||||
if pattern.search(line) and 'timeout=' not in line:
|
||||
missing.append((lineno, line.rstrip()))
|
||||
return missing
|
||||
|
||||
def test_all_requests_calls_have_timeout(self):
|
||||
"""Task 8: No requests.* call is missing a timeout= parameter."""
|
||||
missing = self._calls_missing_timeout()
|
||||
if missing:
|
||||
details = "\n".join(f" line {n}: {txt}" for n, txt in missing)
|
||||
self.fail(
|
||||
f"{len(missing)} requests call(s) are missing timeout=:\n{details}"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
|
||||
Reference in New Issue
Block a user